一种路是双向的,路的长度是正值;另一种路是单向的,路的长度是负值;  如果有负环输出YES;否则输出NO;不同的路可能有相同的起点和终点:必须用邻接表

While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the wormhole! Each of FJ's farms comprises N (1 ≤ N ≤ 500) fields conveniently numbered 1..NM (1 ≤ M ≤ 2500) paths, and W (1 ≤ W ≤ 200) wormholes.

As FJ is an avid time-traveling fan, he wants to do the following: start at some field, travel through some paths and wormholes, and return to the starting field a time before his initial departure. Perhaps he will be able to meet himself :) .

To help FJ find out whether this is possible or not, he will supply you with complete maps to F (1 ≤ F ≤ 5) of his farms. No paths will take longer than 10,000 seconds to travel and no wormhole can bring FJ back in time by more than 10,000 seconds.

Input

Line 1: A single integer, FF farm descriptions follow. 
Line 1 of each farm: Three space-separated integers respectively: NM, and W 
Lines 2..M+1 of each farm: Three space-separated numbers (SET) that describe, respectively: a bidirectional path between S and E that requires T seconds to traverse. Two fields might be connected by more than one path. 
Lines M+2..M+W+1 of each farm: Three space-separated numbers (SET) that describe, respectively: A one way path from S to E that also moves the traveler back T seconds.

Output

Lines 1..F: For each farm, output "YES" if FJ can achieve his goal, otherwise output "NO" (do not include the quotes).

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

For farm 1, FJ cannot travel back in time. 
For farm 2, FJ could travel back in time by the cycle 1->2->3->1, arriving back at his starting location 1 second before he leaves. He could start from anywhere on the cycle to accomplish this.

Sponsor

 1 #include<iostream>
2 #include<cstdio>
3 #include<cstdlib>
4 #include<cstring>
5 #include<algorithm>
6 #include<cmath>
7 #include<queue>
8 #include<vector>
9 #include<map>
10 #include<string>
11 #define LL long long
12 #define eps 1e-8
13 using namespace std;
14 const int inf = 0x3f3f3f3f;
15 int n,m,w,a,b,c,t,tot,ans;
16 struct node{
17 int l,r,num,nex;
18 }e[10010];
19 int head[550],vis[550],dis[550],sum[550];
20
21 void init()
22 {
23
24 memset(head,-1,sizeof(head));
25 tot=0;
26 ans=0;
27 }
28
29 void add(int l,int r,int num)
30 {
31 e[tot].l=l,e[tot].r=r,e[tot].num=num;
32 e[tot].nex=head[l];
33 head[l]=tot++;
34 }
35
36 int spfa(int x)
37 {
38 queue<int >q;
39 while(!q.empty())
40 q.pop();
41 memset(dis,inf,sizeof(dis));
42 memset(vis,0,sizeof(vis));
43 memset(sum,0,sizeof(sum));
44 q.push(x);
45 vis[x]=1;
46 dis[x]=0;
47 sum[x]=1;
48 while(!q.empty())
49 {
50 int now=q.front(); q.pop();
51 vis[now]=0;
52 for(int i=head[now];i!=-1;i=e[i].nex)
53 {
54 int to=e[i].r;
55 if(dis[to]>dis[now]+e[i].num)
56 {
57 dis[to]=dis[now]+e[i].num;
58 if(!vis[to])
59 {
60 sum[to]++;
61 if(sum[to]>n) return 1;
62 q.push(to);
63 vis[to]=1;
64 }
65 }
66 }
67 }
68 return 0;
69 }
70 int main()
71 {
72 scanf("%d",&t);
73 while(t--)
74 {
75 scanf("%d%d%d",&n,&m,&w);
76 init();
77 while(m--)
78 {
79 scanf("%d%d%d",&a,&b,&c);
80 add(a,b,c);
81 add(b,a,c);
82 }
83 while(w--)
84 {
85 scanf("%d%d%d",&a,&b,&c);
86 add(a,b,-c);
87 }
88 for(int i=1;i<=n;i++)
89 {
90 if(spfa(i)){
91 ans=1;
92 break;//否则超时
93 }
94 }
95 if(ans) printf("YES\n");
96 else printf("NO\n");
97 }
98 }

Wormholes (spfa)的更多相关文章

  1. ACM: POJ 3259 Wormholes - SPFA负环判定

     POJ 3259 Wormholes Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu   ...

  2. poj 3259 Wormholes spfa算法

    点击打开链接 Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25582   Accepted: 9186 ...

  3. Wormholes(SPFA+Bellman)

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36860   Accepted: 13505 Descr ...

  4. POJ 1151 Wormholes spfa+反向建边+负环判断+链式前向星

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 49962   Accepted: 18421 Descr ...

  5. POJ3259 :Wormholes(SPFA判负环)

    POJ3259 :Wormholes 时间限制:2000MS 内存限制:65536KByte 64位IO格式:%I64d & %I64u 描述 While exploring his many ...

  6. POJ3259 Wormholes(SPFA判断负环)

    Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...

  7. POJ3259 Wormholes —— spfa求负环

    题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submis ...

  8. uva558 Wormholes SPFA 求是否存在负环

    J - Wormholes Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Stat ...

  9. POJ 3259 Wormholes(SPFA判负环)

    题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是 ...

随机推荐

  1. CS系统中分页控件的制作

    需求:在一个已有的CS项目(ERP中),给所有的列表加上分页功能. 分页的几个概念: 总记录数  totalCount (只有知道了总记录数,才知道有多少页) 每页记录数  pageSize (根据总 ...

  2. label_form

    表单: action "URL" 如果为空,则本form接收 指定接收方 disabled 指定该标签是否可用 method "net" "http& ...

  3. C语言的类型大小

    C语言的类型大小 设计程序的时候我们一般会考虑的尽量的周全,尤其是像C这样的静态类型语言. 有一些溢出的问题就源于没有搞清楚变量的大小范围,所以我们编写的时候需要特别注意 C的整形(整数类型)大小 C ...

  4. ctfhub技能树—文件上传—无验证

    打开靶机 查看页面信息 编写一句话木马 <?php echo "123"; @eval(@$_POST['a']); ?> 上传木马 上传成功,并拿到相对路径地址 查看 ...

  5. ctfhub技能树—彩蛋

    彩蛋题建议大家首先自己动手去找一找 做 好 准 备 后 再 看 下 文 !           1.首页 使用域名查询工具查询子域名 2.公众号 此题关注ctfhub公众号即可拿到,不过多赘述. 3. ...

  6. 【一天一个知识点系列】- Redis Cluser之数据分布

    数据分布 简述 分布式数据库首先要解决把整个数据集按照分区规则映射到多个节点的问题,即把数据集划分到多个节点上,每个节点负责整体数据的一个子集 分区及限制 分区规则 常见的分区规则 顺序分区 哈希分区 ...

  7. M8 E147 不可能为条目*确立账户

    今天用BAPI做发票校验, BAPI_INCOMINGINVOICE_CREATE这个函数使用都正常,可是突然就无法做发票检验了 报了个错误,"不可能为条目BOXT TR 确立账户" ...

  8. BAPI_GOODSMVT_CREATE的参数GOODSMVT_CODE的说明

    BAPI_GOODSMVT_CREATE 的功能就是用于货物移动,其主要可以实现MB*事物的一些功能,其中该BAPI的参数 GOODSMVT_CODE就控制了对应哪个事物码的功能,下面给出该参数的值和 ...

  9. 入门OJ:郭嘉的消息传递

    题目描述 我们的郭嘉大大在曹操这过得逍遥自在,但是有一天曹操给了他一个任务,在建邺城内有N(<=1000)个袁绍的奸细 将他们从1到N进行编号,同时他们之间存在一种传递关系,即若C[i,j]=1 ...

  10. mastercam2018安装教程

    安装前先关闭杀毒软件和360卫士,注意安装路径不能有中文,安装包路径也不要有中文. [安装环境]:Win7/Win8/Win10 1.选中[Mastercam2018]压缩包,鼠标右击选择[解压到Ma ...