poj3259 Bellman_Ford算法

Wormholes

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 34465   Accepted: 12585

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..NM (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: A single integer, FF farm descriptions follow. 
Line 1 of each farm: Three space-separated integers respectively: NM, and W 
Lines 2..M+1 of each farm: Three space-separated numbers (SET) 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 (SET) that describe, respectively: A one way path from S to E that also moves the traveler back T seconds.

Output

Lines 1..F: For each farm, output "YES" if FJ can achieve his goal, otherwise output "NO" (do not include the quotes).

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 1, FJ cannot travel back in time. 
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.

[email protected]$!#%^#$^@#%$^&*^[email protected]$#@#$%^&**(&^%!^&*&!

 1 #include<stdio.h>
 2 #include<string.h>
 3
 4 const int INF=99999999;
 5
 6 struct Node{
 7     int u;
 8     int v;
 9     int o;
10 };
11
12 Node  edge[10250];
13 int dis[550];
14 int n,m,w,nn;
15
16 void relax(int u,int v,int o)
17 {
18     if(dis[v]>dis[u]+o)
19         dis[v]=dis[u]+o;
20 }
21
22 bool Bellan_Ford()
23 {
24     int i,j,k;
25     for(i=1;i<=n;i++)
26         dis[i]=INF;
27
28     for(i=1;i<=n-1;i++)
29     {
30         for(j=1;j<=nn;j++)
31             relax(edge[j].u,edge[j].v,edge[j].o);
32     }
33
34     for(i=1;i<=nn;i++)
35     {
36         if(dis[edge[i].v]>dis[edge[i].u]+edge[i].o)
37         {
38             return false;
39         }
40     }
41     return true;
42 }
43
44 int main()
45 {
46     int F,i,j,k,flg;
47     int s,e,t;
48     scanf("%d",&F);
49     while(F--)
50     {
51         flg=0;nn=1;
52         scanf("%d %d %d",&n,&m,&w);
53         for(i=1;i<=m;i++)
54         {
55             scanf("%d %d %d",&edge[nn].u,&edge[nn].v,&edge[nn].o);
56             nn++;
57             edge[nn].u=edge[nn-1].v,edge[nn].v=edge[nn-1].u,edge[nn].o=edge[nn-1].o;
58             nn++;
59         }
60         for(i=1;i<=w;i++)
61         {
62             scanf("%d %d %d",&edge[nn].u,&edge[nn].v,&edge[nn].o);
63             edge[nn].o=-edge[nn].o;
64             nn++;
65         }
66         nn--;
67         if(Bellan_Ford()==false)
68         {
69             flg=1;
70         }
71         if(flg==1)
72             printf("YES\n");
73         else
74             printf("NO\n");
75     }
76     return 0;
77 }

时间: 2024-10-14 19:20:16

poj3259 Bellman_Ford算法的相关文章

Bellman_Ford算法

Bellman_Ford算法 Bellman_Ford算法也是求单源最短路径的算法,但是它能算带负权边的图的最短路径(对于带负圈的图就无能为力),且可以判断当前图是否带有负圈.它的时间复杂度是O(n*m),其中n为点数,m为边数. Bellman_Ford算法为什么能求得单源最短路径呢?因为它一共松弛n-1轮,每轮遍历了所有的边,所以它每轮至少要生成一个点的最短距离.所以通过n-1轮后,必然产生所有点的最短距离.(可见刘汝佳<<入门经典>>P205以及<<训练指南>

POJ 3259 Bellman_Ford算法

额.关键是读题.反正我是看了解题报告才知道意思的.给你n个点.m条路.双向的.耗费时间.w个虫洞.单向的.时间为负值.问你是否可以从某一点返回看到之前的自己.即为判断是不是有负环.用Bellman_Ford算法. 分分钟打完.排了好久的bug.还是循环那里j和i傻傻的分不清楚. 附代码:#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>#define maxn 0x

Bellman_ford 算法 Currency Exchange POJ1860

Bellman_ford算法用于寻找正环或者负环! 算法导论: 24.1 The Bellman-Ford algorithm The Bellman-Ford algorithm solves the single-source shortest-paths problem in the general case in which edge weights may be negative. Given a weighted, directed graph G = (V, E) with sou

POJ 3259 Wormholes (bellman_ford算法判负环)

Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 32393   Accepted: 11771 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 p

【转】Bellman_ford算法

原文链接:http://www.cnblogs.com/Jason-Damon/archive/2012/04/21/2460850.html 摘自百度百科 Bellman-ford算法是求含负权图的单源最短路径算法,效率很低,但代码很容易写.即进行不停地松弛(relaxation),每次松弛把每条边都更新一下,若n-1次松弛后还能更新,则说明图中有负环(即负权回路,本文最后有解释),无法得出结果,否则就成功完成.Bellman-ford算法有一个小优化:每次松弛先设一个旗帜flag,初值为FA

poj1860 Bellman_Ford算法

Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21922   Accepted: 7910 Description Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and pe

Bellman_Ford算法求带有负边权的最短路算法

给定一个n个点m条边的有向图,图中可能存在重边和自环, 边权可能为负数. 请你求出从1号点到n号点的最多经过k条边的最短距离,如果无法从1号点走到n号点,输出impossible. 注意:图中可能 存在负权回路 . 输入格式 第一行包含三个整数n,m,k. 接下来m行,每行包含三个整数x,y,z,表示点x和点y之间存在一条有向边,边长为z. 输出格式 输出一个整数,表示从1号点到n号点的最多经过k条边的最短距离. 如果不存在满足条件的路径,则输出“impossible”. 数据范围 1≤n,k≤

Dijkstra算法优先队列实现与Bellman_Ford队列实现的理解

1 /* 2 Dijkstra算法用优先队列来实现,实现了每一条边最多遍历一次. 要知道,我们从队列头部找到的都是到 3 已经"建好树"的最短距离以及该节点编号, 并由该节点去更新 树根 到其他点(被更新的节点可以在队列中 4 ,也可以是非队列中的节点)的距离 . 5 6 ////队列中的节点都是要等待更新的节点 7 8 如果某个节点从队列中出来的时候,如果cur.first != dist[cur.second] 就是 cur.second这个节点一开始 9 被更新的最短距离值 和

[ACM] 最短路算法整理(bellman_ford , SPFA , floyed , dijkstra 思想,步骤及模板)

以杭电2544题目为例 最短路 Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你可以帮助他们吗? Input 输入包括多组数据.每组数据第一行是两个整数N.M(N<=100,M<=10000),N表示成都的大街上有几个路口,标号为1的路口是商店所在地,标号为N的路口是赛场所在地,M则表示在成都有几条路.N=M=0