POJ 3259 Wormholes (最短路)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 34302 | Accepted: 12520 |
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 <cstdio>
using namespace std; const int INF = 0xfffffff;
const int SIZE = ;
int D[];
int N,M,W,F;
struct Node
{
int from,to,cost;
}G[SIZE]; bool Bellman_Ford(int);
bool relax(int,int,int);
int main(void)
{
scanf("%d",&F);
while(F --)
{
scanf("%d%d%d",&N,&M,&W);
int i = ;
while(i < * M)
{
scanf("%d%d%d",&G[i].from,&G[i].to,&G[i].cost);
i ++;
G[i] = G[i - ];
swap(G[i].from,G[i].to);
i ++;
}
while(i < W + M * )
{
scanf("%d%d%d",&G[i].from,&G[i].to,&G[i].cost);
G[i].cost = -G[i].cost;
i ++;
}
bool flag = true;
for(int i = ;i <= N;i ++)
if(!Bellman_Ford(i))
{
puts("YES");
flag = false;
break;
}
if(flag)
puts("NO");
} return ;
} bool Bellman_Ford(int s)
{
fill(D,D + ,INF);
D[s] = ;
bool update; for(int i = ;i < N - ;i ++)
{
update = false;
for(int i = ;i < M * + W;i ++)
if(relax(G[i].from,G[i].to,G[i].cost))
update = true;
if(!update)
break;
}
for(int i = ;i < M * + W;i ++)
if(relax(G[i].from,G[i].to,G[i].cost))
return false;
return true;
} bool relax(int from,int to,int cost)
{
if(D[to] > D[from] + cost)
{
D[to] = D[from] + cost;
return true;
}
return false;
}
Bellman_Ford
POJ 3259 Wormholes (最短路)的更多相关文章
- poj 3259 Wormholes(最短路 Bellman)
题目:http://poj.org/problem?id=3259 题意:一个famer有一些农场,这些农场里面有一些田地,田地里面有一些虫洞,田地和田地之间有路,虫洞有这样的性质: 时间倒流.问你这 ...
- POJ 3259 Wormholes 最短路+负环
原题链接:http://poj.org/problem?id=3259 题意 有个很厉害的农民,它可以穿越虫洞去他的农场,当然他也可以通过道路,虫洞都是单向的,道路都是双向的,道路会花时间,虫洞会倒退 ...
- 最短路(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 ...
- ACM: POJ 3259 Wormholes - SPFA负环判定
POJ 3259 Wormholes Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu ...
- POJ 3259 Wormholes(最短路径,求负环)
POJ 3259 Wormholes(最短路径,求负环) Description While exploring his many farms, Farmer John has discovered ...
- POJ 3259 Wormholes(最短路,判断有没有负环回路)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24249 Accepted: 8652 Descri ...
- POJ 3259——Wormholes——————【最短路、SPFA、判负环】
Wormholes Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit St ...
- POJ 3259 Wormholes (Bellman_ford算法)
题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
随机推荐
- Win8制作和使用恢复盘
制作和使用恢复盘要制作恢复盘,请执行以下操作:注:确保计算机连接到交流电源.1. 将指针移至屏幕的右上角或右下角以显示超级按钮,然后单击搜索.2. 根据操作系统的不同,执行以下某项操作:• 在 Win ...
- Ajax.ActionLink与Ajax.BeginForm使用场所的思考
Ajax.ActionLink使用在提交参数明确的情况下,如: Ajax.ActionLink("加入购物车", "AddToCart", "Cart ...
- jquery视频展示 图片轮播
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Remove a Driver Package from the Driver Store
http://technet.microsoft.com/en-us/library/cc730875.aspx Determine the name of the driver package in ...
- PowerShell管理Exchange
#添加Exchange管理单元get-pssnapin -registeredadd-pssnapin microsoft.exchange* #启用邮箱账号(需要域管理员权限,因为需要将某些属性写入 ...
- 10分钟学会基于ASP.NET的 JQuery实例 (转)
本文介绍如何在ASP.NET(ASP.NET/AJAX)里使用基于JQuery的AJAX技术.(源代码下载见最后) 在使用JQuery前,请到www.jquery.com下载最新版本的js代码,然后再 ...
- Codeforces Round #340 (Div. 2) C. Watering Flowers 暴力
C. Watering Flowers 题目连接: http://www.codeforces.com/contest/617/problem/C Descriptionww.co A flowerb ...
- opencv官网
http://wiki.opencv.org.cn/index.php/Template:Doc
- MySQL Cluster 4个数据节点压力测试--mysqlslap工具压400W写
锅巴哥的个人建议:cluster叫电信运营商版本,所以基本上在很大的用户并发量的情况下才会用到,对连接数的线性增长要求高的场景,千兆就不用想了, 没万兆就不用玩了. 很不幸,我的就是千兆网络,我的数据 ...
- [Angular2 Form] Style Validation in Angular 2 Forms
Inputs using Angular 2’s ngModel automatically apply style classes of .ng-validand .ng-invalid each ...