PTA 08-图8 How Long Does It Take (25分)
题目地址
https://pta.patest.cn/pta/test/16/exam/4/question/674
5-12 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 NN (\le 100≤100), the number of activity check points (hence it is assumed that the check points are numbered from 0 to N-1N−1), and MM, the number of activities. Then MM 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
/* 评测结果 时间 结果 得分 题目 编译器 用时(ms) 内存(MB) 用户 2017-07-05 14:42 答案正确 25 5-12 gcc 18 1 测试点结果 测试点 结果 得分/满分 用时(ms) 内存(MB) 测试点1 答案正确 15/15 2 1 测试点2 答案正确 2/2 2 1 测试点3 答案正确 4/4 2 1 测试点4 答案正确 2/2 2 1 测试点5 答案正确 2/2 18 1 简单的拓扑排序,注意处理多起点和多终点的问题 */ #include<stdio.h> #define MAXN 100 #define TRUE 1 #define FALSE 0 #define ERROR -1 struct checkpoint{ int minStratTime; int inEdgeCount; int finished; }gCheckpointTable[MAXN]; int gMatrix[MAXN][MAXN]; void InitCheckpointTable() { int i; for(i=0;i<MAXN;i++) { gCheckpointTable[i].minStratTime=0; gCheckpointTable[i].inEdgeCount=0; gCheckpointTable[i].finished=0; } } void InitMatrix(N) { int i,j; for(i=0;i<N;i++) for(j=0;j<N;j++) gMatrix[i][j]=ERROR; } int FindPoint(N) //返回一个入度为0并且没被访问过的点 { int i; int p=ERROR; for(i=0;i<N;i++) { if(gCheckpointTable[i].inEdgeCount==FALSE && gCheckpointTable[i].finished==FALSE) p=i; } return p; } void Calc(int N) { int i,maxtime,p; while( (p=FindPoint(N)) != ERROR ) //如果还能返回入度为0的点 先叫它v1 { gCheckpointTable[p].finished=TRUE; //先设为处理完成 for(i=0;i<N;i++)//遍历该点所有发出去的边,找一个连接到的点v2 { if(gMatrix[p][i]==ERROR) continue; //如果被指向的结点v2,最小完成时间小于此节点v1的最小完成时间 加上 v1到v2的耗时,那么更新v2的最小时间 if(gCheckpointTable[i].minStratTime<gCheckpointTable[p].minStratTime+gMatrix[p][i]) gCheckpointTable[i].minStratTime = gCheckpointTable[p].minStratTime + gMatrix[p][i]; gCheckpointTable[i].inEdgeCount--; //将v2的入度减一 } } maxtime=0; for(i=0;i<N;i++) //考虑到有多终点问题,算完后把所有节点全扫一遍,挑一个结束时间最大的打出来 { if(gCheckpointTable[i].finished==FALSE) { printf("Impossible"); return; } if(gCheckpointTable[i].minStratTime>maxtime) maxtime=gCheckpointTable[i].minStratTime; } printf("%d",maxtime); } int main() { int i; int N,M; int v1,v2,lastingTime; scanf("%d %d",&N,&M); InitMatrix(N); InitCheckpointTable(); for(i=0;i<M;i++) { scanf("%d %d %d",&v1,&v2,&lastingTime); gMatrix[v1][v2]=lastingTime; gCheckpointTable[v2].inEdgeCount++; } Calc(N); }
posted on 2017-07-09 12:12 gravitykey 阅读(650) 评论(0) 编辑 收藏 举报