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
-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
思路:一道挺典型的拓扑排序,先把所有入度为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);
}