POJ 3259 Wormholes( bellmanFord判负环)
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 36425 | Accepted: 13320 |
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
For farm 2, FJ could travel back in time by the cycle
1->2->3->1, arriving back at his starting location 1 second
before he leaves. He could start from anywhere on the cycle to
accomplish this.
Source
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
using namespace std; const int INF=0x3f3f3f3f;
const double eps=1e-;
const double PI=acos(-1.0); const int maxn=+;
int n;
struct Edge
{
int u, v, w, next;
};
Edge edge[maxn];
int num;
int head[maxn];
void init_edge()
{
num = ;
memset(head, -, sizeof(head));
}
void addedge(int u, int v, int w)
{
edge[num].u = u;
edge[num].v = v;
edge[num].w = w;
edge[num].next = head[u];
head[u] = num++;
}
int dis[maxn];
bool bellmanFord()//bellmanFord模板
{
for(int i = ; i <= n; i++) dis[i] = INF;
dis[] = ;
for(int i = ; i < n; i++)
{
for(int j = ; j < num; j++)
{
if(dis[edge[j].v] > dis[edge[j].u] + edge[j].w)
dis[edge[j].v] = dis[edge[j].u] + edge[j].w;
}
}
//bool flag = 1;
for(int j = ; j < num; j++)
{
if(dis[edge[j].v] > dis[edge[j].u] + edge[j].w)
return ;
}
return ;
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int m ,w;
scanf("%d%d%d",&n, &m, &w);
int a, b, c;
init_edge();
for(int i = ; i < m; i++)
{
scanf("%d%d%d", &a, &b,&c);
addedge(a, b, c);
addedge(b, a, c);
}
for(int i = ; i < w; i++)
{
scanf("%d%d%d", &a, &b,&c);
addedge(a, b, -c);
}
//int flag = 0;
if(!bellmanFord())
printf("YES\n");
else
printf("NO\n");
}
return ;
}
POJ 3259 Wormholes( bellmanFord判负环)的更多相关文章
- POJ 3259 Wormholes(SPFA判负环)
题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是 ...
- POJ 3259 Wormholes (判负环)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46123 Accepted: 17033 Descripti ...
- Wormholes POJ 3259(SPFA判负环)
Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...
- POJ 3259 Wormholes 最短路+负环
原题链接:http://poj.org/problem?id=3259 题意 有个很厉害的农民,它可以穿越虫洞去他的农场,当然他也可以通过道路,虫洞都是单向的,道路都是双向的,道路会花时间,虫洞会倒退 ...
- POJ 3259 Wormholes ( SPFA判断负环 && 思维 )
题意 : 给出 N 个点,以及 M 条双向路,每一条路的权值代表你在这条路上到达终点需要那么时间,接下来给出 W 个虫洞,虫洞给出的形式为 A B C 代表能将你从 A 送到 B 点,并且回到 C 个 ...
- POJ 1860 Currency Exchange (bellman-ford判负环)
Currency Exchange 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/E Description Several c ...
- poj - 3259 Wormholes (bellman-ford算法求最短路)
http://poj.org/problem?id=3259 农夫john发现了一些虫洞,虫洞是一种在你到达虫洞之前把你送回目的地的一种方式,FJ的每个农场,由n块土地(编号为1-n),M 条路,和W ...
- poj 3621 二分+spfa判负环
http://poj.org/problem?id=3621 求一个环的{点权和}除以{边权和},使得那个环在所有环中{点权和}除以{边权和}最大. 0/1整数划分问题 令在一个环里,点权为v[i], ...
- [poj3259]Wormholes(spfa判负环)
题意:有向图判负环. 解题关键:spfa算法+hash判负圈. spfa判断负环:若一个点入队次数大于节点数,则存在负环. 两点间如果有最短路,那么每个结点最多经过一次,这条路不超过$n-1$条边. ...
随机推荐
- linux 网络Socket实战
Preface:就算调通API,也不值得太过自豪!!!悉心细心学习,最好的深度学习就是看-学...*.h/*.class ProtoType; 1,linux C ftp C/S简单实现 ftpS端码 ...
- 关于vs的lib文件和dll文件
一.LIB文件概念 一个lib文件是obj文件的集合.当然,其中还夹杂着其他一些辅助信息,目的是为了让编译器能够准确找到对应的obj文件 二.与DLL的区别 (1)lib是编译时需要的,dll是运行时 ...
- 关于Redis
在同步dump.rdb文件时要执行service redis stop后,再拷贝目标rdb文件过去,然后再start 而不是拷贝目标rdb文件过去后直接执行restart 因为redis在执行sto ...
- eq,neq,gt,lt等表达式缩写
eq 等于neq 不等于gt 大于egt 大于等于lt 小于elt 小于等于like LIKEbetween BETWEENnotnull IS NUT NULLnull IS NULL
- PHP文件目录copy
/**(2) PHP文件目录copy @param string $dirsrc 原目录名称字符串 @param string $dirto 目标目录名称字符串 */ function copyDir ...
- Hibernate框架后续
持久化对象的唯一标识OID 1:我们都知道,在java中按照内存地址来区分同一个类的不同对象 而关系数据库按照主键来区分一条记录 在Hibernate中使用OID来建立内存中的对象和数据 ...
- IOS 创建一张有颜色的UIImage
#import <UIKit/UIKit.h> @interface UIImage (ImageWithColor) + (UIImage *)imageWithColor:(UICol ...
- [ES7] Object.observe + Microtasks
ES6: If you know about the Javascirpt's event loop. You know that any asyns opreations will be throw ...
- Flashback version/Transaction Query,FlashbackTable
Flashback version Query相对于Flashback Query 只能看到某一点的对象状态, Oracle 10g引入的Flashback Version Query可以看到过去某个 ...
- socket——本地服务器和android手机客户端通讯(防止中文乱码)
线上效果图: 服务端接收到的. 客户端接受到服务器返回的. server端代码直接运行在本地就可以了. 手机客户端运行在手机上就行. 先安装客户端,再启动server.然后再输入文字,点击发送. se ...