poj 2204 hdu 1217 Arbitrage spfa判环 解题报告

来源:互联网 发布:js确定取消对话框 编辑:程序博客网 时间:2024/05/31 04:03

Problem Description
Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency into more than one unit of the same currency. For example, suppose that 1 US Dollar buys 0.5 British pound, 1 British pound buys 10.0 French francs, and 1 French franc buys 0.21 US dollar. Then, by converting currencies, a clever trader can start with 1 US dollar and buy 0.5 * 10.0 * 0.21 = 1.05 US dollars, making a profit of 5 percent. 

Your job is to write a program that takes a list of currency exchange rates as input and then determines whether arbitrage is possible or not.
 

Input
The input file will contain one or more test cases. Om the first line of each test case there is an integer n (1<=n<=30), representing the number of different currencies. The next n lines each contain the name of one currency. Within a name no spaces will appear. The next line contains one integer m, representing the length of the table to follow. The last m lines each contain the name ci of a source currency, a real number rij which represents the exchange rate from ci to cj and a name cj of the destination currency. Exchanges which do not appear in the table are impossible.
Test cases are separated from each other by a blank line. Input is terminated by a value of zero (0) for n. 
 

Output
For each test case, print one line telling whether arbitrage is possible or not in the format "Case case: Yes" respectively "Case case: No". 
 

Sample Input
3USDollarBritishPoundFrenchFranc3USDollar 0.5 BritishPoundBritishPound 10.0 FrenchFrancFrenchFranc 0.21 USDollar3USDollarBritishPoundFrenchFranc6USDollar 0.5 BritishPoundUSDollar 4.9 FrenchFrancBritishPound 10.0 FrenchFrancBritishPound 1.99 USDollarFrenchFranc 0.09 BritishPoundFrenchFranc 0.19 USDollar0
 

Sample Output
Case 1: YesCase 2: No
 

思路:

看能不能赚钱,对每个点spfa,dis表示相对于源点的比,如果源点的值能大于1,则可以。

代码:

#include <cstdio>#include <iostream>#include <cstring>#include <string>#include <algorithm>#include <queue>#include <map>using namespace std;#define INF 0x3f3f3f3f#define MAXN 50map<string,int> M;int n,m,vis[MAXN];double cost[MAXN][MAXN],dis[MAXN];int spfa(int s){    for(int i=1;i<=n;i++)        vis[i]=dis[i]=0;    vis[s]=1;    dis[s]=1;    queue<int> Q;    Q.push(s);    while(!Q.empty())    {        int t=Q.front();        Q.pop();        vis[t]=0;        for(int i=1;i<=n;i++)        {            if(dis[i]<dis[t]*cost[t][i])            {                dis[i]=dis[t]*cost[t][i];                if(dis[s]>1.0) return 0;                if(!vis[i])                {                    vis[i]=1;                    Q.push(i);                }            }        }    }    return 1;}int main(){    string s,e;    double w;    int cas=1;    while(cin>>n,n)    {        M.clear();        memset(cost,0,sizeof(cost));        for(int i=1;i<=n;i++)        {            cost[i][i]=1;            cin>>s;            M[s]=i;        }        cin>>m;        for(int i=1;i<=m;i++)        {            cin>>s>>w>>e;            cost[M[s]][M[e]]=w;        }        int flag=0;        for(int i=1;i<=n;i++)        {            if(spfa(i)==0)            {                flag=1;                break;            }        }        printf("Case %d: %s\n",cas++,flag?"Yes":"No");    }    return 0;}


0 0
原创粉丝点击