Nyoj Arbitrage(Floyd or spfa or Bellman-Ford)
描述
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)的更多相关文章
- ACM/ICPC 之 最短路径-Bellman Ford范例(POJ1556-POJ2240)
两道Bellman Ford解最短路的范例,Bellman Ford只是一种最短路的方法,两道都可以用dijkstra, SPFA做. Bellman Ford解法是将每条边遍历一次,遍历一次所有边可 ...
- poj1860 bellman—ford队列优化 Currency Exchange
Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 22123 Accepted: 799 ...
- uva 558 - Wormholes(Bellman Ford判断负环)
题目链接:558 - Wormholes 题目大意:给出n和m,表示有n个点,然后给出m条边,然后判断给出的有向图中是否存在负环. 解题思路:利用Bellman Ford算法,若进行第n次松弛时,还能 ...
- Bellman—Ford算法思想
---恢复内容开始--- Bellman—Ford算法能在更普遍的情况下(存在负权边)解决单源点最短路径问题.对于给定的带权(有向或无向)图G=(V,E),其源点为s,加权函数w是边集E的映射.对图G ...
- Bellman - Ford 算法解决最短路径问题
Bellman - Ford 算法: 一:基本算法 对于单源最短路径问题,上一篇文章中介绍了 Dijkstra 算法,但是由于 Dijkstra 算法局限于解决非负权的最短路径问题,对于带负权的图就力 ...
- 一个人的旅行(floyd+dijskra+SPFA+Bellman)
一个人的旅行 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- POJ 2240 Arbitrage (Bellman Ford判正环)
Arbitrage Time Limit: 1000MS Memory Limit: 65536K Total Submissions:27167 Accepted: 11440 Descri ...
- 图论算法——最短路径Dijkstra,Floyd,Bellman Ford
算法名称 适用范围 算法过程 Dijkstra 无负权 从s开始,选择尚未完成的点中,distance最小的点,对其所有边进行松弛:直到所有结点都已完成 Bellman-Ford 可用有负权 依次对所 ...
- 最短路知识点总结(Dijkstra,Floyd,SPFA,Bellman-Ford)
Dijkstra算法: 解决的问题: 带权重的有向图上单源最短路径问题.且权重都为非负值.如果采用的实现方法合适,Dijkstra运行时间要低于Bellman-Ford算法. 思路: 如果存在一条从i ...
随机推荐
- IIS在W7下使用
1.0.发布程序
- Dubbo入门基础与实例讲解(转)
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka Dubbo是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,0 ...
- LinearLayout具体解释一:LinearLayout的简单介绍
LinearLayout,中文意思是线性布局.假设你是初学android的,肯定会非常困惑"啥叫布局",啥又叫"线性布局"呢. 有的时候,我尝试用官方的语言去解 ...
- 文章3说话 微信商城云server创建后台
一个. 应用server资源 想要进行微信开发.少不了后台server端程序的开发,那么我们首先就要申请server资源.眼下有非常多云server可选,比方新浪的sae ...
- Zygote过程【3】——SystemServer诞生
欢迎转载.转载请注明:http://blog.csdn.net/zhgxhuaa 在ZygoteInit的main()方法中做了几件大事.当中一件便是启动Systemserver进程.代码例如以下: ...
- Linux高性能server规划——多线程编程(在)
多线程编程 Linux主题概述 线程模型 线程是程序中完毕一个独立任务的完整执行序列.即一个可调度的实体. 依据执行环境和调度者的身份.线程可分为内核线程和用户线程.内核线程,在有的系统上也称为LWP ...
- sort和qsort排序
qsort(数组名,数组长度,数组中每个元素大小,compare); compare函数的写法决定了排序是升序还是降序.需要#include<stdlib.h> 例如: int compa ...
- HDU 2017 一系列统计数据
一系列统计数据 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...
- VS2010或2012中,如何设置代码格式化?
ctrl + E,D菜单在 编辑-->高级 里面 第一个菜单项
- 在高德地图应用api,和api展出的标记小的应用程序
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...