08-圖8 How Long Does It Take (25 分)(拓撲排序) 中國大學MOOC-陳越、何欽銘-資料結構-2018秋
阿新 • • 發佈:2018-11-21
08-圖8 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
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
思路:一道挺典型的拓撲排序,先把所有入度為0的點入列,進入迴圈,每次彈出一個節點,找出與這個節點相連的節點,入度減1,更新最小花費時間,再把所有入度為0的節點入列,進入下一次迴圈。加了個cnt判斷有沒有環。
#include <iostream>
#include <algorithm>
#include <queue>
#define MAXN 105
#define INF 1e7
using namespace std;
int Graph[MAXN][MAXN];//矩陣Graph[i][j]代表i->j的cost
void TopSort(int N) {
int cnt = 0;
//計數器判斷有無環路
int* Earliest = new int[N]; //節點的時間花費
int* Indegree = new int[N]; //入度
for (int i = 0; i < N; i++) {
Indegree[i] = 0;
Earliest[i] = 0;
for (int j = 0; j < N; j++) { //初始化 入度
if (Graph[j][i] != INF) {
Indegree[i]++;
}
}
}
queue<int> Q;
for (int i = 0; i < N; i++) { //起始判斷入度為0的節點,放入佇列(其實這裡用堆疊也可以
if (Indegree[i] == 0) {
Q.push(i);
Indegree[i] = -1; //標記節點i避免重複入隊
Earliest[i] = 0;
}
}
while (!Q.empty()) {
int temp = Q.front();
Q.pop();
cnt++; //處理了一個節點cnt++
int longest = 0;
for (int i = 0; i < N; i++) { //遍歷所有節點
if (Graph[temp][i] != INF) { //如果有邊<temp,i>
Indegree[i]--; //i的的入度減1
Earliest[i] = max(Earliest[i], Earliest[temp] + Graph[temp][i]);//節點i的最長花費進行更新
}
}
for (int i = 0; i < N; i++) {//再次迴圈判斷有沒有入度為0的節點,如果有就放入佇列
if (Indegree[i] == 0) {
Q.push(i);
Indegree[i] = -1; //標記節點避免重複入隊
}
}
}
sort(Earliest, Earliest + N);
//對所有節點的時長花費進行從小到大的排序,也處理了有多個終點的情況
if (cnt == N) { //如果處理了N個節點
cout << Earliest[N - 1]; //輸出最長的時間花費
}
else {
cout << "Impossible"; //否則代表圖中有環
}
}
int main() {
int N, M, v1, v2, cost;//節點個數N 邊數M
cin >> N >> M;
for (int i = 0; i < N; i++) { //初始化整張圖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);
}