AOV(Activity On Vertex)网络
/* 邻接表存储 - 拓扑排序算法 */
bool TopSort( LGraph Graph, Vertex TopOrder[] )
{ /* 对Graph进行拓扑排序, TopOrder[]顺序存储排序后的顶点下标 */
int Indegree[MaxVertexNum], cnt;
Vertex V;
PtrToAdjVNode W;
Queue Q = CreateQueue( Graph->Nv );
/* 初始化Indegree[] */
for (V=0; V<Graph->Nv; V++)
Indegree[V] = 0;
/* 遍历图,得到Indegree[] */
for (V=0; V<Graph->Nv; V++)
for (W=Graph->G[V].FirstEdge; W; W=W->Next)
Indegree[W->AdjV]++; /* 对有向边<V, W->AdjV>累计终点的入度 */
/* 将所有入度为0的顶点入列 */
for (V=0; V<Graph->Nv; V++)
if ( Indegree[V]==0 )
AddQ(Q, V);
/* 下面进入拓扑排序 */
cnt = 0;
while( !IsEmpty(Q) ){
V = DeleteQ(Q); /* 弹出一个入度为0的顶点 */
TopOrder[cnt++] = V; /* 将之存为结果序列的下一个元素 */
/* 对V的每个邻接点W->AdjV */
for ( W=Graph->G[V].FirstEdge; W; W=W->Next )
if ( --Indegree[W->AdjV] == 0 )/* 若删除V使得W->AdjV入度为0 */
AddQ(Q, W->AdjV); /* 则该顶点入列 */
} /* while结束*/
if ( cnt != Graph->Nv )
return false; /* 说明图中有回路, 返回不成功标志 */
else
return true;
}
关于问题1:
题目链接:https://pta.patest.cn/pta/test/16/exam/4/question/674
我遇到的坑点:
一开始以为最后一个活动都是最后一个节点,然后提交只有部分对;实际上并不,所以还需要一个变量sum来实现。关键的,我都注释了。自己看吧。
/**
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
*/
#include<cstdio>
#include<queue>
using namespace std;
#define MAXN 101
int pic[MAXN][MAXN];//图
int Indegree[MAXN]; //边集
int Vertex[MAXN]; //顶点集
int flag[MAXN]; //用于检验是否添加过
int N; //顶点数
int M; //活动数
queue<int> que;
void TopSort(){
int V = 0; //临时顶点
int cnt = 0;
int sum = 0; //用于计算总和
for(int i = 0; i < N; ++i){
if(Indegree[i] == 0 && flag[i] == 0){
que.push(i);
flag[i] = 1;
}
while(!que.empty()){
V = que.front();
que.pop();
cnt++;
for(int j = 0 ; j < N; ++j){
if(pic[V][j] != -1){ //有邻边
Vertex[j] = max(Vertex[j], Vertex[V] + pic[V][j]);
sum = sum > Vertex[j] ? sum : Vertex[j]; //!!!重要
if(--Indegree[j] == 0 && flag[j] == 0){
que.push(j);
flag[j] = 1;
}
}
}
}
}
if(cnt != N)
printf("Impossible");
else
printf("%d", sum);
}
int main(void){
int x, y, temp;
scanf("%d%d", &N, &M);
for(int i = 0; i < N; ++i)
for(int j = 0; j < N; ++j)
pic[i][j] = -1;
for(int i = 0; i < M; ++i){
scanf("%d%d%d", &x, &y, &temp);
pic[x][y] = temp;
Indegree[y]++;
}
// for(int i = 0; i < N; ++i){
// for(int j = 0; j < N; ++j){
// printf("%d ", pic[i][j]);
// }
// printf("\n");
// }
TopSort();
return 0;
}