POJ1860 Currency Exchange(最短路)
题目链接。
分析:
以前没做出来,今天看了一遍题竟然直接A了。出乎意料。
大意是这样,给定不同的金币的编号,以及他们之间的汇率、手续费,求有没有可能通过不断转换而盈利。
直接用Bellman-ford检测负环的方法检测。
#include <iostream>
#include <cstdio>
#include <cstring> using namespace std; const int maxn = ;
const int inf = (<<); struct Node {
int u, v;
double r, c;
int next;
}node[maxn]; int n, m, s, cn, head[];
double q, d[]; void add_edge(int u, int v, double r, double c) {
node[cn].u = u;
node[cn].v = v;
node[cn].r = r;
node[cn].c = c;
node[cn].next = head[u];
head[u] = cn++;
} bool bellman_ford(int s) {
for(int i=; i<n; i++) d[i] = -inf;
d[s] = q; for(int i=; i<n-; i++) {
for(int i=; i<cn; i++) {
Node &e = node[i]; if(d[e.u] != -inf && (d[e.u]-e.c)*e.r>d[e.v]) {
d[e.v] = (d[e.u]-e.c)*e.r;
}
}
} for(int i=; i<cn; i++) {
Node &e = node[i]; if(d[e.u] != -inf && (d[e.u]-e.c)*e.r>d[e.v]) {
return true;
}
}
return false;
} int main() {
int u, v;
double r1, c1, r2, c2; while(scanf("%d %d %d %lf", &n, &m, &s, &q) == ) {
cn = ; memset(head, -, sizeof(head)); for(int i=; i<m; i++) {
scanf("%d %d %lf %lf %lf %lf", &u, &v, &r1, &c1, &r2, &c2);
u--; v--;
add_edge(u, v, r1, c1);
add_edge(v, u, r2, c2);
} if(bellman_ford(s-)) printf("YES\n");
else printf("NO\n");
} return ;
}
POJ1860 Currency Exchange(最短路)的更多相关文章
- POJ1860——Currency Exchange(BellmanFord算法求最短路)
Currency Exchange DescriptionSeveral currency exchange points are working in our city. Let us suppos ...
- POJ1860 Currency Exchange【最短路-判断环】
Several currency exchange points are working in our city. Let us suppose that each point specializes ...
- POJ 1860 Currency Exchange 最短路+负环
原题链接:http://poj.org/problem?id=1860 Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Tota ...
- POJ1860 Currency Exchange(bellman-ford)
链接:http://poj.org/problem?id=1860 Currency Exchange Description Several currency exchange points are ...
- POJ 1860 Currency Exchange (最短路)
Currency Exchange Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u S ...
- POJ1860:Currency Exchange(BF)
http://poj.org/problem?id=1860 Description Several currency exchange points are working in our city. ...
- poj1860 Currency Exchange(spfa判断正环)
Description Several currency exchange points are working in our city. Let us suppose that each point ...
- POJ1860 Currency Exchange —— spfa求正环
题目链接:http://poj.org/problem?id=1860 Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Tota ...
- POJ-1860 Currency Exchange( Bellman_Ford, 正环 )
题目链接:http://poj.org/problem?id=1860 Description Several currency exchange points are working in our ...
随机推荐
- PreTranslateMessage作用和用法
PreTranslateMessage作用和用法 PreTranslateMessage是消息在送给TranslateMessage函数之前被调用的,绝大多数本窗体的消息都要通过这里,比較经常使用, ...
- MYSQL 体系结构图-log (踏雪无痕) (UC技术博客)
http://www.cnblogs.com/chenpingzhao/category/690116.html http://www.cnblogs.com/chenpingzhao/p/51074 ...
- XML的四种解析方式
本文描述了构建良好的XML需要遵循的规则.作者详细介绍了构建XML需要考虑的元素,如何命名约定.正确的标记嵌套.属性规则.声明和实体,以及DTD和schema的验证,十分便于新手开始学习了解XML. ...
- PHP接口的声明与引用
PHP接口的声明与引用 <?php//遵循规律:先继承,后接口//单继承,多接口class lei{function fangfa(){return "中国电信提醒您:<br&g ...
- SPOJ 3943 - Nested Dolls 最长不下降子序列LIS(二分写法)
现在n(<=20000)个俄罗斯套娃,每个都有宽度wi和高度hi(均小于10000),要求w1<w2并且h1<h2的时候才可以合并,问最少能剩几个. [LIS]乍一看跟[这题]类似, ...
- javascript实现Map
function Map() { this.elements = new Array(); // 获取MAP元素个数 this.size = function() { return this.elem ...
- 此方法显式使用的 CAS 策略已被 .NET Framework 弃用
用vs2008开发的应用程序在vs2012中打开时提示如下: 此方法显式使用的 CAS 策略已被 .NET Framework 弃用.若要出于兼容性原因而启用 CAS 策略,请使用 NetFx40_L ...
- DropDownList 控件
今天打算学习下dropdownlist控件的取值,当你通过数据库控件或dataset绑定值后,但又希望显示指定的值,这可不是简单的值绑定就OK,上网搜了一些资料,想彻底了解哈,后面发现其中有这么大的奥 ...
- js监听滚动条事件
(function () { if(document.addEventListener){ document.addEventListener('mousewheel',scrollFunc,fals ...
- Shell 脚本编程笔记(一) Hello Shell
最近不断在接触Linux操作系统,对它一个终端走天下的特性感到十分新奇和伟大.同时也被各种命令折磨的死去活来...公司的一个老同事给我讲,在公司的极品geek宅都是只用一个黑黑的框完成一切的.结果我一 ...