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 ...
随机推荐
- SAP-MM:发票、贷方凭证、事后借记、后续贷记
发票和事后借记 相同点:增加对供应商的应付款 不同点:针对同一订单收货,发票要先于事后借记(事后借记是对供应商后期发票金额的补充):发票和金额.订单数量有关系,而事后借记只是订单金额调整的凭证,仅仅是 ...
- 关于配置tnsnames来使用PLSQL连接数据库
关于配置tnsnames来使用PLSQL连接数据库 要想用ORACLE SQLdeveloper或者第三方工具PLSQLdeveloper.Toad等连接ORACLE数据库,必需要配置TNSnames ...
- [Angular 2] Passing data to components with 'properties'
Besides @Input(), we can also use properties on the @Component, to pass the data. import {Component, ...
- 改写URL的查询字符串QUERY_STRING(转)
查询字符串是指URL请求中“问号”后面的部分.比如,http://www.nowamagic.net/?foo=bar中粗体部分就是查询字符串,其中变量名是foo,值是bar. 1. 利用QSA转换查 ...
- android 用代码画虚线边框背景(转)
1.虚线画效果,可以使用Android中的xml来做. 2.直接上代码: <RelativeLayout android:id="@+id/coupon_popup" and ...
- 使用搬瓦工搭建javaweb环境
/* 本文是基于搬瓦工vps的centos-6-x86_64的Linux系统搭建. 需准备的工具:1.putty(用于连接Linux系统) 2.WinSCP(搬瓦工官方提供的ftp上传下载工 ...
- BestCoder 1st Anniversary
Souvenir Accepts: 1078 Submissions: 2366 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 26 ...
- Linux svn一次增加多个文件并批量上传
命令行下操作svn没有使用界面形式的TortoiseSVN直观,但是不管怎样,命令行下操作svn还是有它的有点,如果你碰到一次需要svn add许多个文件怎么办?下面的命令可以帮助你解决这个问题 一次 ...
- Oracle oerr使用
[oracle@cuug ~]$ oerr ora 01555 01555, 00000, "snapshot too old: rollback segment number %s wit ...
- OC - 20.多图下载
效果图 常见问题及解决方法 图片重复下载 将内存保存在内存或沙盒中. 若下载的图片量较大,则会出现UI界面不流畅的现象 在子线程中执行下载操作,然后回到主线程成中进行UI界面的刷新. 由于cell的循 ...