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法郎,一法郎可以购 ...
随机推荐
- APICloud 上滑加载更多
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> ...
- You-Dont-Need-JQuery (你不需要JQuery)
看完这篇文章我才觉得真的要用JQuery ,因为实在是有些地方设计的使用太复杂了, document.querySelector() 和 Document.querySelectorAll 的确是很方 ...
- 洛谷4623 [COCI2012-2013#6] BUREK
题目描述 给定N个三角形,和M条直线,直线要么平行于X轴,要么平行于Y轴,问这M条直线 分别经过多少个三角形内部 (注意是内部即分开的两个多边形的面积均大于零). 输入输出格式 输入格式: 第一行一个 ...
- 禁止浏览器缓存input值
如果不想让浏览器缓存input的值,有2种方法: 方法一: 在不想使用缓存的input中添加 autocomplete="off"; <input type="te ...
- rsync实时同步mysql数据库
1.主机slave 注:没有包的可以去下载 yum -y install gcc gcc-c++ 上传包 rsync-3.1.3.tar.gz 使用tar命令解压 使用gcc gcc-c++编译 ./ ...
- 解决tpcc_load 报错 error while loading shared libraries: libmysqlclient.so.20
在刚开始导入tpcc数据仓库时,可能会遇到 error while loading shared libraries: libmysqlclient.so.20这个错误,找不到库文件. 但是,通过fi ...
- 转载:CentOS查看本机公网IP命令
icanhazip.com 使你在任何地方知道你的公网IP地址 icanhazip.com是一个网址,你在浏览器中输入这个网址,你就能得到你的公网IP地址了. 我在Linux下一般使用curl ica ...
- 原生ajax的请求过程
原生ajax的请求过程 创建全平台兼容的XMLHttpRequest对象: function getXHR(){ var xhr = null; if(window.XMLHttpRequest) { ...
- Java代码实现MySQL数据库的备份与还原
通常在MySQL数据库的备份和恢复的时候,多是采用在cmd中执行mysql命令来实现. 例如: mysqldump -h127.0.0.1 -uroot -ppass test > d:/tes ...
- mysql5.7官网直译SQL语句优化--分组优化
1.14Group By Optimization 分组优化 大多数方法为了满足分组查询需要扫描整个表并且创建一个临时表,其中每组中的值都是连续的,如果可以使用聚合函数和临时表获取各个分组.在某些情况 ...