poj 3259 Wormholes(Bellman-ford 算法)
来源:互联网 发布:阿里云logo矢量 编辑:程序博客网 时间:2024/06/08 00:02
题目地址
http://poj.org/problem?id=3259
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
23 3 11 2 21 3 42 3 13 1 33 2 11 2 32 3 43 1 8
Sample Output
NOYES这题主要是把题读懂。题意是:
2个test case 每个test case 第一行:
N M W
N个点 M条双向正权边 W条单向负权边 第一个test case 最后一行 3 1 3 是单向负权边,3->1的边权值是-3
因为有负边,所以用bellman-ford算法就行。
AC代码:
#include <stdio.h>#include <string.h>#define N 505#define inf 100000int d[N];int n, m;typedef struct{ int from; int to; int cost;}node;node edges[5300];int bellman_ford(int m){ int i, j, k, t; int ok; for(i=1; i<=n; i++) d[i] = inf; d[1] = 0; for(i=1; i<=n-1; i++) { ok = 1; for(j=1; j<=m; j++) { if(d[edges[j].from] > d[edges[j].to] + edges[j].cost) { d[edges[j].from] = d[edges[j].to] + edges[j].cost; ok = 0; } } if(ok) break; } for(j=1; j<=m; j++) { if(d[edges[j].from] > d[edges[j].to] + edges[j].cost) return 0; } return 1;}int main(){ int i, j, t, x; int a, b, c, w; scanf("%d", &t); while(t--) { scanf("%d%d%d", &n, &m, &w); memset(edges, 0, sizeof(edges)); for(i=0, j=1; j<=m;j++) { scanf("%d%d%d", &a, &b, &c); i++; edges[i].from = a; edges[i].to= b; edges[i].cost = c; i++; edges[i].from = b; edges[i].to = a; edges[i].cost = c; } for(j=1; j<=w; j++) { scanf("%d%d%d", &a, &b, &c); i++; edges[i].from = a ; edges[i].to = b; edges[i].cost = -c; } if(bellman_ford(i)) printf("NO\n"); else printf("YES\n"); } return 0;}
- poj 3259 Wormholes(Bellman-ford 算法)
- POj 3259-Wormholes(Bellman-Ford算法)
- POJ 3259 Wormholes(bellman-ford)
- POJ 3259 Wormholes Bellman-Ford
- poj 3259 Wormholes(Bellman-Ford)
- POJ 3259 Wormholes (Bellman-ford)
- POJ 3259 Wormholes 【bellman-ford】
- poj 3259 Wormholes (Bellman-ford)
- 【POJ 3259 Wormholes】+ Bellman-Ford
- poj 3259 Wormholes(bellman-ford)
- POJ 3259 Wormholes (Bellman-Ford算法的运用)
- poj 3259 Wormholes(最短路Bellman-Ford算法)
- poj 3259 Wormholes(SPFA || Bellman-Ford)
- poj 3259 Wormholes【Bellman-Ford Vs SPFA】
- poj 3259 Wormholes SPFA // Bellman-ford
- POJ.3259 Wormholes , Bellman-Ford , 找负权环
- POJ 3259 Wormholes Bellman-Ford找负环
- POJ 3259 Wormholes(Bellman-Ford)
- org.springframework.aop Interface ThrowsAdvice
- 适配器之SimpleAdapter
- FidderCore
- 大O表示法算法复杂度速查表(Big-O Algorithm Complexity Cheat Sheet)
- 针对iphone4调整样式
- poj 3259 Wormholes(Bellman-ford 算法)
- js构造函数
- hashMap
- 自动填充短信验证码(使用ContentObserver)
- WinForm中的ComoBox选项值的多种加载方法
- hdu5446 lucas+中国剩余定理
- 微信支付客户端开发说明 第一篇
- 转载:那些年他(她)们做过的“蠢事”
- 语义化版本号 Sematic Versioning