5-12 How Long Does It Take (25分)

来源:互联网 发布:mac顶端半透明 编辑:程序博客网 时间:2024/05/15 03:10

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 N1), 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], whereS[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 120 1 60 2 40 3 51 4 12 4 13 5 25 4 04 6 94 7 75 7 46 8 27 8 4

Sample Output 1:

18

Sample Input 2:

4 50 1 10 2 22 1 31 3 43 2 5

Sample Output 2:

Impossible
 
  • 时间限制:400ms
  • 内存限制:64MB
  • 代码长度限制:16kB
  • 判题程序:系统默认
  • 作者:陈越
  • 单位:浙江大学

题目判定

http://pta.patest.cn/pta/test/16/exam/4/question/674

// 5-12 拓扑排序 关键路径#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <string>#include <vector>#include <algorithm>#include <sstream>using namespace std ;#define INF 999999#define N 110int n , m ;struct edge{int v ;int l ;};vector<edge> q[N] ;int in_degree[N];int early[N] ;bool tuopu(){int i ;int start = -1;for(i = 0 ; i < n ; i++){if(in_degree[i] == 0){start = i ;break;}}if(start == -1)return false ;early[start] = 0 ;int sums = 0 ;int num = 0 ;int newP = start ;while(num != n-1 ){int len = q[newP].size() ;for(int j = 0 ; j < len ;  j ++){int v = q[newP][j].v ;int l = q[newP][j].l ;in_degree[v] -- ;if(early[newP] + l > early[v]){early[v] = early[newP] + l ;} }in_degree[newP] = -1 ; // 删除该节点newP = -1 ; // 找下一个入度为0的节点点for(i = 0 ; i < n ; i++){if(in_degree[i] == 0){newP = i ;break;}}if(newP == -1)return false ;if(early[newP] > sums)sums = early[newP] ;num ++ ;}printf("%d\n",sums);return true ;}int main(){//freopen("in.txt","r",stdin);int i ;while( scanf("%d%d" , &n , &m) != EOF) {int s , e , l ;for(i = 0 ; i < n ; i++){in_degree[i] = 0 ;early[i] = 0 ;q[i].clear() ;}edge et ;for(i = 0 ; i < m ; i++){scanf("%d%d%d" , &s , &e , &l) ;et.v = e ;et.l = l ;q[s].push_back(et) ;in_degree[e] ++ ;}if(!tuopu() ){printf("Impossible\n");}}return 0 ;}


0 0
原创粉丝点击