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

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

Source

 
     感觉自己这种算法还是太弱了......唉! 没有用floy之间用bellman_floy来做的,bellman算法其实总结起来就是三点:
      第一: 初始化
      第二: 循环优化求解最大或者最小
     第三 : 检测是否存在负环

 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<iostream>
#include<map>
#include<iterator>
using namespace std;
const double inf =-;
const int maxn = ;
struct node
{
int u,v;
double val;
};
node edge[];
double dist[maxn];
/*松弛状态判别*/
bool relax(int u,int v,double val){
if(dist[v]<dist[u]*val){
dist[v]=dist[u]*val;
return ;
}
return ;
}
bool Bellman(int st,int n,int m){
for(int i=;i<=n;i++){ //初始化
dist[i]=inf;
}
dist[st]=;
bool flag;
/*循环优化部分*/
for(int i=; i<n;i++) {
flag=false;
for(int j=;j<=m;j++){
if(relax(edge[j].u,edge[j].v,edge[j].val))
flag=true;
}
if(!flag) break;
}
/*检验部分*/
for(int i=;i<=m;i++){
if(relax(edge[i].u,edge[i].v,edge[i].val))
return ; //有负圈
}
return ;
} int main()
{
int n,m;
map<string ,int> sac;
string temp;
int test=;
while(scanf("%d",&n)==&&n!=){
if(!sac.empty())sac.clear();
for(int i=;i<=n;i++){
cin>>temp;
// sac.insert(pair<string ,int>(temp,i));
sac[temp]=i;
}
cin>>m;
double ss;
string aa,bb;
map<string,int>::iterator p1,p2;
for(int i= ; i<=m ; i++ ){
cin>>aa>>ss>>bb;
p1=sac.find(aa);
p2=sac.find(bb);
edge[i].u=p1->second;
edge[i].v=p2->second;
edge[i].val=ss;
} if(Bellman(,n,m)) printf("Case %d: Yes\n",test);
else printf("Case %d: No\n",test);
test++;
// printf("%lf\n",dist[1]);
}
return ;
}

最新文章

  1. 玩转 Linux 系统的方法论
  2. Atitit.css 规范 bem &#160;项目中 CSS 的组织和管理
  3. property animation ( NineOldAndroid )
  4. flume-ng配置文档简单说明
  5. 转: 最值得阅读学习的 10 个 C 语言开源项目代码
  6. Fastreport使用经验(转)在Delphi程序中访问报表对象
  7. PHP之SQL防注入代码(360提供)
  8. get_category_recommend_goods的正确使用
  9. RSA简介(一)——数论原理
  10. 视音频编解码学习工程:H.264分析器
  11. Maven部署项目到Tomcat
  12. 使用catboost解决ML中高维度不平衡数据集挑战的解决方案
  13. mongodb $用法,等
  14. include_path=&#39;.;C:\php5\pear&#39;解决方法
  15. Excel 2010 得到当天的日期/得到一年中的第几周/得到当前一周中的星期几
  16. 因子和(luoguP1593)(等比数列求和+逆元)
  17. Struts全局跳转
  18. Hive常用函数
  19. atitit.解决SyntaxError: missing ] after element list&quot;不个object 挡成个str eval ....
  20. gossip版本raft算法实现

热门文章

  1. Django中的分页
  2. [UVa1213]Sum of Different Primes(递推,01背包)
  3. sql 语句 嵌套子查询 执行顺序分析
  4. emacs学习
  5. 使用ansible批量管理远程服务器
  6. Using SSH on Linux
  7. hdu 5925 Coconuts 离散化+dfs
  8. Linux命令之乐--awk
  9. 基本分类方法——KNN(K近邻)算法
  10. [html] HTML结构的语义化