P1113 杂务 拓扑

 

#include <bits/stdc++.h>
#define ll long long 
using namespace std;
const int maxn = 100000000;
int head[maxn];
int cnt;
int val[20005],in[20005];
bool book[20005];
int ans[20005];
int n,as;
struct N{
	int v,nxt;	
}edge[maxn];
void add(int u,int v){
	cnt++;
	edge[cnt].v = v;
	edge[cnt].nxt = head[u];
	head[u] = cnt;
}
queue<int> q;
void topo(){
	int v;
	while(!q.empty()){
		int top = q.front();
		q.pop();	book[top] = false;
		for(int i = head[top];i;i = edge[i].nxt){
			v = edge[i].v;
			in[v]--;
			ans[v] = max(ans[v],val[v]+ans[top]);
			as = max(as,ans[v]);
			if(in[v]==0&&(!book[v])){
				
				q.push(v);	book[v] = true;
			}	
		}
	}
}
int main()
{
	int id,w,last;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d %d",&id,&w);
		val[id] = w;
		while(scanf("%d",&last)&&last){
			add(last,id);
			in[id]++;
		}
	}
	for(int i=1;i<=n;i++){
		if(!in[i])q.push(i),book[i]=true;
		ans[i] = val[i];
	}
	topo();
	printf("%d\n",as);
	return 0;
}  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wym_king

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值