给出 N 种货币 M 条兑换关系 开始时所有的货币S 和有X 块钱

接下来M条关系

A B W1 W2 W3 W4

表示

A->B 所需的手续费为W2块钱  汇率为W1

B->A 所需的手续费为W4块钱  汇率为W3

所以对于输入的一行建两条边

要求到最后可以赚到钱

所以当出现了负圈即可赚到无限多的钱

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cctype>
#include <cmath>
#include <string>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#include <map>
typedef long long LL;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define pi acos(-1.0)
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
typedef pair<int, int> PI;
typedef pair<int, PI> PP;
#ifdef _WIN32
#define LLD "%I64d"
#else
#define LLD "%lld"
#endif
//LL quick(LL a, LL b){LL ans=1;while(b){if(b & 1)ans*=a;a=a*a;b>>=1;}return ans;}
//inline int read(){char ch=' ';int ans=0;while(ch<'0' || ch>'9')ch=getchar();while(ch<='9' && ch>='0'){ans=ans*10+ch-'0';ch=getchar();}return ans;}
//inline void print(LL x){printf(LLD, x);puts("");}
//inline void read(int &x){char c = getchar();while(c < '0') c = getchar();x = c - '0'; c = getchar();while(c >= '0'){x = x * 10 + (c - '0'); c = getchar();}}
const int INF=0x3f3f3f3f;
const int MAXN=550;
double dist[MAXN];
struct Edge
{int u,v;double a,b;Edge(int _u=0,int _v=0,double _a=0,double _b=0):u(_u),v(_v),a(_a),b(_b) {}
};
vector<Edge>E;
bool bellman_ford(int start,int n,double x)
{for(int i=1; i<=n; i++)dist[i]=0;dist[start]=x;for(int i=1; i<n; i++){bool flag=false;for(int j=0; j<E.size(); j++){int u=E[j].u;int v=E[j].v;double a=E[j].a;double b=E[j].b;if(dist[v]<(dist[u]-b)*a){dist[v]=(dist[u]-b)*a;flag=true;}}if(!flag)return true;//没有负环回路}for(int j=0; j<E.size(); j++)if(dist[E[j].v]<(dist[E[j].u]-E[j].b)*E[j].a)return false;//有负环回路return true;//没有负环回路
}
int main()
{
#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);freopen("out.txt", "w", stdout);
#endifint n,m,s;double x;while(scanf("%d%d%d%lf",&n,&m,&s,&x)!=EOF){int a,b;double w1,w2,w3,w4;E.clear();for(int i=0; i<m; i++){scanf("%d%d%lf%lf%lf%lf",&a,&b,&w1,&w2,&w3,&w4);E.push_back(Edge(a,b,w1,w2));E.push_back(Edge(b,a,w3,w4));}if(bellman_ford(s,n,x))printf("NO\n");else puts("YES");}return 0;
}

转载于:https://www.cnblogs.com/kewowlo/p/4088351.html

【最短路】 ZOJ 1544 Currency Exchange 判断负圈相关推荐

  1. 【最短路】 ZOJ 1544 Currency Exchange 推断负圈

    给出 N 种货币 M 条兑换关系 開始时全部的货币S 和有X 块钱 接下来M条关系 A B W1 W2 W3 W4 表示 A->B 所需的手续费为W2块钱  汇率为W1 B->A 所需的手 ...

  2. AOJ GRL_1_C: All Pairs Shortest Path (Floyd-Warshall算法求任意两点间的最短路径)(Bellman-Ford算法判断负圈)

    题目链接:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_C All Pairs Shortest Path Input ...

  3. 【算法】Bellman-Ford算法(单源最短路径问题)(判断负圈)

    单源最短路问题是固定一个起点,求它到其他所有点的最短路的问题. 算法: 设 d[i] 表示 起点 s 离点 i 的最短距离. 固定起点s,对所有的点 , i = s , d[i] 置为 0 :i != ...

  4. Currency Exchange(判断有无正环)

    题目描述 Several currency exchange points are working in our city. Let us suppose that each point specia ...

  5. 【算法】Floyd-Warshall算法(任意两点间的最短路问题)(判断负圈)

    问题 求解任意两点间最短路问题也叫多源最短路径问题. 可解决途径 一种方法时把图中每个点当做源点重复算n次Dijkstra 算法(Dijkstra是求单源最短路径的算法),时间复杂度O(n^3),据说 ...

  6. POJ 1860 Currency Exchange 最短路+负环

    原题链接:http://poj.org/problem?id=1860 Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Tota ...

  7. Currency Exchange——最短路Bellman-Ford算法

    [题目描述] Several currency exchange points are working in our city. Let us suppose that each point spec ...

  8. POJ 3259 Wormholes【最短路/SPFA判断负环模板】

    农夫约翰在探索他的许多农场,发现了一些惊人的虫洞.虫洞是很奇特的,因为它是一个单向通道,可让你进入虫洞的前达到目的地!他的N(1≤N≤500)个农场被编号为1..N,之间有M(1≤M≤2500)条路径 ...

  9. LightOJ - 1074 Extended Traffic(最短路+判断负环)

    题目链接:点击查看 题目大意:给出n个城市,每个城市都有一个拥挤度,从a到b的时间是(b的拥挤度-a的拥挤度)^3,点1为起点,求最短时间 题目分析:这个题第一感觉是个裸题,n还非常小,偷了个懒上了一 ...

  10. 模板 - 判断负环(超时高效优化技巧)、01分数规划

    整理的算法模板合集: ACM模板 判断负环 判正环求最长路,判负环求最短路 int n; // 总点数 int h[N], w[N], e[N], ne[N], idx; // 邻接表存储所有边 in ...

最新文章

  1. OpenCV图像处理——修复失焦模糊的图像
  2. ML激活函数使用法则
  3. GitHub 4K+Star!SpaceX火箭数据开放API接口,可用Python进行抓取分析
  4. 连载:阿里巴巴大数据实践—实时技术
  5. 深度解密Go语言之unsafe
  6. localstorage || globalStorage || userData
  7. oracle组合数据类型,oracle复合数据类型-相关方法
  8. 基于libuv的TCP设计(二)
  9. 蓝软服务器文件监控同步系统,蓝软7000ERP通用操作使用教程
  10. CSS3相比CSS新增哪些功能
  11. js实现网页在线聊天功能(四)
  12. Oracle扩展redo
  13. highcharts pie ajax,Basic Pie
  14. 康考迪亚大学应用计算机科学,康考迪亚大学.pdf
  15. Windows配置互联网访问检测服务器-IspSrv
  16. 用PS制作动态雪景的教程
  17. 演讲如何克服紧张情绪
  18. 学生晚上回宿舍时其在实验室的计算机主机,学生晚上回宿舍时, 其在实验室的计算机主机应关闭, 显示器一般不用关。...
  19. Swift对接C++库
  20. PS高阶操作之木质纹理

热门文章

  1. 用工厂流水线的方式来理解 RxJava 的概念
  2. python之Beautiflusoup操作
  3. [Spark]-编译(2.3.1)部署(YARN-Cluster)
  4. ArrayList源码解析
  5. 【原创】编程题练习:头插法尾插法建立单链表及找寻单链表中的倒数第K个节点...
  6. HDU 3065 病毒侵袭持续中(AC自动机 模板)题解
  7. 日记【2010-6-2】
  8. Boost Log : Trivial logging with filters
  9. 对第三组博客的检查情况
  10. [js开源组件开发]js文本框计数组件