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 ...
随机推荐
- web 端即时通讯
1. 前言 Web端即时通讯技术因受限于浏览器的设计限制,一直以来实现起来并不容易,主流的Web端即时通讯方案大致有4种:传统Ajax短轮询.Comet技术.WebSocket技术.SSE(Serve ...
- iterator和iterable的区别
相关博客: http://blog.csdn.net/lipengcn/article/details/51700153 Java中Iterable和Iterator的辨析 http ...
- WPF使用Winform PDFView控件
最近开发wpf项目中有一个模块需要显示PDF文件内容.由于WPF本身没有PDF加载控件(似乎有收费的我查到过类似的资料.如果有新的pdf控件也请通知我一下谢谢). 项目使用之前也是从网上获取的资料,因 ...
- Centos7下Docker的使用
一.安装Docker 1.1.查看原有系统是否已经安装docker yum list installed | grep docker 1.2.如果有则不需要继续安装,想重新安装,先卸载 yum -y ...
- 【Oracle】闪回技术
1.闪回技术描述 2.数据库的准备: --undo表空间要设置成AUTO,设置合适的保存时间.undo表空间: SYS@ENMOEDU> show parameter undo NAME TYP ...
- Visual Studio 编辑代码量大的文件时分屏技巧
- 改造PAXOS算法消灭活锁
分布式一致性协议的目的是确定一个不可变变量分布式存储的取值:通过对国内外一致性算法的研究成果和PAXOS协议活锁的分析,发现引入一个角色作为竞争时的代理提交者就可以解决活锁问题,从而在本文引入“代理提 ...
- MongoDB经典入门(2)--shell命令
MongoDB是MongoDB自带的交互式Javascript shell,用来对MongoDB进行操作和管理的交互式环境. 使用 "./mongo --help" 可查看相关连接 ...
- vc++如何创建窗口
创建一个mfc工程,AppWizard可以用来增加和选择一些特性 可以发现一共有五个类,每一个类都有一个基类,都是mfc基础类库中的类 例如将一个类拷贝一下,将CFrameWnd到msdn中查找一下, ...
- hibernate注解--@transient
@transient:表示该属性并非一个到数据库表的字段的映射,ORM框架将忽略该属性. 如果一个属性并非数据库表的字段映射,就务必将其标示为@Transient,否则,ORM框架默认其注解为@Bas ...