POJ - 2240 Arbitrage (bellman-ford, is the right loop problems)

Meaning of the questions: N give you the rate of exchange currency and the name, m currencies, exchange and asked whether there is a way, from the beginning to the end of a currency money, but many exchange complete more cases

Ideas: currency exchange issues in general is seeking the right to issue a positive loop, and POJ-1860 is essentially the same problem, we can use the bellman-ford algorithm to determine, for each point of the cycle by m times relaxation operation, if the last will change, indicating that the circuit is the right appears

In addition, the name of the currency is to look at maps with map

 

Complete code:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<map>
using namespace std;
const int maxe=2500;
const int maxn=50;
int n,m;
map<string,int> G;
int top;
double dis[maxn];
struct Edge {
    int from,to;
    double val;
} edge[maxe];

void add(int from,int to,double val) {
    edge[top].from=from;
    edge[top].to=to;
    edge[top++].val=val;
}
bool Bellman(int s) {
    memset(dis,0,sizeof dis);
    dis[s]=1;
    for(int i=1; i<=n; i++) {
        for(int j=0; j<top; j++) {
            if(DIS [Edge [J] .to] <DIS [Edge [J]. from ] * Edge [J] .val) 
                DIS [Edge [J] .to] = DIS [Edge [J]. from ] * Edge [ J] .val; 
        } 
    } 
    IF (DIS [S]> 1.0 )
         return  to true ;
     the else 
        return  to false ; 
} 
 
int main ( void ) {
     int CNT = 0 ;
     the while (n-CIN >> && n-) { 
        Top = 0 ; 
        G. the Clear (); // TODO: I think it is not no thing 
        stringS, S1, S2;
         for ( int I = . 1 ; I <= n-; I ++ ) 
            CIN >> S, G [S] = I; 
        CIN >> m;
         Double Rate;
         for ( int I = 0 ; I <m ; I ++ ) { 
            CIN >> >> Rate >> S1 S2; 
            the Add (G [S1], G [S2], Rate); 
        } 
        int I;
         for (I = . 1 ; I <= n-; I ++) // because there is the right ring can output "YES", so here we traverse all nodes on any one node. 
            IF (the Bellman (I))
                 BREAK ; 
        COUT<<"Case "<<++cnt<<": ";
        if(i==n+1)
            cout<<"No"<<endl;
        else
            cout<<"Yes"<<endl;
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/Tianwell/p/11297407.html