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 ;
}

最新文章

  1. 控制台游戏引擎CGE——贪吃蛇
  2. 分离EF connectionString里的db连接串
  3. Libliner 中的-s 参数选择:primal 和dual
  4. mysql jdbc连接
  5. MS CRM 2011的自定义和开发(11)——插件(plugin)开发(三)
  6. 查询Table name, Column name, 拼接执行sql文本, 游标, 存储过程, 临时表
  7. MINA的session.close
  8. VirtualBox的usb支持
  9. android getDecorView()的作用
  10. Android studio ElasticDownloadView
  11. windows部署SpiderKeeper(爬虫监控)
  12. mpvue——修改第三方组件样式
  13. 智联python 技能摘取
  14. ready
  15. C#判断输入的是否为数字(int.TryParse)
  16. 关于@font-face的使用
  17. tf.nn.embedding_lookup函数【转载】
  18. PHP性能监测的工具介绍 - XHProf -参考自https://jingyan.baidu.com/article/7082dc1c173359e40a89bd95.html
  19. mpvue使用vant Weapp运行npm run build命令打包后失效
  20. BZOJ2622 深入虎穴(最短路径)

热门文章

  1. win10 请求操作需要提升解决方案
  2. opencv2.4.9配置+VS2013
  3. 使用C3P0和DBUtils
  4. 单行中文字和图片的相关height和line-height特性
  5. vue中element-ui添加按钮
  6. JMeter与LoadRunner的对比
  7. 关于 SSD 的接口和相关名词(2019-09-10)
  8. UIScrollView 实践经验
  9. 初探postman
  10. More Effective C++: 05技术(25-28)