(简单) POJ 2240 Arbitrage,SPFA。
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.
题目就是问货币能不能通过转换而让自己增加。。。
用的SPFA来判断的环。。。枚举每一个点进行SPFA (也就是说floyd也是可以的。。。)。。。
代码如下:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue> using namespace std; const int INF=10e8;
const int MaxN=; struct Edge
{
int v;
double cost; Edge(int _v=,double _cost=):v(_v),cost(_cost) {}
}; vector <Edge> E[MaxN];
bool vis[MaxN];
int couNode[MaxN]; bool SPFA(double lowcost[],int n,int start)
{
queue <int> que;
int u,v;
double c;
int len; for(int i=;i<=n;++i)
{
vis[i]=;
couNode[i]=;
lowcost[i]=;
} vis[start]=;
couNode[start]=;
lowcost[start]=; que.push(start); while(!que.empty())
{
u=que.front();
que.pop(); vis[u]=;
len=E[u].size(); for(int i=;i<len;++i)
{
v=E[u][i].v;
c=E[u][i].cost; if(lowcost[u]*c>lowcost[v])
{
lowcost[v]=lowcost[u]*c; if(!vis[v])
{
vis[v]=;
++couNode[v];
que.push(v); if(couNode[v]>=n)
return ;
}
}
}
} return ;
} inline void addEdge(int u,int v,double c)
{
E[u].push_back(Edge(v,c));
} char ss[][];
double ans[MaxN];
int N; int find(char *s)
{
for(int i=;i<=N;++i)
if(strcmp(s,ss[i])==)
return i;
} int main()
{
int M;
bool ok;
char ts1[],ts2[];
int t1,t2;
double tr;
int cas=; for(scanf("%d",&N);N;scanf("%d",&N),++cas)
{
for(int i=;i<=N;++i)
{
scanf("%s",ss[i]); E[i].clear();
} scanf("%d",&M); for(int i=;i<=M;++i)
{
scanf("%s %lf %s",ts1,&tr,ts2);
t1=find(ts1);
t2=find(ts2); addEdge(t1,t2,tr);
} ok=; for(int i=;i<=N;++i)
if(!SPFA(ans,N,i))
{
ok=;
break;
} printf("Case %d: ",cas); if(ok)
printf("Yes\n");
else
printf("No\n"); } return ;
}
(简单) POJ 2240 Arbitrage,SPFA。的更多相关文章
- POJ 2240 Arbitrage (spfa判环)
Arbitrage Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of ...
- POJ 2240 Arbitrage spfa 判正环
d[i]代表从起点出发可以获得最多的钱数,松弛是d[v]=r*d[u],求最长路,看有没有正环 然后这题输入有毒,千万别用cin 因为是大输入,组数比较多,然后找字符串用strcmp就好,千万不要用m ...
- POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环)
POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环) Description Arbi ...
- 最短路(Floyd_Warshall) POJ 2240 Arbitrage
题目传送门 /* 最短路:Floyd模板题 只要把+改为*就ok了,热闹后判断d[i][i]是否大于1 文件输入的ONLINE_JUDGE少写了个_,WA了N遍:) */ #include <c ...
- poj 2240 Arbitrage 题解
Arbitrage Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21300 Accepted: 9079 Descri ...
- poj 2240 Arbitrage (Floyd)
链接:poj 2240 题意:首先给出N中货币,然后给出了这N种货币之间的兑换的兑换率. 如 USDollar 0.5 BritishPound 表示 :1 USDollar兑换成0.5 Britis ...
- POJ 2240 Arbitrage(SPFA+邻接矩阵)
( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cmath> #include<algori ...
- POJ 2240 Arbitrage【Bellman_ford坑】
链接: http://poj.org/problem?id=2240 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- poj 2240 Arbitrage bellman-ford算法
点击打开链接 Arbitrage Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13434 Accepted: 5657 ...
随机推荐
- 2.2 sikuli中编程运行
http://www.cnblogs.com/Flint/p/4951703.html a.如果需要指定点击的具体坐标,需要使用click(patten.targetoffset(x, y)). b. ...
- 【纯欧拉函数】 poj 2407
#include <cstdio> #include <iostream> using namespace std; int phi(int x) { int t=x; ;i& ...
- 织梦DEDECMS更新6月7日补丁后出现版权链接的删除办法
说最近网站底部多出了一个Power by DedeCms的超链接版权信息,但是该网友最近只是升级了0607更新补丁,其它的源代码并没有改动,而以前的文章页面并没有,看来是这次更新dedecms后才被加 ...
- MVVM 入门介绍
转载自:http://www.objccn.io/issue-13-1/ 我于 2011 年在 500px 找到自己的第一份 iOS 开发工作.虽然我已经在大学里做了好几年 iOS 外包开发,但这才是 ...
- 安装mysql ,从 mysql-5.5.5 开始innodb作为默认的存储引擎了
[root@Linux opt]# tar -xvf MySQL-5.6.16-1.rhel5.x86_64.rpm-bundle.tar MySQL-shared-compat-5.6.16-1.r ...
- jenkins tags
List Subversion tags (and more) 参数设置 Tags filter ^((?!_ta_).)*$ 表示不含_ta_ Tags filtertrunk|(tags|bran ...
- 高性能IO模型浅析(转)
转自:http://www.cnblogs.com/fanzhidongyzby/p/4098546.html 是我目前看到的解释IO模型最清晰的文章,当然啦,如果想要详细的进一步了解还是继续啃蓝宝书 ...
- CodeForces 678C Joty and Chocolate
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...
- Selenium2+python自动化28-table定位
前言 在web页面中经常会遇到table表格,特别是后台操作页面比较常见.本篇详细讲解table表格如何定位. 一.认识table 1.首先看下table长什么样,如下图,这种网状表格的都是table ...
- ibdata1文件--缩小mysql数据库的ibdata1文件
摘要 在MySQL数据库中,如果不指定innodb_file_per_table参数,单独存在每个表的数据,MySQL的数据都会存放在ibdata1文件. mysql ibdata1存放数据,索引等, ...