poj 3259-- Wormholes(SPFA)
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 37415 | Accepted: 13764 |
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#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f
int m,n,cnt,p;
int head[1010],vis[1010],used[1010],dist[1010];
struct node
{
int u,v;
int val,next;
}edge[20010];
void add(int u,int v,int val)
{
edge[cnt].u=u;
edge[cnt].v=v;
edge[cnt].val=val;
edge[cnt].next=head[u];
head[u]=cnt++;
}
int SPFA(int st)
{
queue<int>q;
memset(dist,INF,sizeof(dist));
memset(used,0,sizeof(used));
memset(vis,0,sizeof(vis));
vis[st]=1;
dist[st]=0;
q.push(st);//各种初始化
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=0;//进入循环一定要改成0,这样才能判断进入了多少次
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(dist[v]>dist[u]+edge[i].val)
{
dist[v]=dist[u]+edge[i].val;
if(!vis[v])
{
vis[v]=1;
used[v]++;
if(used[v]>=m)//判断是否出现了负权边
return 1;
q.push(v);
}
}
}
}
return 0;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(head,-1,sizeof(head));
scanf("%d%d%d",&m,&n,&p);
int x,y,z;
while(n--)
{
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
add(y,x,z);
}
while(p--)
{
scanf("%d%d%d",&x,&y,&z);
add(x,y,-z);
add(y,x,INF);//虫洞是单向的,所以反向是无穷
}
if(SPFA(1)) printf("YES\n");
else printf("NO\n");
}
return 0;
}
poj 3259-- Wormholes(SPFA)的更多相关文章
- ACM: POJ 3259 Wormholes - SPFA负环判定
POJ 3259 Wormholes Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu ...
- POJ 3259 Wormholes(SPFA判负环)
题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是 ...
- poj 3259 Wormholes spfa算法
点击打开链接 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25582 Accepted: 9186 ...
- POJ 3259 Wormholes SPFA算法题解
版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...
- POJ 3259 Wormholes ( SPFA判断负环 && 思维 )
题意 : 给出 N 个点,以及 M 条双向路,每一条路的权值代表你在这条路上到达终点需要那么时间,接下来给出 W 个虫洞,虫洞给出的形式为 A B C 代表能将你从 A 送到 B 点,并且回到 C 个 ...
- 最短路(Bellman_Ford) POJ 3259 Wormholes
题目传送门 /* 题意:一张有双方向连通和单方向连通的图,单方向的是负权值,问是否能回到过去(权值和为负) Bellman_Ford:循环n-1次松弛操作,再判断是否存在负权回路(因为如果有会一直减下 ...
- poj - 3259 Wormholes (bellman-ford算法求最短路)
http://poj.org/problem?id=3259 农夫john发现了一些虫洞,虫洞是一种在你到达虫洞之前把你送回目的地的一种方式,FJ的每个农场,由n块土地(编号为1-n),M 条路,和W ...
- POJ 3259 Wormholes(最短路径,求负环)
POJ 3259 Wormholes(最短路径,求负环) Description While exploring his many farms, Farmer John has discovered ...
- POJ 3259——Wormholes——————【最短路、SPFA、判负环】
Wormholes Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit St ...
- Poj(3259),SPFA,判负环
题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
随机推荐
- [转]浏览器缓存详解: expires, cache-control, last-modified, etag详细说明
最近在对CDN进行优化,对浏览器缓存深入研究了一下,记录一下,方便后来者 画了一个草图: 每个状态的详细说明如下: 1.Last-Modified 在浏览器第一次请求某一个URL时,服务器端的返回状态 ...
- jquery对象与DOM对象的转化(简化版):
1:DOM对象 var apple = document.getElementById('apple'); 2:jquery对象 var _apple = $('#apple'); 3:DOM对象=& ...
- Md2All版本更新记录
Md2All版本更新记录 版本号:V2.8.2更新日期:2018-06-281:结合云图床,解决了Latex公式复制到公众号时有可能报“图片粘贴失败的问题”;2:结合云图床,解决了Latex公式复制到 ...
- 关于OpenCV的Mat画图问题
由于OpenCV的java版本画图有太多错误,只能自己编写画图的代码,在一个函数中,编写出画圆和深度距离的代码, 代码如下: public int CircleMyMat(Mat Show, Poin ...
- trait 和abstract的区别在哪里
无法在一个class上extend多个abstract class,但是你可以use多个trait abstract class是在类型系统上做文章,trait片段是mixin 类型约束 代码复用 c ...
- Git及Github环境搭建(Windows系统)
一.github账号注册 1.打开网址https://github.com 注册账号: 二.本地安装Git 1.安装包下载地址:链接:https://pan.baidu.com/s/1smpnJL7 ...
- 浅谈Json数据格式
我们先来看下w3cschool对json的定义: JSON:JavaScript 对象表示法(JavaScript Object Notation). JSON 是存储和交换文本信息的语法.类似 XM ...
- 数据挖掘系列 (1) 关联规则挖掘基本概念与 Aprior 算法
转自:http://www.cnblogs.com/fengfenggirl/p/associate_apriori.html 数据挖掘系列 (1) 关联规则挖掘基本概念与 Aprior 算法 我计划 ...
- Pavel and barbecue
Pavel and barbecue time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- String 经常用法最优算法实现总结 (一)
<pre name="code" class="java"><span style="font-family: Arial, Hel ...