PAT--How Long Does It Take (25)

Given the relations of all the activities of a project, you are supposed to find the earliest completion time of the project.

Input Specification:

Each input file contains one test case. Each case starts with a line containing two positive integers N (<=100), the number of activity check points (hence it is assumed that the check points are numbered from 0 to N-1), and M, the number of activities. Then M lines follow, each gives the description of an activity. For the i-th activity, three non-negative numbers are given: S[i], E[i], and L[i], where S[i] is the index of the starting check point, E[i] of the ending check point, and L[i] the lasting time of the activity. The numbers in a line are separated by a space.

Output Specification:

For each test case, if the scheduling is possible, print in a line its earliest completion time; or simply 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
Sample Output 1:
18
Sample Input 2:
4 5
0 1 1
0 2 2
2 1 3
1 3 4
3 2 5
Sample Output 2:
Impossible

典型的拓扑排序,可是很久没刷过类似的题了。还是有必要好好研究研究。

重点就在于,拓扑排序过程中如何记录cost,以及如果有多个重点最后输出何值。

解决方法是,在要删掉某个可以拿出来的节点时,修正这条节点(front)指向的节点(i)的cost值cost[i]=max(cost[i],cost[front]+info[front][i]);

最后输出出度为零的节点中耗时最多的节点cost。其实单纯输出最大值也行。

#include
#define num 100
#include
using namespace std;

//节点个数,边数
int N,M;
//bool isin[num];
//入度,出度,每个节点需要花费时间,节点之间关系矩阵
int indgree[num];
int outdegree[num];
int cost[num];
int info[num][num];
//初始化,
void init(){
	int i,j;
	for(i=0;iy?x:y;
}
//主方法
int solve(){
	int n=0;
	int i,front;
	queue Q;
	for(i=0;i=0){//如果要抹掉的节点与某个节点有一条有向边,
				//该边入度减一,计算该边的cost
				indgree[i]--;
				cost[i]=max(cost[i],cost[front]+info[front][i]);
			}
		}
		//重新寻找需要入队的
		for(i=0;i




你可能感兴趣的:(算法OJ)