给出 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_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int 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;
}

【最短路】 ZOJ 1544 Currency Exchange 推断负圈的更多相关文章

  1. POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环)

    POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环) Description Several currency ...

  2. (最短路 SPFA)Currency Exchange -- poj -- 1860

    链接: http://poj.org/problem?id=1860 Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 2326 ...

  3. UVA 11090 Going in Cycle!!(Bellman-Ford推断负圈)

    题意:给定一个n个点m条边的加权有向图,求平均权值最小的回路. 思路:使用二分法求解.对于每个枚举值mid,推断每条边权值减去mid后有无负圈就可以. #include<cstdio> # ...

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

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

  5. 最短路(Bellman_Ford) POJ 1860 Currency Exchange

    题目传送门 /* 最短路(Bellman_Ford):求负环的思路,但是反过来用,即找正环 详细解释:http://blog.csdn.net/lyy289065406/article/details ...

  6. POJ 1860 Currency Exchange (最短路)

    Currency Exchange Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 60000/30000K (Java/Other) T ...

  7. POJ 1860 Currency Exchange (bellman-ford判负环)

    Currency Exchange 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/E Description Several c ...

  8. POJ 1860——Currency Exchange——————【最短路、SPFA判正环】

    Currency Exchange Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u S ...

  9. Currency Exchange(最短路)

    poj—— 1860 Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 29851   Ac ...

随机推荐

  1. Visual formatting model 可视化格式模型

    在可视化格式模型里,每个元素都是由一个或者多个盒子组成,这些盒子的布局由下面规则控制: box dimensions and type.(块元素和行内元素) positioning scheme (n ...

  2. JAVAscript学习笔记 js事件 第一节 (原创) 参考js使用表

    <!DOCTYPE html> <html lang="en" onUnload="ud()"> <head> <me ...

  3. iOS隐藏导航栏底部灰线

    首先创建 p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #de38a5 } span.s1 { } span.s ...

  4. java 中 针对数组进行的工具类

    1.遍历数组的方法: public static void printfArray(int[] arr)  2. 获取数组中最大值: public static int getMax(int[] ar ...

  5. 一起写框架-Ioc内核容器的实现-基础功能-ComponentScan支持组件注解限制(七)

    实现功能 以上的代码我们发现.我们都是将@ComponentScan扫描的路径下的所有类都加载到容器中的. 而实际需求,我们并不希望所有的类都创建对象,而是加了组件注解@Controller,@Ser ...

  6. C# 控件的缩写

    1 btn Button 2 chk CheckBox 3 ckl CheckedListBox 4 cmb ComboBox 5 dtp DateTimePicker 6 lbl Label 7 l ...

  7. 解决webstorm启动索引文件卡死问题

    问题 当目录下的文件数量较大时,用webstorm打开会出现卡顿,甚至卡死现象,例如:node_modules目录 解决方案 不让webstorm索引该目录下的文件步骤:1.node_modules目 ...

  8. [转载] 深入剖析 redis 主从复制

    转载自http://www.cnblogs.com/daoluanxiaozi/p/3724299.html 主从概述 redis 支持 master-slave(主从)模式,redis server ...

  9. 69、django之Form组件

    本篇导航: 小试牛刀 Form类 常用选择插件 自定义验证规则 初始化数据 Django的Form主要具有一下几大功能: 生成HTML标签 验证用户数据(显示错误信息) HTML Form提交保留上次 ...

  10. JavaScript学习笔记(八)——变量的作用域与解构赋值

    在学习廖雪峰前辈的JavaScript教程中,遇到了一些需要注意的点,因此作为学习笔记列出来,提醒自己注意! 如果大家有需要,欢迎访问前辈的博客https://www.liaoxuefeng.com/ ...