poj 3259 Wormholes 【SPFA&&推断负环】
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 36852 | Accepted: 13502 |
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..N,
M (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 of each farm: Three space-separated integers respectively: N,
M, and W
Lines 2..M+1 of each farm: Three space-separated numbers (S,
E, T) 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 (S,
E, T) that describe, respectively: A one way path from S to
E that also moves the traveler back T seconds.
Output
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
分析:
英语不好。就不在翻译了。意思就是求最短路。假设最短路中有负环,输出yes,没有输出no。
代码:
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<cstdlib>
#define INF 0x3f3f3f3f
#define maxn 10010
using namespace std; int N,M,W;
int pnum;
int dis[maxn];
int vis[maxn];
int head[maxn];
int used[maxn];
bool cnt;
struct node{
int from;
int to;
int val;
int next;
};
node pp[2*maxn]; void addpp(int u,int v,int w)
{
node E ={u,v,w,head[u]};
pp[pnum]=E;
head[u]=pnum++;
} void init()
{
int a,b,c;
while(M--)
{
scanf("%d%d%d",&a,&b,&c);
addpp(a,b,c);
addpp(b,a,c);
}
} void gmap()
{
int a,b,c;
while(W--)
{
scanf("%d%d%d",&a,&b,&c);
addpp(a,b,-c);
}
} void SPFA(int s)
{
queue<int>q;
memset(dis,INF,sizeof(dis));
memset(vis,0,sizeof(vis));
memset(used,0,sizeof(used));
dis[s]=0;
vis[s]=1;
used[s]++;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=0;
for(int i=head[u];i!=-1;i=pp[i].next)
{
int v=pp[i].to;
if(dis[v]>dis[u]+pp[i].val)
{
dis[v]=dis[u]+pp[i].val;
if(!vis[v])
{ vis[v]=1;
q.push(v);
used[v]++;
if(used[v]>N)
{
cnt=true;
return;
}
}
}
}
} } int main(){
int T;
scanf("%d",&T);
while(T--)
{
pnum=0;
cnt=false;
memset(head,-1,sizeof(head));
scanf("%d%d%d",&N,&M,&W);
init();
gmap();
SPFA(1);
if(cnt)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
poj 3259 Wormholes 【SPFA&&推断负环】的更多相关文章
- POJ 3259 Wormholes(最短路径,求负环)
POJ 3259 Wormholes(最短路径,求负环) Description While exploring his many farms, Farmer John has discovered ...
- POJ 1151 Wormholes spfa+反向建边+负环判断+链式前向星
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 49962 Accepted: 18421 Descr ...
- ACM: POJ 3259 Wormholes - SPFA负环判定
POJ 3259 Wormholes Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu ...
- POJ 3259 Wormholes SPFA算法题解
版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...
- uva558 Wormholes SPFA 求是否存在负环
J - Wormholes Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Stat ...
- POJ 3259 Wormholes(SPFA判负环)
题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是 ...
- POJ 3259 Wormholes ( SPFA判断负环 && 思维 )
题意 : 给出 N 个点,以及 M 条双向路,每一条路的权值代表你在这条路上到达终点需要那么时间,接下来给出 W 个虫洞,虫洞给出的形式为 A B C 代表能将你从 A 送到 B 点,并且回到 C 个 ...
- [ACM] POJ 3259 Wormholes (bellman-ford最短路径,推断是否存在负权回路)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 29971 Accepted: 10844 Descr ...
- poj 3259 Wormholes spfa算法
点击打开链接 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25582 Accepted: 9186 ...
随机推荐
- hdoj--1083--Courses(最大匹配)
Courses Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- angular2 使用swiper
欢迎加入前端交流群交流知识&&获取视频资料:749539640 第一步: npm install swiper --save 第二步:下载swiper ts支持(http://micr ...
- 前端之HEML
HTML介绍 Web服务本质 import socket sk = socket.socket() sk.bind(("127.0.0.1", 8080)) sk.listen ...
- pc端和移动端的轮播图实现(只是结构,内容以后慢慢补充)
轮播图 PC端 移动端 原生js的写法 图片顺序 8123456781 设置计时器 当过度完成之后判断index是否到达两边界限,是的话设置位移 手指touchstart时,获取位置,暂停计时器 手指 ...
- spring IOC(DI)和AOP
软件152谭智馗 IOC(Inversion of Control,控制倒转)Ioc意味着将你设计好的对象交给容器控制,而不是传统的在你的对象内部直接控制. DI—Dependency Injecti ...
- Dictionary 小知识
Dictionary<string, string>是一个泛型 他本身有集合的功能有时候可以把它看成数组 他的结构是这样的:Dictionary<[key], [value]> ...
- C#自定义控件实现控件随窗口大小改变
1.新建用户控件,取名MyForm. 2.将默认的UserControl改成Form 3.在类中添加以下代码 private float X, Y; //获得控件的长度.宽度.位置.字体大小的数据 p ...
- (转)RabbitMQ学习之路由(java)
http://blog.csdn.net/zhu_tianwei/article/details/40887755 参考:http://blog.csdn.NET/lmj623565791/artic ...
- WIN系统查询版本
cmd -> DISM /online /Get-CurrentEdition //查询系统版本 WIN+R -> slmgr.vbs -ipk 查询系统注册信息slmgr.vbs -dl ...
- java_poi
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import org.apache. ...