【最短路】 ZOJ 1544 Currency Exchange 推断负圈
给出 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 推断负圈的更多相关文章
- POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环)
POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环) Description Several currency ...
- (最短路 SPFA)Currency Exchange -- poj -- 1860
链接: http://poj.org/problem?id=1860 Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 2326 ...
- UVA 11090 Going in Cycle!!(Bellman-Ford推断负圈)
题意:给定一个n个点m条边的加权有向图,求平均权值最小的回路. 思路:使用二分法求解.对于每个枚举值mid,推断每条边权值减去mid后有无负圈就可以. #include<cstdio> # ...
- POJ 1860 Currency Exchange 最短路+负环
原题链接:http://poj.org/problem?id=1860 Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Tota ...
- 最短路(Bellman_Ford) POJ 1860 Currency Exchange
题目传送门 /* 最短路(Bellman_Ford):求负环的思路,但是反过来用,即找正环 详细解释:http://blog.csdn.net/lyy289065406/article/details ...
- POJ 1860 Currency Exchange (最短路)
Currency Exchange Time Limit : 2000/1000ms (Java/Other) Memory Limit : 60000/30000K (Java/Other) T ...
- POJ 1860 Currency Exchange (bellman-ford判负环)
Currency Exchange 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/E Description Several c ...
- POJ 1860——Currency Exchange——————【最短路、SPFA判正环】
Currency Exchange Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u S ...
- Currency Exchange(最短路)
poj—— 1860 Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 29851 Ac ...
随机推荐
- AngularJS学习篇(二十二)
AngularJS 依赖注入 什么是依赖注入 wiki 上的解释是:依赖注入(Dependency Injection,简称DI)是一种软件设计模式,在这种模式下,一个或更多的依赖(或服务)被注入(或 ...
- dubbo专题」dubbo其实很简单,就是一个远程服务调用的框架(1)
一.dubbo是什么? 1)本质:一个Jar包,一个分布式框架,,一个远程服务调用的分布式框架. 既然是新手教学,肯定很多同学不明白什么是分布式和远程服务调用,为什么要分布式,为什么要远程调用.我简单 ...
- CLR之委托的揭秘(一)
初识委托: 在之前的学习中我们已经可以把对象,值,数组当作参数传递给方法,但是有没有可能把方法也当作参数传递给方法呢?有了这个想法于是就有了委托.方法当作一种参数去传递,但是方法有的 ...
- Python之re正则模块二
13.编译的标志 可以用re.I.re.M等参数,也可以直接在表达式中添加"?(iLmsux)"标志 *s:单行,“.”匹配包括换行符在内的所有字符 *i:忽略大小写 *L:让&q ...
- ORA-01745: 无效的主机/绑定变量名 ORA-00917: 缺失的逗号 oracle日期格式错误
今天在oracle中执行插入语句的时候报了一个奇怪的错误,在程序中报的错误是ORA-01745: 无效的主机/绑定变量名,网上一查说是缺失逗号,在查询分析器执行的时候报缺失的逗号,仔细看了一下也没有缺 ...
- 原码、反码、补码的正(nao)确(can)打开方式
我们知道日常生活中使用的数分为整数和实数,整数的小数点固定在数的最右边,可以省略不写,而实数的小数点则不固定.在计算机中只能识别和表示“0”和“1”,而无法识别小数点,因此要想使得计算机能够处理日常使 ...
- java并发之线程执行器(Executor)
线程执行器和不使用线程执行器的对比(优缺点) 1.线程执行器分离了任务的创建和执行,通过使用执行器,只需要实现Runnable接口的对象,然后把这些对象发送给执行器即可. 2.使用线程池来提高程序的性 ...
- js 图片转换为base64
<input id="file" type="file"> <img id="img" style="max-h ...
- iOS上new Date异常解决办法
最近有一个项目要实现使用Angluar写一个简历模板, 用户输入姓名/生日/简介...等内容, 然后生成一份在线的简历 后来测试时遇到简历模板在Android手机跟Google浏览器上根据生日计算得出 ...
- Win32API起始处的mov edi, edi与用户空间InlineHook
在x86平台上,无论是在调试器中跟到系统DLL中时,还是反汇编某个系统DLL时,经常会发现很多API的第一条汇编指令都是mov edi, edi.根据经验来讲,C函数的汇编形式,应该是首先push e ...