题目链接:https://cn.vjudge.net/problem/POJ-2240

题意

套利(Arbitrage)就是通过不断兑换外币,使得自己钱变多的行为

给出一些汇率

问能不能套利

思路

马上想到bellman的判负圈

于是写完WA一发

问题在是否联通的问题上,我随便给Bellman喂了一个节点...

然后有连续WA了5次,问题在我把Yes打成了YES...

  1. 图论题目一定要考虑连通性,至少是查负环时
  2. 注意预处理
  3. 以后再也别手打输出了,就算是天塌下来也别

代码

#include <map>
#include <queue>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn=35, INF=0x3f3f3f3f;
const double eps=1e-6;
struct Edge{
int from, to;
double rate;
};
map<string, int> words;
vector<Edge> edges;
vector<int> G[maxn+5]; inline void addEdge(int from, int to, double rate){
edges.push_back((Edge){from, to, rate});
G[from].push_back(edges.size()-1);
// edges.push_back(Edge{to, from, 1/rate});
// G[to].push_back(edges.size()-1);
} inline bool equal(const double &a, const double &b){
return ((a-b<=eps) && (b-a<=eps));
} bool Bellman(int st, int n){
double dist[maxn+5];
bool inq[maxn+5]={false};
int cnt[maxn+5]={0};
queue<int> que; for (int i=0; i<=maxn; i++) dist[i]=0;//(double)INF;
dist[st]=1.0; inq[st]=true;
que.push(st); while (que.size()){
int from=que.front(); que.pop();
inq[from]=false; for (int i=0; i<G[from].size(); i++){
Edge &e=edges[G[from][i]];
int &to=e.to; double nrate=dist[from]*e.rate; if (dist[to]>nrate || equal(dist[to], nrate)) continue;
dist[to]=nrate; if (inq[to]) continue;
inq[to]=true;
que.push(to); if (++cnt[to]>=n) return true;
}
}return false;
} void init(void){
for (int i=0; i<=maxn; i++) G[i].clear();
edges.clear();
words.clear();
} int main(void){
int n, m, cnt=1;
double rate;
string name, from, to; while (scanf("%d", &n)==1 && n){
init();
for (int i=1; i<=n; i++){
cin >> name;
words[name]=i;
}
cin >> m; for (int i=0; i<m; i++){
cin >> from >> rate >> to;
addEdge(words[from], words[to], rate);
} bool flg=false;
for (int i=1; i<=n; i++)
if (Bellman(i, n)){
printf("Case %d: Yes\n", cnt++);
flg=true;
break;
}
if (!flg) printf("Case %d: No\n", cnt++);
} return 0;
}
Time Memory Length Lang Submitted
954ms 800kB 2317 G++ 2018-05-25 19:11:58

POJ-2240 Arbitrage BellmanFord查可循环圈的更多相关文章

  1. poj 2240 Arbitrage bellman-ford算法

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

  2. POJ 2240 - Arbitrage - [bellman-ford求最短路]

    Time Limit: 1000MS Memory Limit: 65536K Description Arbitrage is the use of discrepancies in currenc ...

  3. POJ - 2240 Arbitrage(Bellman-Ford)

    https://vjudge.net/problem/POJ-2240 题意 已知n种货币,以及m种货币汇率及方式,问能否通过货币转换,使得财富增加. 分析 Bellman-Ford判断正环,注意初始 ...

  4. poj 2240 Arbitrage 题解

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

  5. 最短路(Floyd_Warshall) POJ 2240 Arbitrage

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

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

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

  7. poj 2240 Arbitrage (Floyd)

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

  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(floyd)

    http://poj.org/problem?id=2240 题意 : 好吧,又是一个换钱的题:套利是利用货币汇率的差异进行的货币转换,例如用1美元购买0.5英镑,1英镑可以购买10法郎,一法郎可以购 ...

随机推荐

  1. CDR X7 限时3折618年中大促,是时候出手了!

    力量与激情.胜利与喜悦,这些自带饱满情绪的词,即将“刷屏”这个夏天!32支球队,28个不眠夜,你将与谁度过? 一场视觉上的饕餮盛宴即将开始! 小编也是个疯狂足球迷,虽然中国队无缘今年的俄罗斯世界杯,但 ...

  2. [CodeForces]529B Group Photo 2

    AK爷GhostCai的电脑又蓝屏了Orz 贪心题,确定一个maxh,限定h不大于一个值.枚举maxh. check的时候的细节很多: 1.h>maxh但w<maxh交换的时候需要占用交换 ...

  3. freeswitch 一些坑

    1.You must install libopus-dev to build mod_opus. Stop. 确实已经安装libopus-dev后 将文件 usr/local/src/mod/cod ...

  4. Python中的itertools.product

    例子1:import itertoolsa = itertools.product([1,2,3],[100,200])print(a)for item in itertools.product([1 ...

  5. webpack实现模块化打包

    webpack打包应用和实现 1)安装webpack $ npm install webpack webpack-cli --save-dev 2)添加配置文件 webpack.config.js 3 ...

  6. linux部分常用命令

    linux的命令挺多的,下面是我常用的,其实也不可能在敲代码的时候把这个博客拿出来对着写,就是想记录一下,刚开始都觉得不好记,多敲几遍就记住了!!! 创建文件夹:mkdir filename 删除当前 ...

  7. Linux学习总结(15)——提高 Vim 和 Shell 效率的 9 个建议

    你上一次使用 CAPSLOCK 键是什么时候?很久没有了对不对?噢,我也是,它已经被遗忘了,它浪费了键盘上一个黄金位置.让我们把它重映射成 Control 键来发挥它的作用吧!这里告诉了你在不同的操作 ...

  8. skimage的安装,scikit-image

    在mac上面的安装: pip install -U scikit-image

  9. POJ 2084

    第一题组合数学题.可以使用递推,设1与其他各数分别连边,假设N=3;若1-4,则圆分成两部分计数,此时可以利用乘法原理.(高精度) #include <cstdio> #include & ...

  10. Excel数据导入___你hold住么(一)

    近期小编跟着团队一起开发ITOO3.0高校云平台项目,当中的收获是不言而喻滴,在项目中有个导入功能:导入学生信息:导入班级信息:导入教学楼信息等,在不知多少次的尝试之下,成功实现功能. 框架分析 详解 ...