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 ...
随机推荐
- RH033读书笔记(2)-Lab 3 Getting Help with Commands
Lab 3 Getting Help with Commands Sequence 1: Using the Help Tools 1. man -f keyword whatis keyword l ...
- 于Heroku平台部署maven webapp(java web)工程
眼下,需要Heroku上述部署java web工程,该项目必须使用maven管理 一:新maven webapp工程 编者pom.xml档,增加下面的配置为例, <project xmlns=& ...
- Eclipse——热键&Help
版权声明:本文博主原创文章.博客,未经同意不得转载.
- SecureCRT 6.7.1 RI和谐 皴 补丁 方法
它之前被使用SecureCRT 6.5.3 版本号,咋看和谐补丁,即使中国版本也可(现在才发现SecureCRT.6.2.0) 可是换为 6.7.1 后就怎么也注冊不了了.. 没办法试了各种办法: 先 ...
- servlet(jsp)中的重定向和转发
servlet(jsp)中的重定向和转发 由一个servlet(jsp)从内部转向还有一个servlet(jsp)有两种方式:转发和重定向. 转发:是由一个web组件(servlet)将未完毕的处理交 ...
- ios 多线程开发(二)线程管理
线程管理 iOS和OS X中每一个进程(或程序)由一个或多个线程组成.程序由一个运行main方法的线程开始,中间可以产生其他线程来执行一些指定的功能. 当程序产生一个新线程后,这个线程在程序进程空间内 ...
- struts开发步骤
说来惭愧.这是一个简单的struts折腾了很长一段时间,几乎相同的时间量就花了三天时间来解决.下面的步骤总结一下我开发:(我使用的是MyEclipse); 1.新建一个Exercise3的web Pr ...
- GitLab 安装配置笔记(转)
GitLab的安装方式 GitLab的两种安装方法: 编译安装 优点:可定制性强.数据库既可以选择MySQL,也可以选择PostgreSQL;服务器既可以选择Apache,也可以选择Nginx. 缺点 ...
- 【Java技术位】——代理模式及其事务包
背景 项目中我们会遇到这种情况:在几个方法中增加同样的代码,这些代码是与业务无关的,而且以后有可能因为考虑不周或需求变动再或者是其它原因,我们须要对他们进行逐一进行修改.举个详细的样例,比方程序中的日 ...
- cocos2d-x -- 渠道SDK【棱镜】接入(1)
棱镜SDK简单介绍 若想让游戏上线,渠道接入步骤是不可缺少的,为了避免一对一接入渠道问题,我选择了棱镜SDK,由于棱镜是游戏与渠道SDK的中间层,为CP厂商屏蔽各个渠道SDK之间的差异,整个接入过程, ...