(简单) 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 ...
随机推荐
- 【搜索 回溯】 zoj 1002
题意:一些机枪彼此不能在同一行和同一列,但是由于有墙的阻隔,能保证子弹无法穿透,即可以同行同列,现问如果说给了一个n*n(n<=4)的矩阵,并给出了墙的分布情况,能否求出最大能繁殖的机枪数. 思 ...
- initWithFrame、initWithCoder、awakeFromNib的区别和调用次序 & UIViewController生命周期 查缺补漏
当我们创建或者自定义一个UI控件时,就很可能会调用awakeFromNib.initWithCoder .initWithFrame这些方法.三者的具体区别如下: initWithFrame: 通过代 ...
- Dom+2016/4/20
元素.parentNode父节点 offsetParent
- Light OJ 1006 - Hex-a-bonacci
题目链接:http://acm.hust.edu.cn/vjudge/contest/121396#problem/G http://lightoj.com/volume_showproblem.ph ...
- cordova插件开发-1
这是初级编,实现了js调用Android代码 首先需要编写java代码: public class AppUpdate extends CordovaPlugin { @Override public ...
- alt+shift+j,添加日期、作者等
在preference->Java->codestyle->codetemplates->commnets->type 可以编辑如: /** * @author ${us ...
- RadioGroup+TabHost
=.= //MainActivity public class MainActivity extends TabActivity implements OnCheckedChangeListener ...
- windows 杀进程软件
pchunter 电脑禁用u盘可用金山卫士开启.注册表
- new关键字的理解-问题型
//输出的结果是?????????//The answer is good and gbc public class Example { String str=new String("goo ...
- 2016大连网络赛 Football Games
Football Games Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) P ...