Wormholes
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 36889   Accepted: 13520

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

题意:就是给N组測试数据,给你N个点M条无向边,再给你W个虫洞(能够使时间倒流)。问你能不能看到刚出发时的自己。(就是推断存不存在负环)。

思路:套上SPFA公式然后推断负环就能够了。

#include<stdio.h>//好坑,wa了半天没过去,竟然是由于大写和小写问题。整个人都不好了。
#include<string.h>
#include<queue>
#define INF 0x3f3f3f3f
#define M 3000*2
#define N 550
using namespace std;
int n,m,w,dis[N],vis[N],used[N],head[N],edgenum;
struct node{
int from,to,cost,next;
}edge[M];
void init(){
edgenum=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v,int cost){
node E={u,v,cost,head[u]};
edge[edgenum]=E;
head[u]=edgenum++;
}
void spfa(){
queue<int>q;
memset(vis,0,sizeof(vis));
memset(dis,INF,sizeof(dis));
memset(used,0,sizeof(used));
dis[1]=0;
vis[1]=1;
q.push(1);
used[1]++;
while(!q.empty()){
int u=q.front();
q.pop();
vis[u]=0;
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].to;
if(dis[v]>dis[u]+edge[i].cost){
dis[v]=dis[u]+edge[i].cost;
if(!vis[v]){
vis[v]=1;
used[v]++;
if(used[v]>n){
printf("YES\n");//存在负环就意味着时光能够倒流。
return ;
}
q.push(v);
}
}
}
}
printf("NO\n");
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d%d",&n,&m,&w);
init();
while(m--){
int a,b,cost;
scanf("%d%d%d",&a,&b,&cost);//加入无向边。
add(a,b,cost);
add(b,a,cost);
}
while(w--){
int a,b,cost;//加入单向虫洞。 scanf("%d%d%d",&a,&b,&cost);
cost=-cost;
add(a,b,cost);
}
spfa();
}
return 0;
}

ac代码:

POJ--3529--Wormholes的更多相关文章

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

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

  2. 最短路(Bellman_Ford) POJ 3259 Wormholes

    题目传送门 /* 题意:一张有双方向连通和单方向连通的图,单方向的是负权值,问是否能回到过去(权值和为负) Bellman_Ford:循环n-1次松弛操作,再判断是否存在负权回路(因为如果有会一直减下 ...

  3. poj - 3259 Wormholes (bellman-ford算法求最短路)

    http://poj.org/problem?id=3259 农夫john发现了一些虫洞,虫洞是一种在你到达虫洞之前把你送回目的地的一种方式,FJ的每个农场,由n块土地(编号为1-n),M 条路,和W ...

  4. POJ 3259 Wormholes(最短路径,求负环)

    POJ 3259 Wormholes(最短路径,求负环) Description While exploring his many farms, Farmer John has discovered ...

  5. POJ 3259 Wormholes (Bellman_ford算法)

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

  6. poj 3259 Wormholes

    题目连接 http://poj.org/problem?id=3259 Wormholes Description While exploring his many farms, Farmer Joh ...

  7. POJ 3259 Wormholes(最短路,判断有没有负环回路)

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24249   Accepted: 8652 Descri ...

  8. [ACM] POJ 3259 Wormholes (bellman-ford最短路径,推断是否存在负权回路)

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 29971   Accepted: 10844 Descr ...

  9. POJ 3259 Wormholes【bellman_ford判断负环——基础入门题】

    链接: http://poj.org/problem?id=3259 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  10. POJ 3259——Wormholes——————【最短路、SPFA、判负环】

    Wormholes Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit St ...

随机推荐

  1. C语言中的位域、字节序、比特序、大小端

    转:http://www.360doc.com/content/13/0624/10/496343_295125641.shtml 1.比特序 / 位序 /  bit numbering / bit  ...

  2. [Linux] Linux命令之pstree - 以树状图显示进程间的关系

    转载自: http://codingstandards.iteye.com/blog/842156 pstree命令以树状图显示进程间的关系(display a tree of processes). ...

  3. [转]SSIS高级转换任务—在Package中是用临时表是需要设置RetainSameConnection属性

    本文转自:http://www.cnblogs.com/tylerdonet/archive/2011/05/20/2052306.html 在上一个导入列这个例子中我们创建一个实际的表来存储文件路径 ...

  4. DevExpress 小计 GridControl 隔行换行

    摘自: http://www.cnblogs.com/yuerdongni/archive/2012/09/08/2676753.html 1. 如何解决单击记录整行选中的问题 View->Op ...

  5. Getting to grips with CakePHP’s events system

    CakePHP seems to get a slightly unfavourable reputation when compared to the likes of Symfony or Zen ...

  6. phpMailer中文说明[转]

    A开头: $AltBody--属性出自:PHPMailer::$AltBody文件:class.phpmailer.php说明:该属性的设置是在邮件正文不支持HTML的备用显示 AddAddress- ...

  7. C#注释——爱你不是两三天

    说到注释这个东东,我不得不说:爱你不是两三天,每天却想你很多遍...原来梁静茹同学这首歌不全然是情歌啊~   一句注释也没有的一大片的代码有木有 看着那些无名者写的神秘代码,有没有骂一句,你妹的... ...

  8. Win8 Metro中文件读写删除与复制操作

    Win8Metro中,我们不能在向以前那样调用WIN32的API函数来进行文件操作,因此,下面就来介绍一下Win8 Metro中文件的读写操作. 1 Windows 8 Metro Style App ...

  9. 字符串在内存中的存储——C语言进阶

    字符串是以ASCII字符NUL结尾的字符序列. ASCII字符NUL表示为\0.字符串通常存储在数组或者从堆上分配的内存中.只是,并不是全部的字符数组都是字符串,字符数组可能没有NUL字符. 字符数组 ...

  10. 为什么用strlcpy取代strncpy

    为什么用strlcpy取代strncpy 标签: C, C语言, strlcpy, strncpy 标题: 为什么用strlcpy取代strncpy作者: Demon链接: http://demon. ...