hdu 1217 Arbitrage(佛洛依德)
Arbitrage
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6360 Accepted Submission(s):
2939
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 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.
arbitrage is possible or not in the format "Case case: Yes" respectively "Case
case: No".
#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(佛洛依德)的更多相关文章
- 佛洛依德 c++ 最短路径算法
//20142880 唐炳辉 石家庄铁道大学 #include<iostream> #include<string> using namespace std; #define ...
- POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环)
POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环) Description Arbi ...
- HDU 1217 Arbitrage (Floyd)
Arbitrage http://acm.hdu.edu.cn/showproblem.php?pid=1217 Problem Description Arbitrage is the use of ...
- hdu 1217 Arbitrage (最小生成树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1217 /************************************************* ...
- HDU 1217 Arbitrage(Bellman-Ford判断负环+Floyd)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217 题目大意:问你是否可以通过转换货币从中获利 如下面这组样例: USDollar 0.5 Brit ...
- hdu 1217 Arbitrage (spfa算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217 题目大意:通过货币的转换,来判断是否获利,如果获利则输出Yes,否则输出No. 这里介绍一个ST ...
- [ACM] hdu 1217 Arbitrage (bellman_ford最短路,推断是否有正权回路或Floyed)
Arbitrage Problem Description Arbitrage is the use of discrepancies in currency exchange rates to tr ...
- hdu 1217 Arbitrage
Flody多源最短路 #include<cstdio> #include<cstring> #include<string> #include<cmath&g ...
- HDU 1217 Arbitrage(Floyd的应用)
给出一些国家之间的汇率,看看能否从中发现某些肮脏的......朋友交易. 这是Floyd的应用,dp思想,每次都选取最大值,最后看看自己跟自己的.....交易是否大于一.... #include< ...
随机推荐
- Leetcode96.Unique Binary Search Trees不同的二叉搜索树
给定一个整数 n,求以 1 ... n 为节点组成的二叉搜索树有多少种? 示例: 输入: 3 输出: 5 解释: 给定 n = 3, 一共有 5 种不同结构的二叉搜索树: 假设n个节点存在二叉排序树的 ...
- JRE System Library、Referenced Libraries、Web App Libraries的含义
JRE System Library.Referenced Libraries.Web App Libraries 这三个都是jar包的存放集合. JRE System Library:指Java S ...
- Android——系统权限
Android是一个特权分隔的操作系统,每一个应用程序运行在不同的系统身份中(Linux的user ID和group ID).系统部分和不同的身份被隔离开来.因此,Linux隔离了应用程序(与其它程序 ...
- saltstack+python批量修改服务器密码
saltstack安装:略过 python脚本修改密码: # -*- coding utf-8 -*- import socket import re import os import sys imp ...
- More Effective C++: 04效率
16:牢记80-20准则 80-20准则说的是大约20%的代码使用了80%的程序资源:大约20%的代码耗用了大约80%的运行时间:大约20%的代码使用了80%的内存:大约20%的代码执行80%的磁盘访 ...
- [Java]ITOO初步了解 标签: javajbosstomcat 2016-05-29 21:14 3367人阅读 评论(34)
开始接触Java的ITOO了,这两天在搭环境,结果发现,哇,好多没接触过的东西,先写篇博客来熟悉一下这些工具. JBoss 基于Tomcat内核,青胜于蓝 Tomcat 服务器是一个免费的开放 ...
- 【数据库】sql2008卸载和默认实例的删除 标签: 数据库 2014-11-16 15:15 5878人阅读 评论(30)
在安装sql2008的时候,会碰到这一步,要求创建实例,可以选择默认实例和命名实例,如果是第一次安装的话,可以选择默认实例,但是如果是第二次甚至更多次安装的 话,很多时候会出现不能用默认实例,只能自己 ...
- Java练习 SDUT-3848_Shift Dot
Shift Dot Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 给出平面直角坐标系中的一点,并顺序给出n个向量,求该 ...
- 快递查询API接口集成,有需要的可以直接用
适用于涉及经常发货.寄快递的人群.企业.电商网站.微信公众号平台等对接使用.支持国内外三百多家快递及物流公司的快递单号一站式查询. 使用说明: 1.KuadidiAPI.php 不需要修改改任何东西 ...
- 8-2udp和tcp网络编程以及粘包和解决粘包的方法
一 tcp网络编程 server 端 import socket sk=socket.socket() #实例化一个对象 sk.setsockopt(socket.SOL_SOCKET,socket ...