题意:给出n个点,m条正权的边,w条负权的边,问是否存在负环

因为Bellman_ford最多松弛n-1次, 因为从起点1终点n最多经过n-2个点,即最多松弛n-1次,如果第n次松弛还能成功的话,则说明存在有负环

 #include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
using namespace std; typedef long long LL;
const int INF = (<<)-;
const int maxn=;
int d[maxn];
int flag;
int nodenum,edgenum,ecnt; struct Edge{
int u,v,cost;
} e[maxn]; int Bellman_ford(){
for(int i=;i<=nodenum;i++) d[i]=INF;
d[]=; for(int i=;i<nodenum;i++){
for(int j=;j<=ecnt;j++){
if(d[e[j].v]>d[e[j].u]+e[j].cost)
d[e[j].v]=d[e[j].u]+e[j].cost;
}
} flag=;
for(int i=;i<=ecnt;i++){
if(d[e[i].v]>d[e[i].u]+e[i].cost){
flag=;
break;
}
}
return flag;
} void addedges(int a,int b,int c){
e[++ecnt].u=a;
e[ecnt].v=b;
e[ecnt].cost=c;
} int main(){
int m,w,ncase;
int a,b,c;
scanf("%d",&ncase);
while(ncase--){
ecnt=;
scanf("%d %d %d",&nodenum,&m,&w);
while(m--){
scanf("%d %d %d",&a,&b,&c);
addedges(a,b,c);
addedges(b,a,c);
}
while(w--){
scanf("%d %d %d",&a,&b,&c);
c=-c;
addedges(a,b,c);
}
if(Bellman_ford()) printf("NO\n");
else printf("YES\n");
}
return ;
}

POJ 3259 Wormholes【Bellman_ford判断负环】的更多相关文章

  1. POJ 3259 Wormholes ( SPFA判断负环 && 思维 )

    题意 : 给出 N 个点,以及 M 条双向路,每一条路的权值代表你在这条路上到达终点需要那么时间,接下来给出 W 个虫洞,虫洞给出的形式为 A B C 代表能将你从 A 送到 B 点,并且回到 C 个 ...

  2. POJ 3259 Wormholes 最短路+负环

    原题链接:http://poj.org/problem?id=3259 题意 有个很厉害的农民,它可以穿越虫洞去他的农场,当然他也可以通过道路,虫洞都是单向的,道路都是双向的,道路会花时间,虫洞会倒退 ...

  3. POJ 3259 Wormholes(SPFA判负环)

    题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是 ...

  4. POJ 3259 Wormholes( bellmanFord判负环)

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36425   Accepted: 13320 Descr ...

  5. POJ 3259 Wormholes (判负环)

    Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46123 Accepted: 17033 Descripti ...

  6. POJ 3259 Wormholes【bellman_ford判断负环——基础入门题】

    链接: http://poj.org/problem?id=3259 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  7. POJ 3259:Wormholes bellman_ford判定负环

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 37906   Accepted: 13954 Descr ...

  8. poj 3259 (Bellman_Ford判断负环)

    题意:John的农场里n块地,m条路连接两块地,k个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts.我们的任务是知道会不会在从某块地出发后又回来,看到了离开之前的自己. 思路:虫洞 ...

  9. Wormholes POJ 3259(SPFA判负环)

    Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...

随机推荐

  1. uva 11090

    I I U P C 2 0 0 6 Problem G: Going in Cycle!! Input: standard input Output: standard output You are ...

  2. C# 调用Windows API实现两个进程间的通信

    使用Windows API实现两个进程间(含窗体)的通信http://blog.csdn.net/huangxinfeng/article/details/5513608 从C#下使用WM_COPYD ...

  3. JMeter使用指南--转

    JMeter使用指南 本文重点介绍JMeter工具在测试中地位以及其中一些难以理解或者手册中含糊不清的感念,读者可以通过本文了解这些概念,然后再根据自己的需要查阅JMeter中各个组件的具体用法来完成 ...

  4. http://blog.sina.com.cn/s/blog_5b9b4abe01017638.html

    http://blog.sina.com.cn/s/blog_5b9b4abe01017638.html

  5. weka平台下手动造.arff的数据

    若数据为 sunny,hot,high,FALSE,nosunny,hot,high,TRUE,noovercast,hot,high,FALSE,yesrainy,mild,high,FALSE,y ...

  6. jqGrid中实现radiobutton的两种做法

    http://blog.sina.com.cn/s/blog_4f925fc30102e27j.html   jqGrid中实现radiobutton的两种做法 ------------------- ...

  7. Android 等比例缩放图片

    // 缩放图片 public static Bitmap zoomImg(String img, int newWidth ,int newHeight){ // 图片源 Bitmap bm = Bi ...

  8. Android Calendar获取年月日时分秒毫秒

    开始使用new Date()测试,并用通过date.getMonth(),和date.getDay()获取,不过后来发现这两个访求是jdk1.1版本的,现在已经不用了,而且结果也不正确. ; int ...

  9. Model的验证

    ModelValidator与ModelValidatorProvider ModelValidator public abstract class ModelValidator { public v ...

  10. Spring Loaded is a JVM agent for reloading class file changes

    <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot ...