图论-拓扑排序应用-7-12 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,所有的节点再另外指向另一个超级节点(结束节点),从开始节点进行求拓扑排序,输出结束节点的权值。
③判断(当前节点最早开始时间+持续的时间)和其邻接点的最早开始开始时间哪个大,若(当前节点最早开始时间+持续的时间)大于其邻接点的最早开始开始时间则更新其邻接点的最早开始时间为(当前节点最早开始时间+持续的时间).当前的节点的最早开始时间必须为他的前驱结点最早开始时间最大的那个,保证所有的前驱结点可以执行完。

Code:

#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
#include<numeric>
using namespace std;
struct T{
	int next;
	int dur;
};
vector<vector<T>>g;//邻接表存储节点与边的信息 
vector<int>est;//用来记录最早开始时间 
vector<int>f,s;//用来存储各个节点的发现时间、结束时间 
vector<int>Path;//记录拓扑排序序列 
int N,M;
int flagCycle=0,cnt=1,ts=0;//标志是否存在回路  统计拓扑序列元素个数  时间计数器 
void TopSort(x){
	s[x]=++ts;//更新X节点的发现时间 
	for(int i=0;i<g[x].size();i++){
		if(s[g[x][i].next]==0)//判断x的邻接点g[x][i].next是否被访问过 若未被访问 则进行访问 
			TopSort(g[x][i].next);
		else if(f[g[x][i].next]==0&&s[x]>s[g[x][i].next]){//用于判断是否存在回路 
		//g[x][i].next节点被访问过未结束 g[x][i].next的开始时间大于当前x接节点 说明g[x][i].next节点为x节点的祖先节点 所以存在回路 
			flagCycle=1;//存在回路 
			return;//存在回路直接跳出 
		}
	}
	Path.push_back(x);//将节点加入拓扑序列中 
	f[x]=ts++;//更新x的结束时间 
	
}
void criticalAc(int x){//更新x的最早结束时间  必须保证其前驱结点都必须完成 
	for(int i=0;i<g[x].size();i++){
		if(est[x]+g[x][i].dur>est[g[x][i].next]){//g[x][i].next最早开始时间必须是其前驱所有节点最大的值 
			est[g[x][i].next]=est[x]+g[x][i].dur;
		}
	}
}
int main(){
	cin>>N>>M;
	//设置超级节点(开始节点)为N 设置超级节点(结束节点为N+1) 所以设置vector的size大小为N+2 
	g.resize(N+2);
	est.resize(N+2);
	s.resize(N+2);
	f.resize(N+2);	
	for(int i=0;i<M;i++){
		int a,b,c;
		cin>>a>>b>>c;
		g[a].push_back({b,c});
		g[N].push_back({a,0});//将超级开始节点连接所有节点 
		g[b].push_back({N+1,0});//将所有节点连接超级结束节点 
	}
	TopSort(N);//从超级开始节点开始拓扑排序 
	if(flagCycle)
		cout<<"Impossible";
	else{
		for(int i=N+1;i>=0;i--)
			criticalAc(Path[i]);
		cout<<est[N+1];
	}
	
} 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值