题目链接: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. jquery的animate能渐变background-color

    在freecodecamp 上学习复习时,写了一个demo,引用了 jquery 实现 color 的渐变动画,然后运行测试的时候,发现其他功能都正常,就是无法是实现颜色的动画. 如: $('butt ...

  2. JWT加密

    JWT是一种加密算法,为了防止请求的信息在传输途中被拦截修改 JWT的引用: install-package jwt JWF由三部分组成:Header,Payload,Signature Payloa ...

  3. 基础——(4)D Latch(D锁存器)

    S-R Latch Put a inverter there: Invertor的组成: tie both of the inputs together加上一个nor gate 就能组成一个inver ...

  4. HDU 1086 You can Solve a Geometry Problem too( 判断线段是否相交 水题 )

    链接:传送门 题意:给出 n 个线段找到交点个数 思路:数据量小,直接暴力判断所有线段是否相交 /*************************************************** ...

  5. thinkPHP利用ajax异步上传图片并显示、删除

    近来学习tp5的过程中,项目中有个发帖功能,选择主题图片.如下: 利用原始的文件上传处理,虽然通过原始js语句能实时显示上传图片,但是这样的话会涉及很多兼容问题.使用ajax技术,实现选择性删除所选图 ...

  6. Java并发和多线程3:线程调度和有条件取消调度

    在第1篇中"并发框架基本示例",提到了Executors和ThreadPool.其中,还有个"定时调度"的方法,Executors.newScheduledTh ...

  7. React 中的 refs的应用

    React Refs React 支持一种非常特殊的属性 Ref ,你可以用来绑定到 render() 输出的任何组件上. 这个特殊的属性允许你引用 render() 返回的相应的支撑实例( back ...

  8. tp 在Nginx上各种404

    对于ThinkPHP的URL访问路劲如:http://域名/index.php/Index/BlogTest/read,原先的Nginx的是不支持的pathinfo路劲的,导致你在thinkPHP5上 ...

  9. UVALive 2664 One-way traffic

    One-way traffic Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Or ...

  10. 重启rsyslog服务时出现问题(误删/var/log/messages解决方案)

    今天修改了/etc/rsyslog.conf中的内容后,想着要通过systemctl restart rsyslog重启服务,但是执行完命令后,总感觉/etc/rsyslog.conf中修改的内容没有 ...