poj 2240 Arbitrage (最短路 bellman_ford)

题目:http://poj.org/problem?id=2240

题意:给定n个货币名称,给m个货币之间的汇率,求会不会增加

  和1860差不多,求有没有正环

 刚开始没对,不知道为什么用 double往结构体里传值的时候 会去掉小数点后的 数

 1 #include <iostream>
 2  #include<cstdio>
 3  #include<cstring>
 4  #include<cstdlib>
 5  #include<stack>
 6  #include<queue>
 7  #include<iomanip>
 8  #include<cmath>
 9  #include<algorithm>
10  #include<map>
11  using namespace std;
12  
13  struct node
14  {
15      int u,v;
16      double w;
17  }edge[10010];
18  int cnt,n;
19  double dis[150];
20  
21  bool bellman_ford(int s)
22  {
23      int i,j;
24      memset(dis,0,sizeof(dis));
25      dis[s] = 1;
26  
27      for(i=0; i<n-1; i++)
28      for(j=0; j<cnt; j++)
29      {
30          int u=edge[j].u;
31          int  v=edge[j].v;
32          double w=edge[j].w;
33          if(dis[v]<dis[u]*w)
34          dis[v]=dis[u]*w;
35      }
36      for(j=0; j<cnt; j++)
37      {
38          int u=edge[j].u;
39          int v=edge[j].v;
40          double w=edge[j].w;
41          if(dis[v]<dis[u]*w)
42           return true;
43      }
44      return false;
45  }
46  int main()
47  {
48      map<string,int>mp;
49      char s[50],s2[50],x=1;
50      int m,i,j;
51      while(cin>>n&&n)
52      {
53          cnt=0;
54          for(i=0; i<n; i++)
55          {
56              cin>>s;
57              mp[s]=i;
58          }
59           cin>>m;
60           for(i=1; i<=m; i++)
61           {
62                cin>>s; scanf("%lf",&edge[cnt].w); cin>>s2;
63                edge[cnt].u=mp[s]; edge[cnt].v=mp[s2];
64                cnt++;
65           }
66  
67           if(bellman_ford(0)) printf("Case %d: Yes\n",x++);
68           else printf("Case %d: No\n",x++);
69      }
70      return 0;
71  }
72  

 

posted @ 2013-08-12 10:24  水门  阅读(194)  评论(0)    收藏  举报