uva 558 - Wormholes

点击打开链接uva 558

1思路:利用Bellman_Ford来判断是否存在回环

2分析:在利用Bellman_Fordde 时候如果做了n-1次的松弛步以后还能更新dis数组,那么说明原来的图中存在环,那么最短路就是不存在的。

代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
#define MAXN 2010
#define INF 0xFFFFFFF

int t , n , m;
int dis[MAXN];
struct Edge{
   int x;
   int y;
   int value;
}e[MAXN];

bool judge(){
     for(int i = 0 ; i < m ; i++){
        if(dis[e[i].y] > dis[e[i].x] + e[i].value)
          return false;
     }
     return true;
}

void Bellman_Ford(){
     dis[0] = 0;
     for(int i = 1 ; i < n ; i++)
        dis[i] = INF;
     for(int i = 0 ; i < n ; i++){
        for(int j = 0 ; j < m ; j++){
           if(dis[e[j].y] > dis[e[j].x] + e[j].value)
             dis[e[j].y] = dis[e[j].x] + e[j].value;
        }
     }
     if(judge())
       printf("not possible\n");
     else
       printf("possible\n");
}

int main(){
   scanf("%d", &t);
   while(t--){
      scanf("%d%d" , &n , &m);
      for(int i = 0 ; i < m ; i++)
         scanf("%d%d%d" , &e[i].x , &e[i].y , &e[i].value);
      Bellman_Ford();
   }
   return 0;
}
时间: 2024-07-31 10:02:01

uva 558 - Wormholes的相关文章

UVa 558:Wormholes (求负回路)

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=&problem=499&mosmsg=Submission+received+with+ID+10614453 题目: Wormholes In the year 2163, wormholes were discovered. A wormhole is a sub

最短路专题【完结】

第一题 hdu 1317 XYZZY 点击打开hdu 1317 思路: 1 题目的图是一个有向图,并且可能存在环.第一个点的能量值为100,边的权值利用能量大小,例如2点为-60,如果1->2那么value[1][2] = -602 题目明确指出如果是要win的话,那么必须是经过的每条边都要大于0.那么我们只要把那些经过松弛操作后的点大于0的入队即可,小于等于0的点肯定不会出现在最终的路径上.3 如果存在正环的话,那么就有能量值无限大,那么这个时候只要判断这个点能否到达n4 判断是否是有环还是五

UVa 10602

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1543 类型:贪心 原题: Company Macrohard has released it's new version of editor Nottoobad, which can understand a few voice commands.

UVa 10392 Factoring Large Numbers:素因子分解

10392 - Factoring Large Numbers Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_problem&problem=1333 One of the central ideas behind much cryptography is that factoring

UVa 10182 Bee Maja:规律&amp;amp;O(1)算法

10182 - Bee Maja Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1123 Maja is a bee. She lives in a bee hive with thousands of other bees. This bee hive c

算法题之UVA 763

Fibinary Numbers The standard interpretation of the binary number 1010 is 8 + 2 = 10. An alternate way to view the sequence ``1010'' is to use Fibonacci numbers as bases instead of powers of two. For this problem, the terms of the Fibonacci sequence

算法题:UVa 11461 Square Numbers (简单数学)

11461 - Square Numbers Time limit: 1.000 seconds http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&category=467&page=show_problem&problem=24 56 A square number is an integer number whose square root is also an integer.

UVa 10183 How Many Fibs? (统计斐波那契数个数&amp;amp;高精度)

10183 - How Many Fibs? Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=1124 Recall the definition of the Fibonacci numbers:    f1 := 1    f2 := 2    fn :

UVa 701 The Archeologists&#039; Dilemma: 数学及枚举

701 - The Archeologists' Dilemma Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=642 An archeologist seeking proof of the presence of extraterrestrials i