08-图8 How Long Does It Take (25分)
#include<iostream>
#include<queue>
#define MaxSize 100
#define INFINITY 63225
using namespace std;
int MGraph[MaxSize][MaxSize];
int Dist[MaxSize]={0};
int InDegree[MaxSize];
int N,M;//N represented the number of the activities, M is on behalf of the number of the working pathes
int S,E,L;
void TopSort();
int main()
{
cin >> N >> M;
int i,j;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
MGraph[i][j] =INFINITY;
for(i=0;i<M;i++)
{
cin >>S >> E>> L;
MGraph[S][E] =L;
InDegree[E]++;
}
TopSort();
return 0;
}
void TopSort()
{
queue<int>q;
int i,earliestTime=0;
for(i=0;i<N;i++)
{
if(InDegree[i]==0)
q.push(i);
}
int cnt = 0;//restore the activities which were already excuted
int value;
while(!q.empty())
{
value = q.front();
q.pop();
cnt++;
for(i=0;i<N;i++)
{
if(MGraph[value][i]!=INFINITY)
{
if(--InDegree[i]==0)q.push(i);
if(Dist[i]<MGraph[value][i]+Dist[value]) Dist[i]=MGraph[value][i]+Dist[value];
if(earliestTime<Dist[i]) earliestTime = Dist[i];
}
}
}
if(cnt==N) cout<< earliestTime;
else cout<<"Impossible";
}