PTA shortest duration (topological sorting)

A project consists of several tasks, there has dependent order between tasks. Project managers need to set a series of milestones, each milestone in the completion of the inspection tasks node, and start the follow-up tasks. Now the relationship between the various tasks to a given project, you first calculate the completion time of the project.

Input formats:

First, the first line gives two positive integers: the number of project milestones N (≤100) and the total number of tasks M. Milestone here from 0 to N-1 numbers. Subsequently M rows, each row describes a given task, the format of "task duration milestone task start working towards the end of the" three numbers are non-negative integers, separated by spaces.

Output formats:

If you schedule the entire project is feasible, the output of the earliest completion time in a row; otherwise output "Impossible".

Sample Input 1:

9 12
0 1 6
0 2 4
0 3 5
1 4 1
2 4 1
3 5 2
5 4 0
4 6 9
4 7 7
5 7 4
6 8 2
7 8 4

Output Sample 1:

18

Sample Input 2:

4 5
0 1 1
0 2 2
2 1 3
1 3 4
3 2 5

Output Sample 2:

Impossible

Use adjacency matrix construct directed acyclic graph, topological sorting, and finally into the team if the node is not the total number of nodes, then the unsorted success, or have circuit ...
incidentally brushed up the critical path, the first time take place at the latest time of occurrence of the concept.
Completion time for the entire event is length AOE FIG starting point to the end point from the longest path, this path is called the critical path. Activities on the critical path is called critical activities .
Critical path is not necessarily only one.
The earliest occurrence time : from front to back, the precursor node to the current node required time, takes a maximum value.
The latest occurrence time : back to front, the latest occurrence time successor node - the edge weight value, minimum value.

#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<list>
#include<set>
#include<cmath>
using namespace std;
typedef long long ll;
#define N 109
int mod = 1e9+7,n,m;
#define M 249997
int vis[N];
int edge[N][N];
int degree[N],time1[N];
int main()
{
	cin >> n >> m;
	int x,y,val,cnt=0;
	queue<int> qu;
	memset(edge,-1,sizeof(edge));
	for(int i=0;i<m;i++){
		scanf("%d %d %d",&x,&y,&val);
		edge[x][y] = val;
		degree[y]++;
	}
	for(int i=0;i<n;i++){
		if(degree[i]==0) {
			qu.push(i);
			cnt ++;
		}
	}
	while(qu.size()){
		int e = qu.front();
		qu.pop();
		for(int i=0;i<n;i++){
			if(edge[e][i]!=-1){
				degree[i]--;
				if(degree[i]==0) {
					qu.push(i);
					cnt ++;
				}
				time1[i] = max(time1[i],time1[e]+edge[e][i]);
			}
		}
	}
//	cout << cnt << endl;
	if(cnt!=n) cout << "Impossible" << endl;
	else {
		int ret = 0;
		for(int i=0;i<n;i++) ret = max(ret,time1[i]);
		cout << ret << endl; 
	}
    return 0;
}

Published 79 original articles · won praise 37 · views 8877

Guess you like

Origin blog.csdn.net/SinclairWang/article/details/104255038