Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12918 | Accepted: 5441 |
Description
Input
Output
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
就是求最短路
#include<stdio.h> #include<cmath> #include<string> #include<iostream> using namespace std; int point_num,edge_num; double dict[5000]; struct ed { int sta,end; double time; } edge[5000]; bool bellman() { int i,j; for(i=0;i<=edge_num;i++) dict[i]=0; dict[1]=1; for(i=1;i<point_num;i++) { bool finish=true; for(j=1;j<=edge_num;j++) { int u=edge[j].sta; int v=edge[j].end; double w=edge[j].time; if(dict[v]<dict[u]*w) { dict[v]=dict[u]*w; finish=false; } } if(finish) break; } for(i=1;i<=edge_num;i++) { int u=edge[i].sta; int v=edge[i].end; double w=edge[i].time; if(dict[v]<dict[u]*w) { return true ; } } return false; } int main () { int n,i,j,k,m,flag=1,ci; double rate; string str[50],temp2; char temp[100]; while(scanf("%d",&n)!=EOF&&n) { point_num=n; ci=1; for(i=1;i<=n;i++) { scanf("%s",temp); getchar(); str[i]=temp; } scanf("%d",&m); getchar(); for(i=1;i<=m;i++) { scanf("%s",temp); temp2=temp; for(j=1;j<=n;j++) { if(temp2==str[j]) { break; } } scanf("%lf",&rate); getchar(); scanf("%s",temp); getchar(); temp2=temp; for(k=0;k<n;k++) { if(temp2==str[k]) { break; } } edge[ci].sta=j; edge[ci].end=k; edge[ci].time=rate; ci++; } edge_num=ci-1; if(bellman()) { printf("Case %d: Yes\n",flag++); } else { printf("Case %d: No\n",flag++); } } return 0; }