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
具体思路
就是很普通的关键路径问题,拓扑排序
具体代码
#include<stdio.h>
#include<stdlib.h>
#define MAX 1000
#define BKD 65535
#define ERROR -1
#define false -1
#define true 1
typedef struct node *Post;
struct node
{
int ding;
int bian;
int m[MAX][MAX];
};
typedef Post Mgraph;/*邻接矩阵储存图*/
int Rudu[MAX];
int dist[MAX];
int Chudu[MAX];
Mgraph JIANtu()/*建图*/
{
Mgraph M;
M=(Mgraph)malloc(sizeof(struct node));
int b,d,i,j,zh,a;
scanf("%d",&M->ding);
for( i=0;i<M->ding;i++)
for( j=0;j<M->ding;j++)
{
M->m[i][j]=-1;
Rudu[i]=0;
Chudu[i]=0;
}
scanf("%d",&M->bian);
for(i=0;i<M->bian;i++)
{
scanf("%d %d %d",&b,&d,&zh);
M->m[b][d]=zh;
Rudu[d]++;/*每个点的出入度计算上*/
Chudu[b]++;
}
return M;
}
void Guanjian(Mgraph M)
{
int co[MAX];
int as[MAX];
int i,j;
int k,l;
l=0;
int sum;
int tou,wei;
int cou=0;
tou=wei=0;
for(i=0;i<M->ding;i++)
{
if(Rudu[i]==0)
{
co[tou++]=i;/*入度为0的点,进队列(我用的数组模仿队列的)*/
dist[i]=0;
}
if(Chudu[i]==0)
as[l++]=i;/*出度为0的也记住,都是终点这些点*/
}
while(tou!=wei)/*队列不为空*/
{
j=co[wei++];/*出队*/
for(i=0;i<M->ding;i++)
{
if(dist[i]<M->m[j][i])
dist[i]=M->m[j][i];/*对这个dist数组初始化,如果值小于图中的边,就替换(解决多个起点的)*/
}
cou++;/*计数器*/
for(i=0;i<M->ding;i++)
if((M->m[j][i]!=-1))/*再将除去这个顶点后,入度为0的顶点加入队列*/
{
Rudu[i]--;
if(Rudu[i]==0)
co[tou++]=i;
}
for(i=0;i<M->ding;i++)
{
if(M->m[j][i]>-1)
if(dist[j]+M->m[j][i]>dist[i])/*关键路径,事到这个顶点值最大的*/
dist[i]=dist[j]+M->m[j][i];
}
}
if(cou==M->ding)/*是否所有的顶点都经历过*/
{
sum=dist[as[0]];/*多个终点中,找出最大的终点的关键路径*/
for(i=0;i<l;i++)
{
if(sum<dist[as[i]])
sum=dist[as[i]];
}
printf("%d",sum);
}
else
printf("Impossible");
}
int main()
{
Mgraph M;
M=JIANtu();
Guanjian(M);
return 0;
}
具体结果
为啥我写c,就没人看啊,我写的python就是垃圾中的垃圾还有人看,我写的c就没人看,代码有错请指出,一起进步。