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

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
题解:
这道题意思是这个人的农田里有道路,还有虫洞,道路是双向的,虫洞单向,他喜欢虫洞旅行,想要从起点再回到起点时间在出发的时间之前,经过虫洞时间倒退;
做这道题其实就是判断最短路有没有负环的问题;因为只要有负环,就会无限循环,到起点自然就时间倒退了;
代码:SPFA
 #include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=;
const int MAXM=*;
int dis[MAXN],vis[MAXN],used[MAXN],head[MAXM];
int N,W,M,en,flot;
queue<int>dl;
struct Edge{
int from,to,value,next;
};
Edge edg[MAXM];
void initial(){
memset(dis,INF,sizeof(dis));
memset(vis,,sizeof(vis));
memset(used,,sizeof(used));
memset(head,-,sizeof(head));
while(!dl.empty())dl.pop();
en=;flot=;
}
void print(){
if(flot)puts("YES");
else puts("NO");
}
void add(int u,int v,int w){
Edge E={u,v,w,head[u]};
edg[en]=E;
head[u]=en++;
}
void SPFA(int sx){
dis[sx]=;vis[sx]=;dl.push(sx);
used[sx]++;
while(!dl.empty()){
int k=dl.front();
dl.pop();
vis[k]=;
if(used[k]>N){
flot=;
break;
}
for(int i=head[k];i!=-;i=edg[i].next){
int v=edg[i].to;
if(dis[k]+edg[i].value<dis[v]){
dis[v]=dis[k]+edg[i].value;
if(!vis[v]){
vis[v]=;
dl.push(v);
used[v]++;
if(used[v]>N){
flot=;return ;
}
}
}
}
}
}
void get(){
int F,a,b,c;
scanf("%d",&F);
while(F--){
initial();
scanf("%d%d%d",&N,&M,&W);
while(M--){
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
add(b,a,c);
}
while(W--){
scanf("%d%d%d",&a,&b,&c);
add(a,b,-c);
}
SPFA();
print();
}
}
int main(){
get();
return ;
}

Bellman:

 #include<stdio.h>
#include<string.h>
const int INF=0x3f3f3f3f;
const int MAXN=;
const int MAXM=;
int dis[MAXN];
struct Edge{
int u,v,w;
};
Edge edg[MAXM];
int N,M,W,top;
bool Bellman(int sx){
int u,v,w;
memset(dis,INF,sizeof(dis));
dis[sx]=;
for(int i=;i<=N;i++){
for(int j=;j<top;j++){
u=edg[j].u;v=edg[j].v;w=edg[j].w;
if(dis[u]+w<dis[v])dis[v]=dis[u]+w;
}
}
for(int i=;i<top;i++){
u=edg[i].u;v=edg[i].v;w=edg[i].w;
if(dis[u]+w<dis[v])return false;
}
return true;
}
int main(){
int F;
int a,b,c;
scanf("%d",&F);
while(F--){
top=;
scanf("%d%d%d",&N,&M,&W);
while(M--){
scanf("%d%d%d",&a,&b,&c);
edg[top].u=a;edg[top].v=b;edg[top++].w=c;
edg[top].u=b;edg[top].v=a;edg[top++].w=c;
}
while(W--){
scanf("%d%d%d",&a,&b,&c);
edg[top].u=a;edg[top].v=b;edg[top++].w=-c;
}
if(Bellman())puts("NO");
else puts("YES");
}
return ;
}

Wormholes(SPFA+Bellman)的更多相关文章

  1. poj 3259 Wormholes spfa算法

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

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

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

  3. 一个人的旅行(floyd+dijskra+SPFA+Bellman)

    一个人的旅行 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  4. 最短路(dijskra+SPFA+Bellman)

    最短路 Time Limit : 5000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submissio ...

  5. ZOJ 2770 Burn the Linked Camp(spfa&&bellman)

    //差分约束 >=求最长路径 <=求最短路径 结果都一样//spfa#include<stdio.h> #include<string.h> #include< ...

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

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

  7. POJ3259 :Wormholes(SPFA判负环)

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

  8. POJ3259 Wormholes(SPFA判断负环)

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

  9. POJ3259 Wormholes —— spfa求负环

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

随机推荐

  1. 阿里云 配置apache+python+django 环境 适合菜鸟

    云服务器环境:阿里云服务:20G+1M带宽+centos+512M 一.python安装: 刚开始没有好好利用centos 的yum方法.采用wget从python官网上下载的2.7.5版本.解压安装 ...

  2. OpenWRT交叉编译

    对于当前不在OpenWRT repository中的软件,如果是用源码形式发布的,那么可以用OpenWRT Buildroot进行交叉编译. 首先编译好Buildroot(一般编译过一次固件,就已经编 ...

  3. WINDOWS API 函数(超长,值得学习)

    一.隐藏和显示光标 函数: int ShowCursor ( BOOL bShow );  参数 bshow,为布尔型,bShow的值为False时隐藏光标,为True时显示光标:该函数的返回值为整型 ...

  4. WPF中动态更新TextBlock文字中的超链接,文本

    1.------------------------------------------------------------------------- 修改超链接的文本文字: <TextBloc ...

  5. Unix/Linux环境C编程入门教程(25) C/C++字符测试那些事儿

    isalnum isalpha isascii iscntrl isdigit isgraph isislower isprint isspace ispunct isupper isxdigit介绍 ...

  6. OSCHina技术导向:Java开源QQ工具iQQ

    iQQ 使用Java语言跨平台开发,基于腾讯WebQQ 3.0网络协议.可以使用于Java所支持的各种平台上运行.作者基于Linux(Ubuntu 12.04)系统,使用IDE NetBeans开发, ...

  7. 数据挖掘之分类算法---knn算法(有matlab例子)

    knn算法(k-Nearest Neighbor algorithm).是一种经典的分类算法.注意,不是聚类算法.所以这种分类算法 必然包括了训练过程. 然而和一般性的分类算法不同,knn算法是一种懒 ...

  8. 实际用户ID,有效用户ID和设置用户ID

    摘自http://blog.csdn.net/guosha/article/details/2679334 实际用户ID,有效用户ID和设置用户ID 看UNIX相关的书时经常能遇到这几个概念,但一直没 ...

  9. Linux 下如何安装软件?

    http://zhidao.baidu.com/link?url=OkQCOZtVMXhasC8x9zFTZOumsFKf0WW25Ckr2wBF1xO08EsjrBpnMaTBlIAUYdxZ408 ...

  10. jQuery源代码学习笔记:jQuery.fn.init(selector,context,rootjQuery)代码具体解释

    3.1 源代码 init: function( selector, context, rootjQuery ) { var match, elem, ret, doc; // Handle $(&qu ...