【DG特长生2017 T3】【SSL 2876】工程

工程

题目链接:SSL 2876

题目大意

有一些项目,每个项目有完成的时间。
然后一个任务要在一些任务完成之后才可以开始。
问你最短完成所有任务的时间。
可以同时完成多个可以被完成的任务。

思路

这道题很明显就是拓扑。

我们就直接跑,然后新出现的任务的开始时间是它前面要完成的任务的完成时间中最晚的那一个。

那你可以每次处理可以完成的点的时候枚举取 max ⁡ \max max,但是我这里直接用小根堆。
那你执行完这个任务解锁另一个任务的时候,下一个任务就从你执行完这个任务的时间开始。

然后就是这样了。

代码

#include<queue>
#include<cstdio>
#define ll long long

using namespace std;

struct node {
    
    
	int to, nxt;
}e[100001];
int n, le[201], KK;
int need[201][201], ru[201];
priority_queue <pair<ll, int>, vector<pair<ll, int> >, greater<pair<ll, int> > > q;
bool finish[201];
ll fin_n, t[201], ans;

void add(int x, int y) {
    
    
	e[++KK] = (node){
    
    y, le[x]}; le[x] = KK;
	ru[y]++;
}

void tuopu() {
    
    
	for (int i = 1; i <= n; i++)
		if (!ru[i]) {
    
    
			fin_n++;
			finish[i] = 1;
			ans = max(ans, t[i]);
			q.push(make_pair(t[i], i));
		}
	
	while (!q.empty()) {
    
    
		int now = q.top().second;
		ll nowtime = q.top().first;
		q.pop();
		for (int i = le[now]; i; i = e[i].nxt)
			if (!finish[e[i].to]) {
    
    
				ru[e[i].to]--;
				if (!ru[e[i].to]) {
    
    //这个任务也可以开始被完成了
					fin_n++;
					finish[e[i].to] = 1;
					ans = max(ans, nowtime + t[e[i].to]);
					q.push(make_pair(nowtime + t[e[i].to], e[i].to));
				}
			}
	}
	
	if (fin_n < n) printf("-1");
		else printf("%lld", ans);
}

int main() {
    
    
//	freopen("project.in", "r", stdin);
//	freopen("project.out", "w", stdout);
	
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
		scanf("%lld", &t[i]);
	
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++) {
    
    
			if (i == j) continue;
			scanf("%d", &need[i][j]);
			if (need[i][j])
				add(j, i);
		}
	
	tuopu();
	
	fclose(stdin);
	fclose(stdout);
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43346722/article/details/115414511