前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >poj-------(2240)Arbitrage(最短路)

poj-------(2240)Arbitrage(最短路)

作者头像
Gxjun
发布2018-03-26 10:36:32
8000
发布2018-03-26 10:36:32
举报
文章被收录于专栏:mlml

Arbitrage

Time Limit: 1000MS

Memory Limit: 65536K

Total Submissions: 15640

Accepted: 6563

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

代码语言:javascript
复制
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

代码语言:javascript
复制
Case 1: Yes
Case 2: No

Source

Ulm Local 1996

     感觉自己这种算法还是太弱了......唉! 没有用floy之间用bellman_floy来做的,bellman算法其实总结起来就是三点:

      第一: 初始化

      第二: 循环优化求解最大或者最小

     第三 : 检测是否存在负环

代码语言:javascript
复制
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<string>
 5 #include<iostream>
 6 #include<map>
 7 #include<iterator>
 8 using namespace std;
 9 const double inf =-100;
10 const int maxn = 105;
11 struct node
12 {
13  int u,v;
14  double val;
15 };
16 node edge[1000];
17 double dist[maxn];
18 /*松弛状态判别*/
19 bool relax(int u,int v,double val){
20     if(dist[v]<dist[u]*val){
21         dist[v]=dist[u]*val;
22         return 1;
23     }
24     return 0;
25 }
26 bool Bellman(int st,int n,int m){
27     for(int i=1;i<=n;i++){    //初始化
28         dist[i]=inf;
29     }
30     dist[st]=1;
31     bool flag;
32     /*循环优化部分*/
33     for(int i=1; i<n;i++) {
34           flag=false;
35       for(int j=1;j<=m;j++){
36          if(relax(edge[j].u,edge[j].v,edge[j].val))
37              flag=true;
38          }
39        if(!flag) break;
40     }
41     /*检验部分*/
42     for(int i=1;i<=m;i++){
43        if(relax(edge[i].u,edge[i].v,edge[i].val))
44           return 1;   //有负圈
45     }
46     return 0;
47 }
48 
49 int main()
50 {
51     int n,m;
52     map<string ,int> sac;
53     string temp;
54     int test=1;
55     while(scanf("%d",&n)==1&&n!=0){
56             if(!sac.empty())sac.clear();
57         for(int i=1;i<=n;i++){
58            cin>>temp;
59         // sac.insert(pair<string ,int>(temp,i));
60            sac[temp]=i;
61         }
62          cin>>m;
63          double ss;
64          string aa,bb;
65         map<string,int>::iterator p1,p2;
66         for(int i=1 ; i<=m ; i++ ){
67            cin>>aa>>ss>>bb;
68            p1=sac.find(aa);
69            p2=sac.find(bb);
70            edge[i].u=p1->second;
71            edge[i].v=p2->second;
72            edge[i].val=ss;
73         }
74 
75         if(Bellman(1,n,m)) printf("Case %d: Yes\n",test);
76            else printf("Case %d: No\n",test);
77         test++;
78         // printf("%lf\n",dist[1]);
79     }
80     return 0;
81 }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014-08-12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档