Arbitrage
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 30790   Accepted: 12761

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

用spfa解决POJ2240,因学习spfa 的时间较短,犯了不少错误,在代码行中,会注释出来以备察看一些新手可能需要注意的地方,接下来看题意....

题意:给你一些钱币,看你是否能通过汇率转换,赚到更多的钱。

例如给出   :

  我们看第一个样例,1美元可以兑换0.5英镑,1英镑可以兑换10法郎,但1法郎只能兑换0.21美元。

  那么如果你有1美元,我们先兑换成英镑,在兑换成法郎,最后兑换成美元  == 1*0.5*10*0.21=1.05美元,,,明显看出,经过一系列的兑换之后,我们的金钱更多了....那么再看第二个样例, 我们显然可以知道,此题的题意就是让我们求出一条兑换钱币的次序,或者说路径,看是否能有一条路径兑换之后,我们的钱币比原来更多了。

  那么接下来看代码吧..


#include <stdio.h>
#include <memory.h>
#include <string.h>
#include <queue>
#define MAXN 31
int n,m,head[MAXN],vis[MAXN],cnt[MAXN];
char name[][];  //用来读入各种货币的名称
using namespace std;
typedef struct
{
int start,end;
int next;
double w;
} Edge;
Edge edges[];
double dis[MAXN]; int Find(char str1[])
{
for(int i=; i<=n; i++)
if(strcmp(name[i],str1)==)
return i;//没有考虑找不到的情况,但是实际上,题目不会给出找不到的货币名称....
return -;
} bool spaf(int s)
{
queue<int>que;
memset(dis,,sizeof(dis));
memset(vis,false,sizeof(vis));
memset(cnt,,sizeof(cnt));
vis[s]=dis[s]=;    //这里一开始学习spfa的时候,还保留着用其他方法的习惯,总给vis[1]置为1 ,,但其实这里应该写成vis[s]=1,不然题目就会出现一些小错误,导致过了样例,却还是wa了
que.push(s);
while(!que.empty())
{
int temp=que.front();
que.pop();
vis[temp]=;
for(int i=head[temp]; i!=-; i=edges[i].next)
{int x=edges[i].start,y=edges[i].end;
double w=edges[i].w;
if(dis[y]<dis[x]*w)
{
dis[y]=dis[x]*w;
if(!vis[y])
{
vis[y]=;
if(++cnt[y]>n)    //为了防止某一个节点无限被调用,形成环路,则可以一直赚钱,卡在这里
return true;
que.push(y);
}
}
}
}
return false;
}
int main()
{
int len=;
char start[],end[];
while(scanf("%d",&n)&&n!=)
{
getchar();
for(int i=; i<=n; ++i)
gets(name[i]);
scanf("%d",&m);
memset(head,-,sizeof(head));
for(int i=; i<m; ++i)
{
scanf(" %s %lf %s",start,&edges[i].w,end);
edges[i].start=Find(start);
edges[i].end=Find(end);
edges[i].next=head[edges[i].start];
head[edges[i].start]=i;
}
m=;
for(int i=; i<=n; ++i)
{
if(spaf(i)==true)    
{
m=;
break;
}
}
if(m)
printf("Case %d: Yes\n",len++);
else
printf("Case %d: No\n",len++);
}
return ;
}

最新文章

  1. 算法-MergeSort
  2. POJ 1804 Brainman(归并排序)
  3. sehll_if
  4. Objective-C语言分类与协议
  5. hadoop 异常及处理总结-01(小马哥-原创)
  6. WCF服务运行一段时间后客户端无法连接WCF服务的解决办法 (转)
  7. P1894セチの祈り
  8. jquery插件autocomplete
  9. 在chart上加入一条指示线
  10. Image控件
  11. [Windows Phone] 如何在 Windows Phone 应用程式制作市集搜寻
  12. C# 通过扩展WebBrowser捕获网络连接错误信息
  13. jdk环境变量配置及配置原因
  14. bug终结者 团队作业第三周
  15. GODOT 3.0 开发进度汇报 #7
  16. docker被入侵后.............
  17. 高并发数据库之MySql性能优化实战总结
  18. 【Web API系列教程】3.3 — 实战:处理数据(建立数据库)
  19. tomcat 项目发布方式
  20. 502 解决:[WARNING] fpm_children_bury

热门文章

  1. Spring Cloud Eureka(一): 开篇说明及目录汇总
  2. nuxt使用教程
  3. mysql 1045 - Access denied for user &#39;root&#39;@&#39;*.*.*.*&#39; (using password YES)
  4. Ubuntu 16.04 一键安装P4开发环境记录
  5. SSM整合(自己收藏)
  6. gitolite 代码访问控制
  7. L1-049 天梯赛座位分配 (20 分)
  8. 因OpenCV版本不一致所引发的报错
  9. leetcode84 柱状图
  10. nginx 反向代理实现负载均衡*理论