Problem Description
Ali has taken the Computer Organization and Architecture course this term. He learned that there may be dependence between instructions, like WAR (write after read), WAW, RAW. If the distance between two instructions is less than the Safe Distance, it will result in hazard, which may cause wrong result. So we need to design special circuit to eliminate hazard. However the most simple way to solve this problem is to add bubbles (useless operation), which means wasting time to ensure that the distance between two instructions is not smaller than the Safe Distance. The definition of the distance between two instructions is the difference between their beginning times. Now we have many instructions, and we know the dependent relations and Safe Distances between instructions. We also have a very strong CPU with infinite number of cores, so you can run as many instructions as you want simultaneity, and the CPU is so fast that it just cost 1ns to finish any instruction. Your job is to rearrange the instructions so that the CPU can finish all the instructions using minimum time.
Input
The input consists several testcases. The first line has two integers N, M (N <= 1000, M <= 10000), means that there are N instructions and M dependent relations. The following M lines, each contains three integers X, Y , Z, means the Safe Distance between X and Y is Z, and Y should run after X. The instructions are numbered from 0 to N - 1.
Output
Print one integer, the minimum time the CPU needs to run.
Sample Input
5 2
1 2 1
3 4 1
Sample Output
2
拓扑排序
还是多线程的
题意:有n个指令m个要求 例如 X Y Z 代表 指令Y必须在指令X后 Z秒执行 输出cpu运行的最小时间
运行最小时间 也就是要满足最大的时间要求
vector<node>存图 node 结构体包含 mubiao timm
入读为零的点 tim[] 初始化为1
tim[mp[j][i].mubiao]=max(tim[mp[j][i].mubiao],temp+mp[j][i].timm); //确保满足最大时间要求
#include<bits/stdc++.h> using namespace std; int n,m; int g; struct node { int mubiao; int timm; }gg[10005]; vector<node> mp[1005]; int tim[1005]; int in[1005]; queue<int>q; int max(int ss,int bb) { if(ss>bb) return ss; return bb; } //struct node gg; int main() { while(scanf("%d%d",&n,&m)!=EOF) { for(int i=0;i<n;i++) { mp[i].clear(); in[i]=0; } memset(tim,0,sizeof(tim)); for(int i=0;i<m;i++) { scanf("%d%d%d",&g,&gg[i].mubiao,&gg[i].timm); mp[g].push_back(gg[i]); in[gg[i].mubiao]++; } for(int i=0;i<n;i++) { if(in[i]==0) { q.push(i); tim[i]=1; } } int re=0; while(!q.empty()) { int j=q.front(); q.pop(); int temp=tim[j]; if(re<=temp) re=temp; for(unsigned int i=0;i<mp[j].size();i++) { tim[mp[j][i].mubiao]=max(tim[mp[j][i].mubiao],temp+mp[j][i].timm); if(--in[mp[j][i].mubiao]==0) { q.push(mp[j][i].mubiao); } } } printf("%d\n",re); } return 0; }