POJ-2240 Arbitrage BellmanFord查可循环圈
题目链接:https://cn.vjudge.net/problem/POJ-2240
题意
套利(Arbitrage)就是通过不断兑换外币,使得自己钱变多的行为
给出一些汇率
问能不能套利
思路
马上想到bellman的判负圈
于是写完WA一发
问题在是否联通的问题上,我随便给Bellman喂了一个节点...
然后有连续WA了5次,问题在我把Yes打成了YES...
- 图论题目一定要考虑连通性,至少是查负环时
- 注意预处理
- 以后再也别手打输出了,就算是天塌下来也别
代码
#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查可循环圈的更多相关文章
- poj 2240 Arbitrage bellman-ford算法
点击打开链接 Arbitrage Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13434 Accepted: 5657 ...
- POJ 2240 - Arbitrage - [bellman-ford求最短路]
Time Limit: 1000MS Memory Limit: 65536K Description Arbitrage is the use of discrepancies in currenc ...
- POJ - 2240 Arbitrage(Bellman-Ford)
https://vjudge.net/problem/POJ-2240 题意 已知n种货币,以及m种货币汇率及方式,问能否通过货币转换,使得财富增加. 分析 Bellman-Ford判断正环,注意初始 ...
- poj 2240 Arbitrage 题解
Arbitrage Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21300 Accepted: 9079 Descri ...
- 最短路(Floyd_Warshall) POJ 2240 Arbitrage
题目传送门 /* 最短路:Floyd模板题 只要把+改为*就ok了,热闹后判断d[i][i]是否大于1 文件输入的ONLINE_JUDGE少写了个_,WA了N遍:) */ #include <c ...
- POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环)
POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环) Description Arbi ...
- poj 2240 Arbitrage (Floyd)
链接:poj 2240 题意:首先给出N中货币,然后给出了这N种货币之间的兑换的兑换率. 如 USDollar 0.5 BritishPound 表示 :1 USDollar兑换成0.5 Britis ...
- 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(floyd)
http://poj.org/problem?id=2240 题意 : 好吧,又是一个换钱的题:套利是利用货币汇率的差异进行的货币转换,例如用1美元购买0.5英镑,1英镑可以购买10法郎,一法郎可以购 ...
随机推荐
- GCC中的强符号和弱符号及强引用和弱引用
1. 强符号和弱符号 1.1 u-boot和kernel中的__weak指令 u-boot和kernel比较普遍地使用了__weak来定义函数. 在include\linux\compiler-gcc ...
- CorelDRAW教程:怎样绘制制作箭头流程图?
箭头流程图主要由矢量图和连接符组成,通过图形之间的顺序阐述的一个过程,应用也是非常广泛,有些软件中会自带流程图,对于CDR这款矢量绘图软件来说,手动制作流程图是简单且高效的.首先CorelDRAW中就 ...
- ZBrush中Nudge推动笔刷介绍
本文我们来介绍Nudge推动笔刷,该笔刷在使用时能够产生旋转涂抹的效果,Nudge笔刷允许您在模型表面移动顶点,而这些移动的顶点仍然停留在模型的原来的表面,它与Move笔刷还是不同的,利用Move笔刷 ...
- 使用 Shiro 设计基于用户、角色、权限的通用权限管理系统
一.前言 在大型的信息管理系统中,经常涉及到权限管理系统 下面来个 demo,很多复杂的系统的设计都来自它 代码已经放到github上了,地址:https://github.com/larger5/s ...
- easyUI combobox的使用
1.需要用到的方法 设置组合框(combobox)值的数组. $('#cc').combobox('setValues', ['001','002']); 设置组合框(combobox)的值. $(' ...
- W3c盒子模型+IE盒子模型+box-sizing属性
1.盒子模型有两种,标准盒模型和IE盒模型,其中W3C标准的盒模型就是在网页的顶部加上 DOCTYPE 声明. (1)W3C标准的盒模型 W3C盒子模型包括4部分:margin,border,padd ...
- centos7安装anaconda之后报错:rpm: /home/wyl/anaconda3/lib/liblzma.so.5: version `XZ_5.1.2alpha' not found (required by /lib64/librpmio.so.3)
1.报错 参考:https://stackoverflow.com/questions/47633870/rpm-lib64-liblzma-so-5-version-xz-5-1-2alpha-no ...
- 《Exception》第八次团队作业:Alpha冲刺(第二天)
一.项目基本介绍 项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 Exception 作业学习目标 1.掌握软件测试基础技术.2.学习迭代式增量软 ...
- 改变浏览器页面默认滚动条样式scrollbar
scrollbar-3d-light-color 设置或检索滚动条亮边框颜色scrollbar-highlight-color 设置或检索滚动条3D界面的亮边(ThreedHighlight)颜色sc ...
- UVA340-Master-Mind Hints(紫书例题3.4)
MasterMind is a game for two players. One of them, Designer, selects a secret code. The other, Break ...