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 ...
随机推荐
- sqlplus的非交互式使用
sqlplus交互界面存在的问题 sqlplus的两种非交互式使用方式 1 通过外部命令文件 2 通过标准输入 利用管道做兴许处理 须要注意的地方 一个简单的脚本 1 sqlplus交互界面存在的问题 ...
- Wireshark 过滤 基本语法
转载 1.过滤IP,如来源IP或者目标IP等于某个IP 例子: ip.src eq 192.168.1.107 or ip.dst eq 192.168.1.107 或者 ip.addr eq 1 ...
- 20175227张雪莹 2018-2019-2 《Java程序设计》第五周学习总结
20175227张雪莹 2018-2019-2 <Java程序设计>第五周学习总结 教材学习内容总结 第六章接口与实现 接口 接口体中所有的常量访问权限一定是public和static(可 ...
- 安装一个Linux
Linux--虚拟机的的安装: 首先需要一个可执行(VMware-workstation-full-14.1.2-8497320.exe)的文件和一个Linux(CentOS-7-x86_64-DVD ...
- PC/FORTH 变量|常数|数组
body, table{font-family: 微软雅黑} table{border-collapse: collapse; border: solid gray; border-width: 2p ...
- vuex-Action(异步)
Action 类似于 mutation,不同在于: Action 提交的是 mutation,而不是直接变更状态. Action 可以包含任意异步操作. const store = new Vuex. ...
- LeetCode 46 全排列
题目: 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3 ...
- Java实现数据库与eclipse的连接
JavaBean:用于传递数据,拥有与数据相关的逻辑处理 JSP:从Model接收数据并生成HTML Servlet:接收HTTP请求并控制Model和View jdbc:用于驱动连接 一.[建立数据 ...
- 微信小程序用setData给数组对象赋值
假如现在要给数组marker中的对象属性赋值 data: { marker: [ { latitude: ' ' , longitude: ' ' } ] }, 在方法中的写法为 fetchJ ...
- SQL注入之Sqli-labs系列第三十关(基于WAF防护的双引号报错注入)和三十一关
开始挑战第三十关和三十一关(Protection with WAF) 0x1 前言 这关其实和29关是同样的,login.php页面存在防护,只要检测到存在问题就跳转到hacked.php页面,不同的 ...