POJ 2240 Arbitrage

   跟原来的货币交换的题目类似,是说给你n中货币和m中货币之间的交换汇率,问你说能不能使财富增值。这里我用了map。Wrong了两次,一次边集数组开小了,还有就是Yes,No大小写看错了,伤心呐。影响早上的大好心情。Bellman-Ford.

代码:

#include<iostream>
#include<string>
#include<map>
#define maxn 40
using namespace std;
map<string,int> mp;
struct Edge
{
       int v,u;
       double w;
} e[1005];
int size,n;
double dist[maxn];
void AddEdge(int a,int b,double c)
{
     e[size].u=a;
     e[size].v=b;
     e[size].w=c;
     size++;
}
bool Bellman_Ford()
{
     int i,j;
     for( i=1; i<=n; i++){
          bool flag=false;
          for( j=0; j<size; j++){
               // cout<<e[j].u<<' '<<e[j].w<<endl;
               if( dist[e[j].u]*e[j].w>dist[e[j].v]){
                   dist[e[j].v]=dist[e[j].u]*e[j].w;
                   flag=true;
               }
          }
          if( !flag) break;
          if( i==n&&flag) return true;
     }
     return false;
}
int main()
{
    int m,ca,i;
    double c;
    string str1,str2;
    ca=0;
    while( cin>>n&&n){
           ++ca;
           mp.clear();
           for( i=1; i<=n; i++){
                cin>>str1;
                mp[str1]=i;
           }
           cin>>m;
           memset(e,0,sizeof(e));
           size=0;
           while( m--){
                  cin>>str1>>c>>str2;
                  AddEdge(mp[str1],mp[str2],c);
           }
          memset(dist,0,sizeof(dist));
                dist[1]=1;
           cout<<"Case "<<ca<<": ";
           if( Bellman_Ford())
               cout<<"Yes"<<endl;
           else
               cout<<"No"<<endl;
    } 
}


你可能感兴趣的:(c,String)