poj 3259 bellman-ford判断是否存在负权回路

//
//  main.cpp
//  poj3259
//
//  Created by Fangpin on 15/5/28.
//  Copyright (c) 2015年 FangPin. All rights reserved.
//

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
int g[600][600];
struct Edge{
    int from,to,dist;
};
int n,d[1000];
vector<Edge> vec;

bool bf(){
    for(int i=0;i<1000;++i)
        d[i]=10000000;
    d[1]=0;
    for(int i=0;i<n-1;++i){
        bool flag=false;
        for(int j=0;j<vec.size();++j){
            Edge &e=vec[j];
            if(d[e.to]>d[e.from]+e.dist){
                d[e.to]=d[e.from]+e.dist;
                flag=true;
            }
        }
        if(!flag) break;
    }
    for(int i=0;i<vec.size();++i){
        Edge &e=vec[i];
        if(d[e.to]>d[e.from]+e.dist)
            return false;
    }
    return true;
}
int main(int argc, const char * argv[]) {
    int t;
    cin>>t;
    while(t--){
        int m,k;
        vec.clear();
        cin>>n>>m>>k;
        memset(g,0,sizeof(g));
        for(int i=1;i<=m;++i){
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            Edge e;
            e.from=a;
            e.to=b;
            e.dist=c;
            vec.push_back(e);
            swap(e.from,e.to);
            vec.push_back(e);
        }
        for(int i=1;i<=k;++i){
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            c=-c;
            Edge e;
            e.from=a;
            e.to=b;
            e.dist=c;
            vec.push_back(e);
        }
        if(bf()) cout<<"NO\n";
        else cout<<"YES\n";
    }
    return 0;
}

时间: 2024-08-22 03:42:15

poj 3259 bellman-ford判断是否存在负权回路的相关文章

[ACM] POJ 3259 Wormholes (bellman-ford最短路径,判断是否存在负权回路)

Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 29971   Accepted: 10844 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 Wormholes 【最短路之负权环的判断】

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

poj3259 bellman——ford Wormholes解绝负权问题

Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 35103   Accepted: 12805 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(可判负权回路+记录路径)

#include<iostream> #include<cstdio> using namespace std; #define MAX 0x3f3f3f3f #define N 1010 int nodenum, edgenum, original; //点,边,起点 typedef struct Edge //边 { int u, v; int cost; }Edge; Edge edge[N]; int dis[N], pre[N]; bool Bellman_Ford()

poj 3259 bellman最短路判断有无负权回路

Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36717   Accepted: 13438 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 Wormholes(最短路,判断有没有负环回路)

F - Wormholes Time Limit:2000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u SubmitStatus Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a on

poj 3259(bellman最短路径)

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

图论 --- spfa + 链式向前星 : 判断是否存在正权回路 poj 1860 : Currency Exchange

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

poj 1860 Currency Exchange (SPFA、正权回路 bellman-ford)

链接:poj 1860 题意:给定n中货币,以及它们之间的税率,A货币转化为B货币的公式为 B=(V-Cab)*Rab,其中V为A的货币量, 求货币S通过若干此转换,再转换为原本的货币时是否会增加 分析:这个题就是判断是否存在正权回路,可以用bellman-ford算法,不过松弛条件相反 也可以用SPFA算法,判断经过转换后,转换为原本货币的值是否比原值大... bellman-ford    0MS #include<stdio.h> #include<string.h> str