题目大意
有一些项目,每个项目有完成的时间。
然后一个任务要在一些任务完成之后才可以开始。
问你最短完成所有任务的时间。
其中,可以同时完成多个可以被完成的任务。
解
拓扑排序模板…
代码
#include<cstdio>
#include<queue>
#include<iostream>
#include<cmath>
using namespace std;
queue<int>Q;
int n, ti[233], pd, r[233], l[233], t, timee[233], finishh, k, ans;
struct asdf{
int to, next;
} a[100001];
int main(){
freopen("project.in","r",stdin);
freopen("project.out","w",stdout);
scanf("%d", &n);
for(int i = 1; i <= n; ++i)
scanf("%d", &ti[i]);
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j)
if(i != j){
scanf("%d", &pd);
if(pd == 1){
++r[i]; //入度
a[++t] = (asdf){
i, l[j]}; l[j] = t; //建边
}
}
}
for(int i = 1; i <= n; ++i){
if(r[i] == 0){
//入度为0,没有先决条件
Q.push(i);
timee[i] = ti[i];
ans = max(ans, timee[i]); //有人没加这一条WA了一个点
++finishh;
}
}
while(Q.size()){
k = Q.front();
Q.pop();
for(int i = l[k]; i; i = a[i].next){
--r[a[i].to];
timee[a[i].to] = max(timee[a[i].to], timee[k] + ti[a[i].to]); //统计用时
ans = max(ans, timee[a[i].to]);
if(r[a[i].to] == 0){
//如果先决条件都完成了,那么这个也可以开始做了
Q.push(a[i].to);
++finishh; //能完成的工程数量+1
if(finishh == n) break;
}
}
if(finishh == n) break;
}
if(finishh == n) printf("%d", ans);
else printf("-1");
fclose(stdin);
fclose(stdout);
}