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。的更多相关文章

  1. POJ 2240 Arbitrage (spfa判环)

    Arbitrage Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of ...

  2. POJ 2240 Arbitrage spfa 判正环

    d[i]代表从起点出发可以获得最多的钱数,松弛是d[v]=r*d[u],求最长路,看有没有正环 然后这题输入有毒,千万别用cin 因为是大输入,组数比较多,然后找字符串用strcmp就好,千万不要用m ...

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

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

  4. 最短路(Floyd_Warshall) POJ 2240 Arbitrage

    题目传送门 /* 最短路:Floyd模板题 只要把+改为*就ok了,热闹后判断d[i][i]是否大于1 文件输入的ONLINE_JUDGE少写了个_,WA了N遍:) */ #include <c ...

  5. poj 2240 Arbitrage 题解

    Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 21300   Accepted: 9079 Descri ...

  6. poj 2240 Arbitrage (Floyd)

    链接:poj 2240 题意:首先给出N中货币,然后给出了这N种货币之间的兑换的兑换率. 如 USDollar 0.5 BritishPound 表示 :1 USDollar兑换成0.5 Britis ...

  7. POJ 2240 Arbitrage(SPFA+邻接矩阵)

    ( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cmath> #include<algori ...

  8. POJ 2240 Arbitrage【Bellman_ford坑】

    链接: http://poj.org/problem?id=2240 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  9. poj 2240 Arbitrage bellman-ford算法

    点击打开链接 Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13434   Accepted: 5657 ...

随机推荐

  1. Oulipo HDU 1686 KMP模板

    题目大意:求模式串在主串中的出现次数. 题目思路:KMP模板题 #include<iostream> #include<algorithm> #include<cstri ...

  2. Swift 使用Extension 场景 浅析

    别人一看到我的 Swift 代码,立刻就会问我为什么如此频繁的使用 extension.这是前几天在我写的另一篇文章中收到的评论: 我大量使用 extension 的主要目的是为了提高代码可读性.以下 ...

  3. dhcpv6开源软件配置

    ISC-dhcp server for IPv6 1.  下载源码:http://www.isc.org/software/dhcp 2.安装:最好以root身份安装,否则会permission de ...

  4. php实现json

    <?PHP function __json_encode( $data ) { if( is_array($data) || is_object($data) ) { $islist = is_ ...

  5. Air打包exe

    1.用flash创建一个airtest.fla,发布目标选择为AIR.ctrl+enter会得到如下文件: 2.把flex sdk的bin中找到adl.exe,复制过来,放置到:项目目录\bin\ad ...

  6. 关于c中的inline

    在c中,为了解决一些频繁调用的小函数大量消耗栈空间或是叫栈内存的问题,特别的引入了inline修饰符,表示为内联函数.栈空间就是指放置程式的局部数据也就是函数内数据的内存空间,在系统下,栈空间是有限的 ...

  7. 删除正在登录的SQL账号

    exec sp_who '用户名' kill @spid sp_droplogin 用户名

  8. 编程实现prim算法和Dijkstra算法。

    网址链接:http://blog.csdn.net/anialy/article/details/7603170

  9. EF在单例模式及C/S方式开发时,操作数据对象以后如果发生异常,要做善后工作。

    try{ 删除或修改 }catch { _DBContext.Refresh(RefreshMode.StoreWins, entity); }

  10. 转 spring security的使用

    [转自:http://haohaoxuexi.iteye.com/blog/2154714] 关于登录 目录 1.1     form-login元素介绍 1.1.1    使用自定义登录页面 1.1 ...