描述
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. Linux - SVN下载项目

    SVN下载项目 本文地址:http://blog.csdn.net/caroline_wendy 使用SVN.在Git上下载项目. $ mkdir chunyu_trunk //创建目录 $ ls / ...

  2. HDU 4828 (卡特兰数+逆)

    HDU 4828 Grids 思路:能够转化为卡特兰数,先把前n个人标为0.后n个人标为1.然后去全排列,全排列的数列.假设每一个1的前面相应的0大于等于1,那么就是满足的序列,假设把0看成入栈,1看 ...

  3. char与unsigned char 差别

    char 与 unsigned char的本质差别 http://bbs.csdn.net/topics/270080484 同一个内存内容:10010000      你用char*   解释是-1 ...

  4. jq分页插件

    jq分页插件 http://www.zhangxinxu.com/jq/pagination_zh/ html --------------- <tbody id="hiddenres ...

  5. mysql监视器MONyog的使用

    MONyog是个商业收费软件,可是能够找一下破解版.我用的是4.72破解版 1.       图1.1 在server设置中,如图1.1. 在Sniffer Settings里Enable sniff ...

  6. hdu 2899 hdu 3400 三分/几何

    hdu2899 : 水提,直接三分,事实上求导后二分也能够. #include<iostream> #include<cstdio> using namespace std; ...

  7. Chrome 小工具: 启动本地应用 (Native messaging)

    最近遇到一个新的问题.需要使用Chrome 插件, 从我们对我们当地的一个网站之一启动C#应用,同时通过本申请值执行不同的操作. 在这里记录下解决的过程.以便以后查找 首先我们须要新建一个google ...

  8. CareerCup chapter 1 Arrays and Strings

    1.Implement an algorithm to determine if a string has all unique characters What if you can not use ...

  9. 乐在其中设计模式(C#) - 适配器模式(Adapter Pattern)

    原文:乐在其中设计模式(C#) - 适配器模式(Adapter Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 适配器模式(Adapter Pattern) 作者:webabc ...

  10. A Novel Multi-label Classification Based on PCA and ML-KNN

     ICIC Express Letters                  ICIC International ⓒ2010 ISSN 1881-803X Volume4, Number5, O ...