题目链接: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. servlet缺省路径

    servlet缺省路径 servlet的缺省路径(<url-pattern>/</url-pattern>)是在tomcat服务器内置的一个路径.该路径对应的是一个Defaul ...

  2. 亲历:IT 从业者避免猝死攻略 v1.0

    作者:香蕉痞 出处:http://www.geekpark.net/read/view/191188?u=0 亲历:IT 从业者避免猝死攻略 v1.0 By 香蕉痞 | 2013/10/28 [核心提 ...

  3. HBase入门操作 常用命令和增删改查的简单应用操作

    这里启动关闭Hadoop和HBase的顺序一定是: 启动Hadoop—>启动HBase—>关闭HBase—>关闭Hadoop ssh localhost 开启hadoopcd /us ...

  4. echarts图表属性说明

    参考博客: https://blog.csdn.net/luanpeng825485697/article/details/76691965

  5. mybatis中根据日期模糊查询

    首先设置起始日期startDate和结束日期endDate,数据库中日期字段为achive_time,表名为dos_dossier<select id="getDossiers&quo ...

  6. CSS学习(五)

    导航栏 熟练使用导航栏,对于任何网站都非常重要. 使用CSS你可以转换成好看的导航栏而不是枯燥的HTML菜单. 导航栏=链接列表 作为标准的HTML基础一个导航栏是必须的.在我们的例子中我们将建立一个 ...

  7. java解析XML saxReader.read(xml) 错误:org.dom4j.DocumentException: no protocol

    java解析XML saxReader.read(xml) 错误:org.dom4j.DocumentException: no protocol 完整错误信息: org.dom4j.Document ...

  8. @crossorigin注解跨域

    在@controller中类的头部有一个@CrossOrigin注解. @CrossOrigin是用来处理跨域请求的注解 先来说一下什么是跨域: (站在巨人的肩膀上) 跨域,指的是浏览器不能执行其他网 ...

  9. HDU 5375 Gray code(2015年多校联合 动态规划)

    题目连接 : 传送门 题意: 给定一个长度为的二进制串和一个长度为n的序列a[],我们能够依据这个二进制串得到它的Gray code. Gray code中假设第i项为1的话那么我们就能够得到a[i] ...

  10. Cocos2d-x 常见宏

    1)NS_CC_BEGIN cocos2d命名空间開始 2) NS_CC_END  cocos2d命名空间结束 3)USING_NS_CC 声明cocos2d命名空间 4)CC_SYNTHESIZE_ ...