描述
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.

 
输入
The input file 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.
输出
For each test case, print one line telling whether arbitrage is possible or not in the format "Case case: Yes" respectively "Case case: No".
样例输入
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
样例输出
Case 1: Yes
Case 2: No
来源
NKOJ or 1996/97 Ulm Internal Contest
上传者
苗栋栋

题意:给出一些货币和货币之间的兑换比率,问是否可以使某种货币经过一些列兑换之后,货币值增加。举例说就是1美元经过一些兑换之后,超过1美元。可以输出Yes,否则输出No。

AC代码:

 #include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <queue>
using namespace std;
#define INF 0x3f3f3f3f
#define MAX 111 double mp[MAX][MAX];
int n,m; void floyd()
{
for(int k=; k<=n; k++)
for(int i=; i<=n; i++)
for(int j=; j<=n; j++)
if(mp[i][j]< mp[i][k]*mp[k][j])
mp[i][j]=mp[i][k]*mp[k][j];
} void init()
{
for(int i=; i<=n; i++){
for(int j=; j<=n; j++){
if(i==j)
mp[i][j]=;
else
mp[i][j]=;
}
}
} int main()
{
int sum=;
double rate;
char a[],b[],c[];
while(~scanf("%d",&n)&&n){
init();
map<string,int> mmp;
for(int i=; i<=n; i++){
scanf("%s",a);
mmp[a]=i;
}
scanf("%d",&m);
for(int i=; i<=m; i++){
scanf("%s%lf%s",b,&rate,&c);
int x=mmp[b];
int y=mmp[c];
mp[x][y]=rate;
//printf("%d\n",mp[x][y]);
}
floyd();
int flag=;
for(int i=; i<=n; i++){
//printf("%d\n",mp[i][i]);
if(mp[i][i]>){
flag=;
break;
}
}
printf("Case %d: ",++sum);
printf("%s\n",flag ? "Yes" : "No");
}
}

SPFA:

 #include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <queue>
using namespace std;
#define INF 0x3f3f3f3f
#define MAX 111 int n, m;
double dis[MAX], mp[MAX][MAX];
struct node
{
char name[];
}a[MAX]; int find(char *s)
{
for(int i = ; i < n; i++)
if(strcmp(a[i].name, s) == )
return i;
} int SPFA(int p)
{
queue<int> q;
bool vis[MAX];
memset(dis,,sizeof(dis));
memset(vis, , sizeof(vis));
while(!q.empty())
q.pop();
dis[p] = ;
vis[p] = ;
q.push(p);
while(!q.empty())
{
int x = q.front();
q.pop();
vis[x] = false;
for(int i = ; i < n; i++)
{
if(dis[i] < dis[x] * mp[x][i])
{
dis[i] = dis[x] * mp[x][i];
if(dis[p] > 1.0)
return ;
if(!vis[i])
{
vis[i] = true;
q.push(i);
}
}
}
}
return ;
} int main()
{
int i, j, cas = ;
char s1[], s2[];
double s;
while(~scanf("%d",&n) && n)
{
for(i = ; i < n; i++)
{
for(j = ; j < n; j++)
{
if(i == j)
mp[i][j] = ;
else
mp[i][j] = ;
}
}
for(i = ; i < n; i++)
scanf("%s",a[i].name);
scanf("%d",&m);
for(i = ; i < m; i++)
{
scanf("%s%lf%s",s1, &s, s2);
int u = find(s1), v = find(s2);
mp[u][v] = s;
}
int flag = ;
for(i = ; i < n; i++)
{
if(SPFA(i) == )
{
flag = ;
break;
}
}
printf("Case %d: ",++cas);
printf("%s\n", flag ? "Yes" : "No");
}
return ;
}

Bellman_Ford代码(hdu  可过):

 #include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <queue>
using namespace std;
#define INF 0x3f3f3f3f
#define MAX 111 struct node
{
int x,y;
double rate;
}e[MAX]; int n,m,v;
bool flag;
double dis[MAX]; bool Bellman_Ford(int p)
{
memset(dis,,sizeof(dis));
dis[p]=;
for(int j=; j<n; j++)
for(int i=; i<v; i++)
{
if(dis[e[i].y] < dis[e[i].x] * e[i].rate)
dis[e[i].y] = dis[e[i].x] * e[i].rate;
}
//for(int i=0; i<v; i++)
// printf("%d\n",dis[e[i].y]);
for(int i = ; i<v; i++)
if(dis[e[i].y] < dis[e[i].x] * e[i].rate)
return true;
return false;
} int main()
{
int sum=;
char a[], b[], c[];
double rate;
while(~scanf("%d",&n)&&n){
v=;
map<string,int> mp;
for(int i=; i<=n; i++){
scanf("%s",a);
mp[a]=i;
}
scanf("%d",&m);
for(int i=; i<=m; i++){
scanf("%s%lf%s",b,&rate,c);
int x=mp[b];
int y=mp[c];
e[v].x=x;
e[v].y=y;
e[v++].rate=rate;
}
flag=Bellman_Ford();
if (flag)
printf("Case %d: Yes\n",++sum);
else
printf("Case %d: No\n", ++sum);
}
}

Nyoj Arbitrage(Floyd or spfa or Bellman-Ford)的更多相关文章

  1. ACM/ICPC 之 最短路径-Bellman Ford范例(POJ1556-POJ2240)

    两道Bellman Ford解最短路的范例,Bellman Ford只是一种最短路的方法,两道都可以用dijkstra, SPFA做. Bellman Ford解法是将每条边遍历一次,遍历一次所有边可 ...

  2. poj1860 bellman—ford队列优化 Currency Exchange

    Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 22123   Accepted: 799 ...

  3. uva 558 - Wormholes(Bellman Ford判断负环)

    题目链接:558 - Wormholes 题目大意:给出n和m,表示有n个点,然后给出m条边,然后判断给出的有向图中是否存在负环. 解题思路:利用Bellman Ford算法,若进行第n次松弛时,还能 ...

  4. Bellman—Ford算法思想

    ---恢复内容开始--- Bellman—Ford算法能在更普遍的情况下(存在负权边)解决单源点最短路径问题.对于给定的带权(有向或无向)图G=(V,E),其源点为s,加权函数w是边集E的映射.对图G ...

  5. Bellman - Ford 算法解决最短路径问题

    Bellman - Ford 算法: 一:基本算法 对于单源最短路径问题,上一篇文章中介绍了 Dijkstra 算法,但是由于 Dijkstra 算法局限于解决非负权的最短路径问题,对于带负权的图就力 ...

  6. 一个人的旅行(floyd+dijskra+SPFA+Bellman)

    一个人的旅行 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  7. POJ 2240 Arbitrage (Bellman Ford判正环)

    Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:27167   Accepted: 11440 Descri ...

  8. 图论算法——最短路径Dijkstra,Floyd,Bellman Ford

    算法名称 适用范围 算法过程 Dijkstra 无负权 从s开始,选择尚未完成的点中,distance最小的点,对其所有边进行松弛:直到所有结点都已完成 Bellman-Ford 可用有负权 依次对所 ...

  9. 最短路知识点总结(Dijkstra,Floyd,SPFA,Bellman-Ford)

    Dijkstra算法: 解决的问题: 带权重的有向图上单源最短路径问题.且权重都为非负值.如果采用的实现方法合适,Dijkstra运行时间要低于Bellman-Ford算法. 思路: 如果存在一条从i ...

随机推荐

  1. composite template 组合模式

      1. 主要优点 组合模式的主要优点如下: (1) 组合模式可以清楚地定义分层次的复杂对象,表示对象的全部或部分层次,它让客户端忽略了层次的差异,方便对整个层次结构进行控制. (2) 客户端可以一致 ...

  2. Linux学习笔记——举例说,makefile 多个文件

    0.前言     从学习C语言開始就慢慢開始接触makefile,查阅了非常多的makefile的资料但总感觉没有真正掌握makefile,假设自己动手写一个makefile总认为非常吃力. 所以特意 ...

  3. 直接插入排序---java实现

    思路:遍历无序的原数组,把第i个的后一个即i+1去与前面的i个逐个比较... 解法一: package com.sheepmu.text; import java.util.Arrays; /* * ...

  4. UVA How Big Is It?

    题目例如以下: How Big Is It?  Ian's going to California, and he has to pack his things, including hiscolle ...

  5. 微信电脑版(Mac和Windows)安装

    内容简介 1.微信Windows版 2.微信Mac版 3.总结优势 微信电脑版 众所周知,腾讯公司(马化腾先生执掌的巨头公司)开发的超成功App:微信.一经推出便引发业界轰动,使用人数更是直逼QQ. ...

  6. Nagios显示器mysql定从库: libmysqlclient.so.18: cannot open shared object file: No such

    做mysql的slave时间监控,必须check_mysql文字,check当误差: error while loading shared libraries: libmysqlclient.so.1 ...

  7. Hello ASP.NET5

    2015年11月30日, ASP.NET 5 RC1 已经发布,本文尝试了一下ASP.NET5项目的创见一发布到IIS.开发环境,win10 64位,visual studio2015(已更新upda ...

  8. 深入理解学习Git工作流(转)

    个人在学习git工作流的过程中,从原有的 SVN 模式很难完全理解git的协作模式,直到有一天我看到了下面的文章,好多遗留在心中的困惑迎刃而解,于是我将这部分资料进行整理放到了github上,欢迎st ...

  9. 文章之间的基本总结:Activity生命周期

    孔子:温故而知新.它可以作为一个教师.<论语> 同样的学习技巧.对于技术文件或书籍的经典技术,期待再次看到它完全掌握,这基本上是不可能的,所以,我们常常回来几次,然后仔细研究,为了理解作者 ...

  10. 最新jhost免费jsp云空间会员邀请码

    jhost支持jsp.php的免费云空间,邀请码用于激活空间服务: 邀请码:20141003104317_149661                  有效期:2014-10-03 http://w ...