Arbitrage

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6360    Accepted Submission(s):
2939

Problem 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 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.
 
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
 
Source
 
Recommend
Eddy   |   We have carefully selected several similar
problems for you:  1142 1162 1385 1301 1596 
 
最短路的变形,由于数据只到30,所以可以采用floyd算法,不过需要注意的是,这里是求最大的倍率。
 
题意:题目大意就是给了你各种货币之间的兑换关系,问你是否存在1个单元的某货币经过一个回路的兑换后>=1个单元( 有利润 )。
 
附上代码:
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#define M 35
using namespace std;
double map[M][M];
int n; void floyd() //利用floyd算法计算最大赔率
{
int k,i,j;
for(k=; k<=n; k++)
for(i=; i<=n; i++)
for(j=; j<=n; j++)
if(map[i][j]<map[i][k]*map[k][j])
map[i][j]=map[i][k]*map[k][j];
} int main()
{
int m,i,j,w=;
char s[M],str[M][M];
while(~scanf("%d",&n)&&n)
{
for(i=; i<=n; i++)
scanf("%s",str[i]);
for(i=; i<=n; i++)
for(j=; j<=n; j++)
{
if(i==j) map[i][j]=; //因为是找最大的汇率,因此初始时本身转本身为1,其他转化为0
else map[i][j]=;
}
scanf("%d",&m);
int a,b;
double c;
for(i=; i<=m; i++)
{
scanf("%s",s);
for(a=; a<=n; a++) //将其转化为map数组记录
if(!strcmp(s,str[a]))
break;
scanf("%lf",&c);
scanf("%s",s);
for(b=; b<=n; b++)
if(!strcmp(s,str[b]))
break;
map[a][b]=c;
}
floyd();
cout<<"Case "<<w++<<": ";
if(map[][]>)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
return ;
}

邻接表:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define N 35
#define M 35*35*10
#define INF 0x3f3f3f3f
using namespace std;
struct Edge
{
int from,to;
double val;
int next;
} edge[M*];
int n,m,tol,s,t,fail;
double dis[N];
bool vis[N];
int head[M*]; void init()
{
tol=;
memset(head,-,sizeof(head));
} void addEdge(int u,int v,double w)
{
edge[tol].from=u;
edge[tol].to=v;
edge[tol].val=w;
edge[tol].next=head[u];
head[u]=tol++;
} void getmap()
{
char str[N][N];
char s[N];
for(int i=; i<=n; i++)
scanf("%s",str[i]);
int a,b;
double c;
scanf("%d",&m);
while(m--)
{
scanf("%s",s);
for(a=; a<=n; a++)
if(!strcmp(s,str[a]))
break;
scanf("%lf",&c);
scanf("%s",s);
for(b=; b<=n; b++)
if(!strcmp(s,str[b]))
break;
addEdge(a,b,c);
}
memset(vis,false,sizeof(vis));
memset(dis,,sizeof(dis));
} void spfa()
{
queue<int>q;
q.push();
dis[]=1.0;
vis[]=true;
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=false;
for(int i=head[u]; i!=-; i=edge[i].next)
{
int v=edge[i].to;
if(dis[v]<dis[u]*edge[i].val)
{
dis[v]=dis[u]*edge[i].val;
if(!vis[v])
{
vis[v]=true;
q.push(v);
}
if(dis[]>)
{
fail=;
return;
} }
}
} } int main()
{ int i,j,T=;
while(~scanf("%d",&n)&&n)
{
init();
getmap();
printf("Case %d: ",T++);
fail=;
spfa();
if(fail)
printf("Yes\n");
else
printf("No\n");
}
return ;
}

hdu 1217 Arbitrage(佛洛依德)的更多相关文章

  1. 佛洛依德 c++ 最短路径算法

    //20142880 唐炳辉 石家庄铁道大学 #include<iostream> #include<string> using namespace std; #define ...

  2. POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环)

    POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环) Description Arbi ...

  3. HDU 1217 Arbitrage (Floyd)

    Arbitrage http://acm.hdu.edu.cn/showproblem.php?pid=1217 Problem Description Arbitrage is the use of ...

  4. hdu 1217 Arbitrage (最小生成树)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1217 /************************************************* ...

  5. HDU 1217 Arbitrage(Bellman-Ford判断负环+Floyd)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217 题目大意:问你是否可以通过转换货币从中获利 如下面这组样例: USDollar 0.5 Brit ...

  6. hdu 1217 Arbitrage (spfa算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217 题目大意:通过货币的转换,来判断是否获利,如果获利则输出Yes,否则输出No. 这里介绍一个ST ...

  7. [ACM] hdu 1217 Arbitrage (bellman_ford最短路,推断是否有正权回路或Floyed)

    Arbitrage Problem Description Arbitrage is the use of discrepancies in currency exchange rates to tr ...

  8. hdu 1217 Arbitrage

    Flody多源最短路 #include<cstdio> #include<cstring> #include<string> #include<cmath&g ...

  9. HDU 1217 Arbitrage(Floyd的应用)

    给出一些国家之间的汇率,看看能否从中发现某些肮脏的......朋友交易. 这是Floyd的应用,dp思想,每次都选取最大值,最后看看自己跟自己的.....交易是否大于一.... #include< ...

随机推荐

  1. Fitnesse批量读取变量信息,并保存到用例执行上下文中

    Fitnesse变量可以分成两种,一种是自定义变量,另一种是用例执行过程中的临时变量. 在Finesse使用过程中,如果需要定义一些公共的变量,可以统一在一个文件中使用自定义变量的方法,将公共变量全部 ...

  2. jquery 调用asp.net后台代码

    1.需要引用对应的命名空间 System.Web.Services 2.后台方法: 必须是static 约束   必须添加[WebMethod()] 属性 示例: <script type=&q ...

  3. 六.随机神经网络Boltzmann(玻尔兹曼机)

    Hopfield网络具有最优计算功能,然而网络只能严格按照能量函数递减方式演化,很难避免伪状态的出现,且权值容易陷入局部极小值,无法收敛于全局最优解. 如果反馈神经网络的迭代过程不是那么死板,可以在一 ...

  4. JS---案例:开机动画

    案例:开机动画 由上下两部分组成,先下面的高变为0 ,再最大的div宽为0,形成一个缩小到没有的动画效果 点击的X是在背景图上的,在上面设置了一个空的span用于注册点击事件 <!DOCTYPE ...

  5. CI框架--浅谈前后台区分

    谈到CI框架,这是我第二个用到的框架,初步使用过后,眼前一亮.CI框架上手简单.模式明确.适合新手学习框架时入手. 下面给大家讲讲CI框架区分前后台文件的具体做法: 首先在application文件夹 ...

  6. Linux下安装配置maven

    参考博客: http://www.blogjava.net/caojianhua/archive/2011/04/02/347559.html 注意事项: 1.解压目录 我的maven解压目录为: / ...

  7. 前端如何实现图片懒加载(lazyload) 提高用户体验

    定义 图片懒加载又称图片延时加载.惰性加载,即在用户需要使用图片的时候加载,这样可以减少请求,节省带宽,提高页面加载速度,相对的,也能减少服务器压力. 惰性加载是程序人性化的一种体现,提高用户体验,防 ...

  8. mac上SVN的图形工具 SmartSVN注册

    mac上SVN的图形工具 SmartSVN注册 打开smartsvn,选中license注册 选中文件smartsvn.license,下一步下一步就ok了 smartsvn.license Name ...

  9. echarts radar 一些会用到了的功能记录

    点击雷达图小标题进行相应操作 // options中添加,坐标轴的标签是否响应和触发鼠标事件,默认不响应. radar:{ triggerEvent: true } // 添加监听事件, 点击雷达图标 ...

  10. TCP/IP 协议栈学习代码

    全部代码 直接使用socket 客户端 import java.io.*; import java.net.Inet4Address; import java.net.InetSocketAddres ...