题目描述:

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 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

3
USDollar
BritishPound
FrenchFranc
3
USDollar 0.5 BritishPound
BritishPound 10.0 FrenchFranc
FrenchFranc 0.21 USDollar

3
USDollar
BritishPound
FrenchFranc
6
USDollar 0.5 BritishPound
USDollar 4.9 FrenchFranc
BritishPound 10.0 FrenchFranc
BritishPound 1.99 USDollar
FrenchFranc 0.09 BritishPound
FrenchFranc 0.19 USDollar

0

Sample Output

Case 1: Yes
Case 2: No

题目大意:根据不同货币之间不同汇率转换,看能否将起初的金额增加,其实与之前的 正权回路例题意思一样 链接:之前的正确回路问题

题目思路:与之前做法类似,只不过算法公式换了而已,将之前的算法公式转化为,dis[u]*edge[i].w>dis[u]即可。建议如果为了刷题而进来的童鞋们,先不要看代码自己实现一下哦。提交网址

具体实现:这个题与之前类似就不说了昂,也就是这个题的输入比较复杂,我是用map处理的,或者与之前的一篇文章类似,可以将字符串转为数组下标,要写一个pos函数:pos函数链接例题,这样之后构图就可以了,把所有边权值加好,图构建完成之后,跑一遍spfa判正圈就可以了。

代码实现:

spfa代码+注释:

/*从来不向它们低头,永不放弃*/
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <map>
#include <string>
#include <iostream>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn=1e6+5;
const int INF=1e9;
struct node{
    int e;
    double rate;
    int next;
}edge[maxn];
int head[maxn];
int vis[maxn];
double dis[maxn];
ll cnt=0;
void restart()
{
    cnt=0;
    memset(head,-1,sizeof(head));
}
void addedge(int u,int v,double w)
{
    edge[cnt].e=v;
    edge[cnt].rate=w;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
bool spfa(int n)
{
    for(int i=1;i<=n;i++) dis[i]=0,vis[i]=0;
    queue<int>q;
    q.push(1);
    dis[1]=1;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int e=edge[i].e;
            double x=dis[u]*edge[i].rate;
            if(dis[e]-x<0)
            {
                dis[e]=x;
                q.push(e);
                vis[e]++;
                if(vis[e]>n+1) return true;
            }
        }
    }
    if(dis[1]-1.0>0) return true;//不要忘记这个条件
    return false;
}
int main()
{
    int n,m;
    int cnt=0;
    while(scanf("%d",&n)&&n)
    {
        restart();
        int ans=0;
        map<string,int>mp;
        for(int i=1;i<=n;i++)
        {
            char s[1500];
            scanf("%s",s);
            if(!mp[s]) mp[s]=++ans;
        }
        scanf("%d",&m);
        for(int i=1;i<=m;i++)
        {
            char s1[1500],s2[1500];double rate;
            scanf("%s%lf%s",s1,&rate,s2);
            addedge(mp[s1],mp[s2],rate);
        }
        printf("Case %d: %s\n",++cnt,spfa(n)?"Yes":"No");
    }
    return 0;
}