POJ 1860 Bellman-Ford反向 求正权回路

//题目的意思就是找正权回路
//我们知道Bellman-Ford可以标记负权回路
//我们不妨把Belliman-Ford的不等式反写
//就可以标记正权回路了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxe=210;
const int maxv=110;
int cnt;
double dis[maxv];
struct Edge {
    int from,to;
    double rate,commission;
} edge[maxe];
void init() {
    cnt=0;
    memset(dis,0,sizeof dis);
}
void addedge(int from,int to,double rate,double commission) {
    edge[cnt].from=from;
    edge[cnt].to=to;
    edge[cnt].rate=rate;
    edge[cnt++].commission=commission;
}
bool bellman_ford(int s,double v,int n,int cnt,Edge edge[],double dist[]) {
    bool flag;
    dist[s]=v;
    for(int j=0; j<n+1; j++) {
        flag=false;
        for(int i=0; i<cnt; i++) {
            if(dist[edge[i].to]<(dist[edge[i].from]-edge[i].commission)*edge[i].rate) {
                dist[edge[i].to]=(dist[edge[i].from]-edge[i].commission)*edge[i].rate;
                flag=true;
            }
        }
        if(flag==false)
            break;
    }
    return flag;
}

int main(void) {
#ifndef ONLINE_JUDGE
    freopen("E:\\input.txt","r",stdin);
#endif // ONLINE_JUDGE
    ios::sync_with_stdio(false);
    int n,m,s,a,b;
    double v,rab,cab,rba,cba;
    while(cin>>n>>m>>s>>v) {
        init();
        for(int i=0; i<m; i++) {
            cin>>a>>b>>rab>>cab>>rba>>cba;
            addedge(a,b,rab,cab);
            addedge(b,a,rba,cba);

        }
        if(bellman_ford(s,v,n,cnt,edge,dis))
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }
    return 0;
}

 

    原文作者:Bellman - ford算法
    原文地址: https://blog.csdn.net/shadandeajian/article/details/81284621
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞