POJ Wormholes 最短路径 ballman_ ford 有负环

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <iostream>
 5 #include <queue>
 6 #define MAX 9999999
 7
 8 using namespace std;
 9
10 struct node
11 {
12     int u, v, w;//u 为起点,v为终点,w为u—>v的权值
13 };
14 node edge[5203];
15 int n, m;//n 点数   m 边数
16
17 bool bellman_ford()
18 {
19     int i, j;
20     bool flag;
21     int dis[503];//保存最短路径
22
23     fill(dis,dis+n,MAX);//初始化
24     dis[1] = 0;//因为判断是否有负环,对整个图而言,So  s = 1;
25     //
26     for(i=1;i<n;i++)//共需进行|V|-1次
27     {
28         flag = false;//优化   初始化为假
29         for(j=0;j<m;j++)//对每一条边
30         {
31             // if  u.d>v.d+w(u,v) , u.d = v.d+w(u,v);
32             if(dis[edge[j].u]>dis[edge[j].v]+edge[j].w){//进行松弛
33                 dis[edge[j].u] = dis[edge[j].v]+edge[j].w;//松弛操作成功
34                 flag = true;//松弛成功变为真
35             }
36         }
37         if(!flag)//若每条边没有松弛
38             break;//跳出循环
39     }
40     //
41     for(i=0;i<m;i++)
42         if(dis[edge[i].u]>dis[edge[i].v]+edge[i].w)//进行|V|-1次操作后  有边还能进行松弛  说明
43             return true;//存在负环
44     return false;//不存在负环
45 }
46
47 int main()
48 {
49     int t, k, i;
50
51     scanf("%d",&t);//输入测试数据的组数
52     while(t-- && scanf("%d %d %d",&n,&m,&k)){//输入点数,正边数,负边数
53         for(i=0;i<m;i++)
54         {
55             scanf("%d %d %d",&edge[i].u,&edge[i].v,&edge[i].w);//输入u,v,w;
56             edge[i+m].u = edge[i].v;//双向
57             edge[i+m].v = edge[i].u;//双向
58             edge[i+m].w = edge[i].w;//双向
59         }
60         m <<= 1;//正边为双向 所以m = m*2;
61         for(i=m;i<m+k;i++)
62         {
63             scanf("%d %d %d",&edge[i].u,&edge[i].v,&edge[i].w);//存负边数(单向)
64             edge[i].w = -edge[i].w;//负边就要是负的
65         }
66         m += k;//单向,So不需要*2
67         printf("%s\n",bellman_ford()?"YES":"NO");//输出结果
68     }
69     return 0;
70 }

题目大意: 第一行 输入一个数  是表示几组测试数据

    第二行  三个数 N(点的个数),M(正边的个数),W(负边的个数) 注意 :正边为双向的,负边为单向的。

    然后 M行u,v,w;

    再然后W行u,v,w;

    求这个图是不是存在负环。 有 YES 没NO。

时间: 2024-10-26 21:21:58

POJ Wormholes 最短路径 ballman_ ford 有负环的相关文章

POJ 1364 King 差分约束 找负环

嘛,虽然是一道水题+模板题,不过还是学到了很多东西的,记录一下. 首先题目给出的不等式是小于,但是差分约束系统只能处理小于等于的情况,所以要转化成小于等于的进行处理.对于整数处理方法非常简单= = 然后是找负环的情况,其实不需要考虑图连不连通,只要一开始就把所有的点的d置成0,然后都push进队列里面就好了. PS:这种方法同样可以用在处理多源点最短路问题上. #include <cstdio> #include <cstring> #include <cmath> #

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

POJ 3259 虫洞旅行 spfa判负环

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

uva558 Wormholes SPFA 求是否存在负环

J - Wormholes Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description In the year 2163, wormholes were discovered. A wormhole is a subspace tunnel through space and time connecting two star systems. Wormholes

uva 558 Wormholes (Bellman-Ford算法判断负环)

uva 558 Wormholes In the year 2163, wormholes were discovered. A wormhole is a subspace tunnel through space and time connecting two star systems. Wormholes have a few peculiar properties: Wormholes are one-way only. The time it takes to travel throu

POJ 2679 Adventurous Driving | SPFA + 判定负环

POJ 2679 Adventurous Driving 恶心的输入恶心的题面啊...这道题学到的东西还是蛮多的 Description..有向图..边权有两个:1.费用, 2.长度.要求找出S到T花费最小的路.一定要是花费最小....在花费最小的情况下输出路径长度的最小值..然后边权可以为负..不保证S到T一定有一条路..同时点a到点b之间可能有多条路... 总之就是..S不能到T输出VOID,S到T的路径上有负环(即没有最小值)输出UNBOUND..其他有解情况输出最小花费和最小路径长度..

poj 2949 Word Rings 参数搜索+负环探测

类似poj 3621. 代码: //poj 2949 //sep9 #include <iostream> #include <string> #include <map> #include <vector> #include <cmath> using namespace std; const int maxL=1024; const int maxM=100100; char s[maxL]; struct Words { int in,ou

ACM: POJ 3259 Wormholes - SPFA负环判定

POJ 3259 Wormholes Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu 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 pa

Poj 3259 Wormholes 负环判断 SPFA &amp; BellmanFord

#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list> #include <