POJ3259 Wormholes
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.
#include <iostream>
#include <vector>
using namespace std;
#define INF 0x7fffffff int FieldsN, dist[]; struct Edge
{
int s, e;
int t;
Edge() {}
Edge(int _s, int _e, int _t) :
s(_s), e(_e), t(_t) {}
}; vector<Edge> edges; bool Bellmen_Ford(int v)
{
int s, e, t;
int Size = edges.size(); for (int k = ; k <= FieldsN; k++)
dist[k] = INF;
dist[v] = ;
for (int k = ; k < FieldsN; k++) {
for (int i = ; i < Size; i++) {
s = edges[i].s;
e = edges[i].e;
t = edges[i].t;
if (dist[s] != INF && dist[s] + t < dist[e])
dist[e] = dist[s] + t;
}
}
for (int i = ; i < Size; i++) {
s = edges[i].s;
e = edges[i].e;
t = edges[i].t;
if (dist[s] + t < dist[e]) return true;
}
return false;
} int main()
{
int F, M, W, S, E, T; cin >> F;
while (F--) {
edges.clear();
cin >> FieldsN >> M >> W;
while (M--) {
cin >> S >> E >> T;
edges.push_back(Edge(S, E, T));
edges.push_back(Edge(E, S, T));
}
while (W--) {
cin >> S >> E >> T;
edges.push_back(Edge(S, E, -T));
} if (Bellmen_Ford()) printf("YES\n");
else printf("NO\n");
} //system("pause");
return ;
}
POJ3259 Wormholes的更多相关文章
- POJ3259 :Wormholes(SPFA判负环)
POJ3259 :Wormholes 时间限制:2000MS 内存限制:65536KByte 64位IO格式:%I64d & %I64u 描述 While exploring his many ...
- poj3259 Wormholes【Bellman-Ford或 SPFA判断是否有负环 】
题目链接:poj3259 Wormholes 题意:虫洞问题,有n个点,m条边为双向,还有w个虫洞(虫洞为单向,并且通过时间为倒流,即为负数),问你从任意某点走,能否穿越到之前. 贴个SPFA代码: ...
- POJ3259 Wormholes 【spfa判负环】
题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
- POJ3259——Wormholes(Bellman-Ford+SPFA)
Wormholes DescriptionWhile exploring his many farms, Farmer John has discovered a number of amazing ...
- POJ--3259 Wormholes (SPFA判负环)
题目电波 3259 Wormholes #include<iostream> #include<cstring> #include<algorithm> #in ...
- poj3259 Wormholes(Bellman-Ford判断负圈)
https://vjudge.net/problem/POJ-3259 一开始理解错题意了,以为从A->B一定得走路,B->A一定得走虫洞.emmm其实回来的时候可以路和虫洞都可以走,只要 ...
- poj3259: Wormholes(BF模板题)
http://poj.org/problem?id=3259 Description While exploring his many farms, Farmer John has discovere ...
- poj3259 Wormholes【最短路-bellman-负环】
While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole ...
- POJ-3259 Wormholes(判断负环、模板)
Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...
随机推荐
- mac上配置java开发环境
项目在mac上跑起来的步骤: 1. 访问,https://brew.sh/ 装上这个然后 brew install git brew install maven, settings.xml需要放 ...
- excel表格中,怎么根据一列重复的数据求对应另一列总和
如下: 求出姓名对应分数总和对应 : 首先复制一份Sheet2 对Sheet1进行操作选中A列姓名 数据->删除重复项->以前选中区域排序->删除重复项 然后删除对应成绩项选中张三对 ...
- position的sticky与fixed
fixed(固定定位) 生成绝对定位的元素,相对于浏览器窗口进行定位.元素的位置通过 "left", "top", "right" 以及 & ...
- 走进JavaScript
JavaScript的作用:操作HTML元素,响应用户的操作,处理数据: script标签的type或者language可以写也可以不写: script标签防止位置:head结束之前或者body结束之 ...
- day41-python多进程多线程-多线程共享
线程共享变量多线程和多进程不同之处在于多线程本身就是可以和父进程共享内存的,这也是为什么其中一个线程挂掉以后,为什么其他线程也会死掉的道理. import threading def worker() ...
- java中的\b是什么意思?
java中有2个地方有\b,一个是特殊字符\b,另一个是在正则表达式中表示边界的意思. 我们这里只讨论特殊字符\b 我这里一共接受到几种解释: 1.退格符相当于键盘上的Backspace符号 back ...
- 利用教育邮箱注册JetBrains产品(pycharm、idea等)的方法
转载:http://www.cnblogs.com/wang-meng/p/8887436.html 1,申请邮箱 地址为:http://mdu.edu.rs/ 邮箱的前缀可以改成自己喜欢的字符 ...
- 【PL/SQL基础知识】结构
1.pl/sql块的结构 declare --声明的变量.类型.游标 begin --程序的执行部分(类似于java的main()方法) exception --针对begin块中出现的异常 ---w ...
- Navicat For MySQL--外键建立与cannot add foreign key constraint分析
hrm_job.png 参考资料: https://blog.csdn.net/ytm15732625529/article/details/53729155 https://www.cnblogs. ...
- es6 Array数组方法
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...