[Usaco2006 Dec]Wormholes 虫洞
来源:互联网 发布:java中cgi是什么 编辑:程序博客网 时间:2024/06/07 09:39
Description
John在他的农场中闲逛时发现了许多虫洞。虫洞可以看作一条十分奇特的有向边,并可以使你返回到过去的一个时刻(相对你进入虫洞之前)。John的每个农场有
Input
Line 1: 一个整数
Line 1 of each farm: 三个整数
Lines
Lines
Output
Lines
Sample Input
2
3 3 1
1 2 2
1 3 4
2 3 1
3 1 3
3 2 1
1 2 3
2 3 4
3 1 8
Sample Output
NO
YES
HINT
Source
Gold
思路
其实就是问图中有没有负环,spfa找负环就好了。
代码
#include <cstdio>#include <algorithm>#include <cstring>const int maxn=5000;const int maxm=25000;int pre[(maxm<<1)+10],now[maxn+10],son[(maxm<<1)+10],tot,val[(maxm<<1)+10];int dist[maxn+10],sum[maxn+10],head,tail,q[(maxn<<8)+10],b[maxn+10];int n,m,w,t;int ins(int a,int b,int c){ tot++; pre[tot]=now[a]; now[a]=tot; son[tot]=b; val[tot]=c; return 0;}int spfa(int s){ memset(dist,63,sizeof dist); memset(sum,0,sizeof sum); head=0; tail=1; dist[s]=0; q[1]=s; b[s]=1; while(head!=tail) { head++; int u=q[head],j=now[u]; sum[u]++; if(sum[u]>=n) { return 1; } while(j) { int v=son[j]; if(dist[v]>dist[u]+val[j]) { dist[v]=dist[u]+val[j]; if(!b[v]) { b[v]=1; tail++; q[tail]=v; } } j=pre[j]; } b[u]=0; } return 0;}int solve(){ scanf("%d%d%d",&n,&m,&w); while(m--) { int a,b,c; scanf("%d%d%d",&a,&b,&c); ins(a,b,c); ins(b,a,c); } while(w--) { int a,b,c; scanf("%d%d%d",&a,&b,&c); ins(a,b,-c); } int s=0; for(int i=1; i<=n; i++) { if(!b[i]) { s+=spfa(i); } if(s) { break; } } if(s) { puts("YES"); } else { puts("NO"); } return 0;}int main(){ scanf("%d",&t); while(t--) { memset(now,0,sizeof now); memset(b,0,sizeof b); tot=0; solve(); } return 0;}
阅读全文
0 0
- [Usaco2006 Dec]Wormholes 虫洞
- bzoj1715[Usaco2006 Dec]Wormholes 虫洞
- [BZOJ1715][Usaco2006 Dec]Wormholes 虫洞
- bzoj 1715: [Usaco2006 Dec]Wormholes 虫洞
- BZOJ 1715: [Usaco2006 Dec]Wormholes 虫洞
- BZOJ1715: [Usaco2006 Dec]Wormholes 虫洞 spfa 求负环
- bzoj1715 [Usaco2006 Dec]Wormholes 虫洞 spfa
- 【BZOJ 1715】 [Usaco2006 Dec]Wormholes 虫洞
- BZOJ 1715 [Usaco2006 Dec]Wormholes 虫洞 SPFA
- bzoj1715 [Usaco2006 Dec]Wormholes 虫洞(spfa判负环)
- [Usaco2006 Dec]Milk Patterns
- bzoj 1717: [Usaco2006 Dec] Milk Patterns
- bzoj1648 [Usaco2006 Dec]Cow Picnic 奶牛野餐
- bzoj1649 [Usaco2006 Dec]Cow Roller Coaster
- bzoj1650 [Usaco2006 Dec]River Hopscotch 跳石子
- BZOJ 1649: [Usaco2006 Dec]Cow Roller Coaster
- bzoj1650: [Usaco2006 Dec]River Hopscotch 跳石子
- bzoj1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐
- html之jQuery .end()妙用
- JNI编程
- 2017-10-23 & 2017-10-24 集训总结
- 《深入理解java虚拟机》学习笔记6——类加载机制
- 机器学习作业5
- [Usaco2006 Dec]Wormholes 虫洞
- matlab多行注释
- 安卓的历史
- kafka(2)--多节点
- Uva 725 Division (技巧暴力)
- Warning[Pa082]: undefined behavior: the order of volatile accesses is undefined in this statement
- 1212: [视频]【计算几何】判断线段相交(跨立实验)
- 有没有会做c++计算器的
- 登录注册并记住状态