题目链接: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

最新文章

  1. hive :MetaException(message:Version information not found in metastore. )
  2. beta版本工作百分比
  3. BZOJ-2875 随机数生成器 矩阵乘法快速幂+快速乘
  4. eclipse debug source not fount
  5. Linux常用命令_(进程管理)
  6. [ios]iOS 图形编程总结
  7. QCom MSM MDP显示驱动一些点的简记
  8. FastLoad错误 — SELECT Failed. 2652
  9. java_不知道数据类型情况下,数组遍历-反射
  10. Spring 初学 1
  11. ExtractFileDir 与 ExtractFilePath 的差别
  12. window 7 改变窗口颜色
  13. Swift分割字符串
  14. [HNOI2007]紧急疏散
  15. SpringBoot中并发定时任务的实现、动态定时任务的实现(看这一篇就够了)
  16. C# 一段通用的写log 日志的好程序
  17. csdn博客
  18. Mac OS 10.12 - 安装Homebrew,像Ubuntu里面的apt一样简单地安装和删除软件!
  19. 如何利用JConsole观察分析JAVA程序的运行
  20. struts2学习(3)struts2核心知识II

热门文章

  1. Unity 需不需要再建Assets文件夹
  2. 使用 Jersey 和 Apache Tomcat 构建 RESTful Web 服务
  3. CF482C Game with Strings (状压DP+期望DP)
  4. 实现双向数据绑定mvvm
  5. js 对象的创建方式和对象的区别
  6. [terry笔记]python内置函数
  7. C#-WebService基础02
  8. POJ——T1679 The Unique MST
  9. ZOJ 3435
  10. Tachyon在Spark中的作用(Tachyon: Reliable, Memory Speed Storage for Cluster Computing Frameworks 论文阅读翻译)