http://poj.org/problem?id=3259

Description

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.
 

题目大意:虫洞问题,现在有n个点,m条边,代表现在可以走的通路,比如从a到b和从b到a需要花费c时间,现在在地上出现了w个虫洞,虫洞的意义就是你从a到b话费的时间是-c(时间倒流,并且虫洞是单向的),现在问你从某个点开始走,能回到从前

解题思路:其实给出了坐标,这个时候就可以构成一张图,然后将回到从前理解为是否会出现负权环,用bellman-ford就可以解出了

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 900001
struct node
{
int u,v,w;
}q[];
int dis[];
int n,m,w1,count=;
int B()
{
int flag=;
for(int i=;i<=n;i++)
dis[i]=N;
dis[]=;
for(int i=;i<=n-;i++)
{
flag=;
for(int j=;j<count;j++)
{
if(dis[q[j].v]>dis[q[j].u]+q[j].w)//这里u,v是不能颠倒的,因为
{
dis[q[j].v]=dis[q[j].u]+q[j].w;
flag=;
}
}
/* for(int j=0;j<n;j++)
printf(".%d",dis[j]);*/
if(!flag) break;
}
for(int i=;i<count;i++)
{
if(dis[q[i].v]>dis[q[i].u]+q[i].w)
return ;
}
return ;
}
int main()
{
int x,y,x1,T;
scanf("%d",&T);
while(T--)
{
count=;
scanf("%d%d%d",&n,&m,&w1);
while(m--)
{
scanf("%d%d%d",&x,&y,&x1);
q[count].u=x;
q[count].v=y;
q[count++].w=x1;
q[count].u=y;
q[count].v=x;
q[count++].w=x1;
}
while(w1--)
{
scanf("%d%d%d",&x,&y,&x1);
q[count].u=x;
q[count].v=y;
q[count++].w=-x1;//他是有方向的
}
int t=B();
if(t==) printf("YES\n");
else printf("NO\n");
}
return ;
}

第二次写的:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define INF 0x7fffffff
using namespace std;
int n,m,k,tt;
struct node
{
int x,y,z;
} q[];
int dis[];
void add(int xx,int yy,int zz)
{
q[tt].x=xx;
q[tt].y=yy;
q[tt++].z=zz;
}
void BF()
{
int flag;
dis[]=;
for(int i=; i<=n; i++)
{
flag=;
for(int i=; i<tt; i++)
{
if(dis[q[i].y]>dis[q[i].x]+q[i].z)
{
dis[q[i].y]=dis[q[i].x]+q[i].z;
flag=;
}
}
if(flag==) break;
}
if(flag==) printf("YES\n");
else printf("NO\n");
}
int main()
{
int T,zz,xx,yy;
cin>>T;
while(T--)
{
cin>>n>>m>>k;
tt=;
for(int i=; i<m; i++)
{
cin>>xx>>yy>>zz;
add(xx,yy,zz);
add(yy,xx,zz);
}
for(int i=; i<k; i++)
{
cin>>xx>>yy>>zz;
add(xx,yy,-zz);
}
BF();
}
return ;
}

poj3259: Wormholes(BF模板题)的更多相关文章

  1. poj3259 Wormholes (判负环)【spfa】(模板)

    <题目链接> 题目大意: John的农场里N块地,M条路连接两块地,W个虫洞,虫洞是一条单向路,会在你离开之前把你传送到目的地,就是当你过去的时候时间会倒退Ts.我们的任务是知道会不会在从 ...

  2. POJ3259 Wormholes

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

  3. HDU 4347 - The Closest M Points - [KDTree模板题]

    本文参考: https://www.cnblogs.com/GerynOhenz/p/8727415.html kuangbin的ACM模板(新) 题目链接:http://acm.hdu.edu.cn ...

  4. [AHOI 2009] 维护序列(线段树模板题)

    1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec  Memory Limit: 64 MB Description 老师交给小可可一个维护数列的任务,现在小 ...

  5. HDU 2222 AC自动机模板题

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...

  6. POJ2774 & 后缀数组模板题

    题意: 求两个字符串的LCP SOL: 模板题.连一起搞一搞就好了...主要是记录一下做(sha)题(bi)过程心(cao)得(dan)体(xin)会(qing) 后缀数组概念...还算是简单的,过程 ...

  7. HDU 1251 Trie树模板题

    1.HDU 1251 统计难题  Trie树模板题,或者map 2.总结:用C++过了,G++就爆内存.. 题意:查找给定前缀的单词数量. #include<iostream> #incl ...

  8. HDU-3549 最大流模板题

    1.HDU-3549   Flow Problem 2.链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 3.总结:模板题,参考了 http://ww ...

  9. HDU 4280:Island Transport(ISAP模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=4280 题意:在最西边的点走到最东边的点最大容量. 思路:ISAP模板题,Dinic过不了. #include & ...

随机推荐

  1. python string.py 源码分析 一

    # Some strings for ctype-style character classification c风格字符串 whitespace = ' \t\n\r\v\f' #空白字符 \t 制 ...

  2. E - Train Problem I

    As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want to ge ...

  3. sg函数的理解

    sg,是用来判断博弈问题的输赢的,当sg值为0的时候,就是输,不为0就是赢: 在这之前,我们规定一个对于集合的操作mex,表示最小的不属于该集合的非负整数. 举几个栗子:mex{0,1,2}=3,me ...

  4. Code Labels

    Code Labels Code labels are three-letter codes with which commit messages can be prefixed. CODE Labe ...

  5. spark - Locality Level

    这几个值在图中代表 task 的计算节点和 task 的输入数据的节点位置关系 PROCESS_LOCAL: 数据在同一个 JVM 中,即同一个 executor 上.这是最佳数据 locality. ...

  6. thinkPHP框架 简单的删除和修改数据的做法 和 模板继承的意思大概做法

    BiaodanController.class.php控制器页面 <?php namespace Admin\Controller; use think\Controller; class Bi ...

  7. 好用的 Chrome 插件,提升你的学习工作效率

    Google Chrome 应该是大部分人都用的一款浏览器,但却有很少人会注意到它丰富的扩展插件,擅于使用这些插件,能让自己的工作效率大大提高,今天趁着周末休息,就不谈技术伤大家的脑细胞了,给大家分享 ...

  8. Codeforces 677D - Vanya and Treasure - [DP+优先队列BFS]

    题目链接:http://codeforces.com/problemset/problem/677/D 题意: 有 $n \times m$ 的网格,每个网格上有一个棋子,棋子种类为 $t[i][j] ...

  9. hdu 6390 欧拉函数+容斥(莫比乌斯函数) GuGuFishtion

    http://acm.hdu.edu.cn/showproblem.php?pid=6390 题意:求一个式子 题解:看题解,写代码 第一行就看不出来,后面的sigma公式也不会化简.mobius也不 ...

  10. iOS循环引用常见场景和解决办法

    好多场景会导致循环引用,例如使用Block.线程.委托.通知.观察者都可能会导致循环引用. 1.委托 遵守一个规则,委托方持有代理方的强引用,代理方持有委托方的弱引用. 实际场景中,委托方会是一个控制 ...