Instructions Arrangement
本题事件(顶点)为事件结束。方便求最短完成时间,即关键路径长度。
#include<bits/stdc++.h>
using namespace std;const int Max=1000+10;
const int INF=INT_MAX;
struct Edge{
int to;
int l;
Edge(int x,int y):to(x),l(y){}
};
vector<Edge> graph[Max];
int ve[Max];
int vl[Max];
int indegree[Max];
void CritialPath(int n){
vector<int> topology;
queue<int> node;
for(int i=0;i<n;i++){
if(indegree[i]==0){
node.emplace(i);
ve[i]=1; //度为0的点为实际源点,顶点代表事件结束,所以初始化为1 !!
}
}
while(!node.empty()){
int u=node.front();
topology.emplace_back(u);
node.pop();
for(int i=0;i<graph[u].size();i++){
int v=graph[u][i].to;
int d=graph[u][i].l;
ve[v]=max(ve[v],ve[u]+d);
indegree[v]--;
if(indegree[v]==0){
node.emplace(v);
}
}
}
}
int main(){
int n,m;
while(cin>>n>>m){
for(int i=0;i<m;i++){
int from,to,l;
cin>>from>>to>>l;
graph[from].emplace_back(Edge(to,l));
indegree[to]++;
}
CritialPath(n);
int answer=0;
for(int i=0;i<n;i++){
answer=max(answer,ve[i]);
}
cout<<answer<<endl;
}
return 0;
}