标签:c++
5 2 1 2 1 3 4 1
2HintIn the 1st ns, instruction 0, 1 and 3 are executed; In the 2nd ns, instruction 2 and 4 are executed. So the answer should be 2.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
struct node
{
int to,value;
};
vector<node>next[1005]; //向量+结构体 邻接表
int in_degree[1005]; //节点的入度
int finish[1005]; //节点的开始时间
int m,n;
void topsort()
{
queue<int>q;
int i,j;
int now;
for(i=0;i<m;i++)
if(in_degree[i]==0)
{
finish[i]=1; //题意
q.push(i);
}
while(!q.empty())
{
now=q.front();
// cout<<now<<endl;
q.pop();
for(i=0;i<next[now].size();i++)
{
int b=next[now][i].to;
int v=next[now][i].value;
finish[b]=max(finish[b],finish[now]+v); //某个工程的开始时间
in_degree[b]--;
if(in_degree[b]==0)
q.push(b);
}
}
return;
}
int main()
{
int i,j;
while(~scanf("%d%d",&m,&n))
{
memset(finish,0,sizeof(finish));
memset(in_degree,0,sizeof(in_degree));
for(i=0; i<=m; i++)
next[i].clear();
int a,b,c;
node d;
while(n--)
{
scanf("%d%d%d",&a,&b,&c);
d.to=b;
d.value=c;
next[a].push_back(d);
in_degree[b]++;
}
topsort();
int ans=0;
for(i=0;i<=m;i++)
if(finish[i]>ans)
ans=finish[i];
cout<<ans<<endl;
}
return 0;
}
HDU4109 Instrction Arrangement 拓扑排序 关键路径
标签:c++
原文地址:http://blog.csdn.net/axuan_k/article/details/41788033