POJ3259 Wormholes 【Bellmanford推断是否存在负回路】
版权声明:本文为博主原创文章,未经博主同意不得转载。 https://blog.csdn.net/u011775691/article/details/27612757
非常easy的bellmanford题目。这里比較具体:http://blog.csdn.net/lyy289065406/article/details/6645790
直接代码
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
const int SIZE=11111;
const int MAXLEN=1<<30;
struct sss
{
int s,e,v;
}ed[SIZE];
int N,M,WH;
int dis[SIZE];
int ednum;
bool Bellman()
{
for(int i=0;i<N-1;i++)
{
bool flag=false;
for(int j=0;j<ednum;j++)
{
if(dis[ed[j].e]>dis[ed[j].s]+ed[j].v)
{
flag=true;
dis[ed[j].e]=dis[ed[j].s]+ed[j].v;
}
}
if(!flag)
break;
}
for(int j=0;j<ednum;j++)
{
if(dis[ed[j].e]>dis[ed[j].s]+ed[j].v)
{
return true;
}
}
return false;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("G:/1.txt","r",stdin);
freopen("G:/2.txt","w",stdout);
#endif
int f,u,v,w;
cin>>f;
while(f--)
{
ednum=0;
cin>>N>>M>>WH;
for(int i=0;i<SIZE;i++)
{
dis[i]=MAXLEN;
}
for(int i=0;i<M;i++)
{
cin>>u>>v>>w;
ed[ednum].s=u;
ed[ednum].e=v;
ed[ednum++].v=v;
ed[ednum].e=u;
ed[ednum].s=v;
ed[ednum++].v=w;
}
for(int i=0;i<WH;i++)
{
cin>>u>>v>>w;
ed[ednum].s=u;
ed[ednum].e=v;
ed[ednum++].v=-w;
}
if(Bellman())
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
POJ3259 Wormholes 【Bellmanford推断是否存在负回路】的更多相关文章
- poj-3259 Wormholes(无向、负权、最短路之负环判断)
http://poj.org/problem?id=3259 Description While exploring his many farms, Farmer John has discovere ...
- POJ3259——Wormholes(Bellman-Ford+SPFA)
Wormholes DescriptionWhile exploring his many farms, Farmer John has discovered a number of amazing ...
- POJ No 3259 Wormholes Bellman-Ford 判断是否存在负图
题目:http://poj.org/problem?id=3259 题意:主要就是构造图, 然后判断,是否存在负图,可以回到原点 /* 2 3 3 1 //N, M, W 1 2 2 1 3 4 2 ...
- poj3259 Wormholes【Bellman-Ford或 SPFA判断是否有负环 】
题目链接:poj3259 Wormholes 题意:虫洞问题,有n个点,m条边为双向,还有w个虫洞(虫洞为单向,并且通过时间为倒流,即为负数),问你从任意某点走,能否穿越到之前. 贴个SPFA代码: ...
- POJ3259 :Wormholes(SPFA判负环)
POJ3259 :Wormholes 时间限制:2000MS 内存限制:65536KByte 64位IO格式:%I64d & %I64u 描述 While exploring his many ...
- POJ3259Wormholes(判断是否存在负回路)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 38300 Accepted: 14095 Descr ...
- uva558 Wormholes SPFA 求是否存在负环
J - Wormholes Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Stat ...
- POJ 3259 Wormholes(最短路径,求负环)
POJ 3259 Wormholes(最短路径,求负环) Description While exploring his many farms, Farmer John has discovered ...
- Bellman-Ford(可解决负权边)--时间复杂度优化
Bellman-Ford 可解决带有负权边的最短路问题 解决负权边和Dijkstra相比是一个优点,Bellman-Ford的核心代码只有4行:: u[],v[],w[] 分别存一条边的顶点.权值,d ...
随机推荐
- 对Java平台的理解
1) Java是一种面向对象的语言(封装,继承,多态),最显著的特性有两个方面: ----书写一次,到处运行(Write once,run anywhere) 能够非常容易的获得跨平台的能力 --- ...
- bzoj1609 / P2896 [USACO08FEB]一起吃饭Eating Together(最长不降子序列)
P2896 [USACO08FEB]一起吃饭Eating Together 显然的最长不升/降子序列,求出最长值,则答案为$n-$最长值(改掉剩下的). 复杂度$O(nlogn)$ (然鹅有神仙写了$ ...
- 已知圆上三个点坐标,求圆半径 r 和 圆心坐标
问题: 已知圆上三个点坐标分别为(x1,y1).(x2,y2).(x3,y3) 求圆半径R和圆心坐标(X,Y) X,Y,R为未知数,x1,y1,x2,y2,x3,y3为常数 则由圆公式:(x1-X)² ...
- 安装Git【转】
本文转载自:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 最早Git是在Linu ...
- openwrt编译系统制作ubi镜像时会使用系统自带的ubinize还是openwrt编译系统中编译的ubinize
答:使用openwrt编译系统中编译的ubinize,那么这个工具在哪个目录下?在staging_dir/host/bin目录下
- Git WorkBehavior
https://tortoisegit.org/docs/tortoisegit/tgit-dug-showlog.html Repository Demo https://github.com/Ch ...
- Java如何用一行代码初始化ArrayList
参考链接: 1.Initialization of an ArrayList in one line 2.java怎么用一行代码初始化ArrayList
- css可应用的渐进增强新特性
1. 让有滚动行为的元素平滑滚动 scroll-behavior: smooth; <div class="smooth"> </dvi> .smooth ...
- CentOS下安装Python-pip
1.安装epel-release软件包:自动配置yum的软件仓库,弥补centos内容更新有时比较滞后或是一些扩展的源没有. yum -y install epel-release 2.安装pytho ...
- URAL 1106 Two Teams (DFS)
题意 小组里有N个人,每个人都有一个或多个朋友在小组里.将小组分成两个队伍,每个队伍的任意一个成员都有至少一个朋友在另一个队伍. 思路 一开始觉得和前几天做过的一道2-sat(每个队伍任意两个成员都必 ...