poj 3259 虫洞 & poj 1860 货币兑换 bellman-ford解法

Wormholes

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 58342 Accepted: 21816

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 path that delivers you to its destination at a time that is BEFORE you entered the wormhole! Each of FJ’s farms comprises N (1 ≤ N ≤ 500) fields conveniently numbered 1..NM (1 ≤ M ≤ 2500) paths, and W (1 ≤ W ≤ 200) wormholes.

As FJ is an avid time-traveling fan, he wants to do the following: start at some field, travel through some paths and wormholes, and return to the starting field a time before his initial departure. Perhaps he will be able to meet himself :) .

To help FJ find out whether this is possible or not, he will supply you with complete maps to F (1 ≤ F ≤ 5) of his farms. No paths will take longer than 10,000 seconds to travel and no wormhole can bring FJ back in time by more than 10,000 seconds.

Input

Line 1: A single integer, 
F
F farm descriptions follow. 

Line 1 of each farm: Three space-separated integers respectively: 
N
M, and 
W 

Lines 2..
M+1 of each farm: Three space-separated numbers (
S
E
T) that describe, respectively: a bidirectional path between 
S and 
E that requires 
T seconds to traverse. Two fields might be connected by more than one path. 

Lines 
M+2..
M+
W+1 of each farm: Three space-separated numbers (
S
E
T) that describe, respectively: A one way path from 
S to 
E that also moves the traveler back 
T seconds.

Output

Lines 1..
F: For each farm, output “YES” if FJ can achieve his goal, otherwise output “NO” (do not include the quotes).

Sample Input

2
3 3 1
1 2 2
1 3 4
2 3 1
3 1 3
3 2 1
1 2 3
2 3 4
3 1 8

Sample Output

NO
YES

题意:有N个地块,地块间有M条双向道路(path)和W个虫洞(worm hole),path连接两个地块行走需要T时间,worm hole同样连接两个地块但为单向,穿过虫洞会回溯T时间,问是否存在可行解使从一个地块出发回到该地块时比出发的时刻早?

由于出发地块不限,使用bellmanford求负环即可。

#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<iomanip>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
const int MAXINT=0x3f3f3f3f;
int f, n, m, w, ne;
int d[505];
struct {
    int beg,end,len;
}edge[5205];
void adde(int s,int e,int t){
    edge[ne].beg=s;
    edge[ne].end=e;
    edge[ne].len=t;
    ne++;
}
bool relax(int p){ // 松弛
    int t=d[edge[p].beg]+edge[p].len;
    if(d[edge[p].end]>t){
        d[edge[p].end]=t;
        return true;
    }
    return false;
}
int bellmanford(int st)
{
    bool flag;
    for(int i=1;i<=n;i++){
        d[i]=MAXINT;
    }   d[st]=0;
    for(int i=0;i<n;i++){
        flag=false;
        for(int j=0;j<ne;j++){
            if(relax(j)) flag=true;
        }
        if(d[st]<0) return 1;
        if(!flag) return 2;
    }
    for(int i=0;i<ne;i++) // 判断负环
        if(relax[i]) return 1;
    return 3;
}
int main()
{
    int s,e,t;
    cin>>f;
    for(int _=0;_<f;_++){
        ne=0;
        cin>>n>>m>>w;
        for(int i=0;i<m;i++){
            cin>>s>>e>>t;
            adde(s,e,t);
            adde(e,s,t);
        }
        for(int i=0;i<w;i++){
            cin>>s>>e>>t;
            adde(s,e,-t);
        }
        for(int i=1;i<=n;i++){ //遍历所有出发地块
            if(bellmanford(i)==1){
                cout<<"YES"<<endl;
                break;
            }
            if(i==n)
                cout<<"NO"<<endl;
        }
    }
    return 0;
}

Currency Exchange

Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 34150 Accepted: 13099

Description

Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can be several points specializing in the same pair of currencies. Each point has its own exchange rates, exchange rate of A to B is the quantity of B you get for 1A. Also each exchange point has some commission, the sum you have to pay for your exchange operation. Commission is always collected in source currency. 

For example, if you want to exchange 100 US Dollars into Russian Rubles at the exchange point, where the exchange rate is 29.75, and the commission is 0.39 you will get (100 – 0.39) * 29.75 = 2963.3975RUR. 

You surely know that there are N different currencies you can deal with in our city. Let us assign unique integer number from 1 to N to each currency. Then each exchange point can be described with 6 numbers: integer A and B – numbers of currencies it exchanges, and real R
AB, C
AB, R
BA and C
BA – exchange rates and commissions when exchanging A to B and B to A respectively. 

Nick has some money in currency S and wonders if he can somehow, after some exchange operations, increase his capital. Of course, he wants to have his money in currency S in the end. Help him to answer this difficult question. Nick must always have non-negative sum of money while making his operations. 

Input

The first line of the input contains four numbers: N – the number of currencies, M – the number of exchange points, S – the number of currency Nick has and V – the quantity of currency units he has. The following M lines contain 6 numbers each – the description of the corresponding exchange point – in specified above order. Numbers are separated by one or more spaces. 1<=S<=N<=100, 1<=M<=100, V is real number, 0<=V<=10
3

For each point exchange rates and commissions are real, given with at most two digits after the decimal point, 10
-2<=rate<=10
2, 0<=commission<=10
2

Let us call some sequence of the exchange operations simple if no exchange point is used more than once in this sequence. You may assume that ratio of the numeric values of the sums at the end and at the beginning of any simple sequence of the exchange operations will be less than 10
4

Output

If Nick can increase his wealth, output YES, in other case output NO to the output file.

Sample Input

3 2 1 20.0
1 2 1.00 1.00 1.00 1.00
2 3 1.10 1.00 1.10 1.00

Sample Output

YES

题意:有N种货币,M个货币兑换点,每个兑换点仅可在两种货币之间相互兑换,兑换会扣除原货币手续费C,之后乘以汇率R,即形如(100USD – 0.39USD) * 29.75 = 2963.3975RUR,USD至RUR的C=0.39, R=29.75。现有N单位第S种货币的初始资金,问是否有某种兑换方式使兑换回第S种货币时比初始资金更多。

想一想就会发现与3259形式很相似,在此处“兑换点”其实是兑换边,图中的点为不同种类的货币。而此处我们要找的是正环,改写松弛函数relax即可。

#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<iomanip>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#define MAXINT 0x3f3f3f3f
using namespace std;
bool vis[105];
double dist[105];
int n, m, s;
double v;
struct {
    int beg, end;
    double r,c;
}edge[205];
void check(){    //只是一个用来监控的函数,无意义
    for(int i=1;i<=n;i++)
        cout<<dist[i]<<" ";
    cout<<endl;
    return;
}
bool relax(int p){ //p denotes an edge
    double t=(dist[edge[p].beg]-edge[p].c)*edge[p].r;
    if(t>dist[edge[p].end]){    //比已知方案数额大,则更新
        dist[edge[p].end]=t;
        //cout<<"dist "<<edge[p].end<<" = "<<t<<endl;
        //cout<<edge[p].c<<" "<<edge[p].r<<endl;
        return true;
    }
    return false;
}
bool bellmanford(){
    bool flag;
    for(int i=0;i<=n;i++)
        dist[i]=0;
    dist[s]=v;                    //初始点为开始所持有的货币
    for(int i=0;i<n-1;i++){
        flag=false;
        for(int j=0;j<2*m;j++){
            if(relax(j)&&!flag) flag=true;
        }
        //check();
        if(dist[s]>v) return true; //若初始持有货币增加,结束
        if(!flag) return false;
    }
    for(int i=0;i<2*m;i++){    //若有正环,结束
        if(relax(i)) return true;
    }
    return false;
}
void adde(int i,int beg,int end,double r,double c){
    edge[i].beg=beg;
    edge[i].end=end;
    edge[i].r=r;
    edge[i].c=c;
    return;
}
int main()
{
    int a,b;
    double rab,cab,rba,cba;
    cin>>n>>m>>s>>v;
    for(int i=0;i<m;i++){
        cin>>a>>b>>rab>>cab>>rba>>cba;
        adde(2*i,a,b,rab,cab);    //双向边
        adde(2*i+1,b,a,rba,cba);
    }
    if(bellmanford())   cout<<"YES"<<endl;
    else    cout<<"NO"<<endl;
    return 0;
}

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