POJ 1860(spfa)
http://poj.org/problem?id=1860
题意:汇率转换,与之前的2240有点类似,不同的是那个题它去换钱的时候,是不需要手续费的,这个题是需要手续费的,这是个很大的不同。
思路:还是转换成为最短路的问题,主要的困难也就是关于它的松弛方程。dist [edge[i].v] < (dist[ tmp ] - edge[ i ].r) * edge[ i ].c 。这个就是松弛方程,当它的钱的数目增多的时候松弛。
#include <stdio.h>
#include <string.h>
#include <queue> using namespace std; int m,n,p,head[ ],pos,num[ ];
bool vis[ ];
double val,dist[ ]; struct note{
int v,next;
double r,c;
}edge[ ]; void add(int x,int v,double r,double c)
{
edge[ pos ].v = v;
edge[ pos ].r = r;
edge[ pos ].c = c;
edge[ pos ].next = head[ x ];
head[ x ] = pos ++;
} bool spfa()
{
queue<int >s;
s.push(p);
dist[ p ] = val;
vis[ p ] = true;
num[ p ] ++;
while(!s.empty())
{
int tmp = s.front();
s.pop();
vis[ tmp ] = false;
for( int i = head[ tmp ] ; i != - ; i = edge[ i ].next)
{
if( dist[ edge[ i ].v ] < (dist[ tmp ] - edge[ i ].c ) * edge[ i ].r )
{
dist[ edge[ i ].v ] = (dist[ tmp ] - edge[ i ].c ) * edge[ i ].r;
if( !vis[ edge[ i ].v ] )
{
s.push( edge[ i ].v );
vis[ edge[ i ].v ] = true;
num[ edge[ i ].v ] ++; //可能构成正权回路,这个是用来判断的。但次数比m次要大的时候,就肯定是增多的。可以直接return 。
if( num[edge[ i ].v] > m)
return true;
}
}
}
}
if( dist[ p ] > val ) return true; //如果之后的钱比之前的多,也return true.
return false;
} int main()
{
// freopen("in.txt","r",stdin);
int a,b;
double r1,c1,r2,c2;
pos = ;
memset( head , - , sizeof( head ) );
memset( dist , , sizeof( dist ) );
memset( vis , false ,sizeof( vis ) );
memset( num , , sizeof( num ) );
scanf("%d%d%d%lf",&m,&n,&p,&val);
for( int i = ; i <= n ; i++ )
{
scanf("%d%d%lf%lf%lf%lf",&a,&b,&r1,&c1,&r2,&c2);
add(a,b,r1,c1);
add(b,a,r2,c2);
}
if(spfa()) printf("YES\n");
else printf("NO\n");
return ;
}
POJ 1860(spfa)的更多相关文章
- Currency Exchange POJ - 1860 (spfa判断正环)
Several currency exchange points are working in our city. Let us suppose that each point specializes ...
- POJ Wormholes (SPFA)
http://poj.org/problem?id=3259 Description While exploring his many farms, Farmer John has discovere ...
- 模板C++ 03图论算法 1最短路之单源最短路(SPFA)
3.1最短路之单源最短路(SPFA) 松弛:常听人说松弛,一直不懂,后来明白其实就是更新某点到源点最短距离. 邻接表:表示与一个点联通的所有路. 如果从一个点沿着某条路径出发,又回到了自己,而且所经过 ...
- POJ题目(转)
http://www.cnblogs.com/kuangbin/archive/2011/07/29/2120667.html 初期:一.基本算法: (1)枚举. (poj1753,poj29 ...
- 最短路(SPFA)
SPFA是Bellman-Ford算法的一种队列实现,减少了不必要的冗余计算. 主要思想是: 初始时将起点加入队列.每次从队列中取出一个元素,并对所有与它相邻的点进行修改,若某个相邻的点修改成功,则将 ...
- Bellman-Ford算法及其队列优化(SPFA)
一.算法概述 Bellman-Ford算法解决的是一般情况下的单源最短路径问题.所谓单源最短路径问题:给定一个图G=(V,E),我们希望找到从给定源结点s属于V到每个结点v属于V的最短路径.单源最短路 ...
- Repeater POJ - 3768 (分形)
Repeater POJ - 3768 Harmony is indispensible in our daily life and no one can live without it----may ...
- Booksort POJ - 3460 (IDA*)
Description The Leiden University Library has millions of books. When a student wants to borrow a ce ...
- Radar Installation POJ - 1328(贪心)
Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. ...
随机推荐
- Qt——设计颜色编辑器
Qt中有一些封装好的对话框,比如QMessageBox.QColorDialog等,用途很广,但缺点是样式难以改变,直接拿来用可能与我们自己的系统样式不搭配,显得很难看. 所以我在无聊的时候,就自己写 ...
- 在iOS APP中使用H5显示百度地图时如何支持HTTPS?
现象: 公司正在开发一个iOSAPP,使用h5显示百度地图,但是发现同样的H5页面,在安卓可以显示出来,在iOS中就显示不出来. 原因分析: 但是现在iOS开发中,苹果已经要求在APP中的所有对外连接 ...
- LeetCode:Word Ladder I II
其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...
- Elasticssearch学习教程
http://www.cnblogs.com/eggTwo/p/4425269.html
- tomcat远程部署应用
Tomcat安装成功后,在ip地址:8080上就可以看见熟悉的首页,在这个首页中,上方有一个manage app按钮,点击就可以进行应用管理了.这样就不需要使用ftp把war包传上去了. 要想远程部署 ...
- Matlab2015入门学习01
1. 两个命令: clear: 清除内存中变量的值(在workspace中可以看到) clc: 清除Command Window中的输出 2. 脚本编辑器: matlab脚本扩展名为*.m 在Comm ...
- Python【第四章】:socket
ocket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求. socket起源于Uni ...
- bzoj 1305 dance跳舞
最大流. 首先二分答案,问题转化为x首舞曲是否可行. 考虑建图,对每个人建立三个点,分别表示全体,喜欢和不喜欢. 源点向每个男生全体点连一条容量为x的边. 每个男生整体点向喜欢点连一条容量为正无穷的边 ...
- 移动端webUI框架(HTML5手机框架)
淘宝SUI Mobile框架 官网地址:http://m.sui.taobao.org/ SUI Mobile 是一套基于 Framework7 开发的UI库.它非常轻量.精美,只需要引入我们的CDN ...
- MySQL索引背后的数据结构及算法原理
摘要 本文以MySQL数据库为研究对象,讨论与数据库索引相关的一些话题.特别需要说明的是,MySQL支持诸多存储引擎,而各种存储引擎对索引的支持也各不相同,因此MySQL数据库支持多种索引类型,如BT ...