Topic :
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 4Sample Output 1:
18Sample Input 2:
4 5 0 1 1 0 2 2 2 1 3 1 3 4 3 2 5Sample Output 2:
Impossible
Random analysis:
A very typical topological sorting, first put all the points whose in-degree is 0 into the list, enter the loop, pop up a node at a time, find the node connected to this node, reduce the in-degree by 1, update the minimum cost, and then put all the Nodes with an in-degree of 0 enter the queue and enter the next cycle. Added a cnt to judge whether there is a ring.
, the code:
#include <iostream> #include <algorithm> #include <queue> #define MAXN 105 #define INF 1e7 using namespace std; int Graph[MAXN][MAXN];//Matrix Graph[i][j] represents the cost of i->j void TopSort(int N) { int cnt = 0; //Counter determines whether there is a loop int* Earliest = new int[N]; //The time spent by the node int * Indegree = new int [N]; // Int for (int i = 0; i < N; i++) { Indegree[i] = 0; Earliest[i] = 0; for (int j = 0; j < N; j++) { //initialize in-degree if (Graph[j][i] != INF) { Indegree[i]++; } } } queue<int> Q; for (int i = 0; i < N; i++) { //The node whose in-degree is 0 is initially judged and placed in the queue (in fact, the stack can also be used here if (Indegree[i] == 0) { Q.push(i); Indegree[i] = -1; //Mark node i to avoid repeated enqueue Earliest[i] = 0; } } while (!Q.empty()) { int temp = Q.front(); Q.pop(); cnt++; //Processed a node cnt++ int longest = 0; for (int i = 0; i < N; i++) { //traverse all nodes if (Graph[temp][i] != INF) { //if there is an edge <temp,i> Indegree[i]--; //Indegree of i minus 1 Earliest[i] = max(Earliest[i], Earliest[temp] + Graph[temp][i]);//Update the longest cost of node i } } for (int i = 0; i < N; i++) {//Loop again to determine whether there is a node with an in-degree of 0, if there is, put it in the queue if (Indegree[i] == 0) { Q.push(i); Indegree[i] = -1; //Mark nodes to avoid repeated enqueue } } } sort(Earliest, Earliest + N); // Sort the duration of all nodes from small to large, and also handle the case of multiple endpoints if (cnt == N) { //If N nodes are processed cout << Earliest[N - 1]; //Output the longest time spent } else { cout << "Impossible"; //Otherwise it means there is a cycle in the graph } } int main() { int N, M, v1, v2, cost;//The number of nodes N and the number of edges M cin >> N >> M; for (int i = 0; i < N; i++) { //Initialize the entire image INF for (int j = 0; j < N; j++) { Graph[i][j] = INF; } } for (int i = 0; i < M; i++) { //输入<v1,v2>的cost cin >> v1 >> v2 >> cost; Graph[v1][v2] = cost; } TopSort(N); }