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)的更多相关文章

  1. Currency Exchange POJ - 1860 (spfa判断正环)

    Several currency exchange points are working in our city. Let us suppose that each point specializes ...

  2. POJ Wormholes (SPFA)

    http://poj.org/problem?id=3259 Description While exploring his many farms, Farmer John has discovere ...

  3. 模板C++ 03图论算法 1最短路之单源最短路(SPFA)

    3.1最短路之单源最短路(SPFA) 松弛:常听人说松弛,一直不懂,后来明白其实就是更新某点到源点最短距离. 邻接表:表示与一个点联通的所有路. 如果从一个点沿着某条路径出发,又回到了自己,而且所经过 ...

  4. POJ题目(转)

    http://www.cnblogs.com/kuangbin/archive/2011/07/29/2120667.html 初期:一.基本算法:     (1)枚举. (poj1753,poj29 ...

  5. 最短路(SPFA)

    SPFA是Bellman-Ford算法的一种队列实现,减少了不必要的冗余计算. 主要思想是: 初始时将起点加入队列.每次从队列中取出一个元素,并对所有与它相邻的点进行修改,若某个相邻的点修改成功,则将 ...

  6. Bellman-Ford算法及其队列优化(SPFA)

    一.算法概述 Bellman-Ford算法解决的是一般情况下的单源最短路径问题.所谓单源最短路径问题:给定一个图G=(V,E),我们希望找到从给定源结点s属于V到每个结点v属于V的最短路径.单源最短路 ...

  7. Repeater POJ - 3768 (分形)

    Repeater POJ - 3768 Harmony is indispensible in our daily life and no one can live without it----may ...

  8. Booksort POJ - 3460 (IDA*)

    Description The Leiden University Library has millions of books. When a student wants to borrow a ce ...

  9. Radar Installation POJ - 1328(贪心)

    Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. ...

随机推荐

  1. 配置文件类 Properties

    Properties(配置文件类): 主要用于生产配置文件与读取配置文件的信息. Properties属于集合类,继承于Hashtable. Properties要注意的细节:    1. 如果配置文 ...

  2. js动态创建style节点(js文件中添加css)

    ie6 不能 document.createElement('style') 然后append到head标签里.所以就找到这样个好文章 --------------------- 有很多提供动态创建 ...

  3. animate支持的css属性

    支持下列CSS 样式 * backgroundPosition * borderWidth * borderBottomWidth * borderLeftWidth * borderRightWid ...

  4. 【swift学习笔记】一.页面转跳的条件判断和传值

    在转跳页面之前我们可能会判断一下是否有输入正确的数据,如下图所示,如果没有数据输入则弹出提示,不进行转跳. 一:页面转跳 1.第一个页面的上放上一个Label一个textfield和一个按钮 2.在第 ...

  5. Json——js和C#对Json的操作

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式.博主记得几年前在华为外包项目中有一个和Android应用交互的需求,Andr ...

  6. Unable to load R3 module D:\Program Files\Oracle\VirtualBox/VBoxDD.DLL (VBoxDD): GetLastError=1790 (VERR_UNRESOLVED_ERROR).

    Unable to load R3 module D:\Program Files\Oracle\VirtualBox/VBoxDD.DLL (VBoxDD): GetLastError=1790 ( ...

  7. iOS多线程的详情使用示例--简进祥

    大家都知道,在开发过程中应该尽可能减少用户等待时间,让程序尽可能快的完成运算.可是无论是哪种语言开发的程序最终往往转换成汇编语言进而解释成机器码来执行.但是机器码是按顺序执行的,一个复杂的多步操作只能 ...

  8. SingleTon单例模式总结篇

    在Java设计模式中,单例模式相对来说算是比较简单的一种构建模式.适用的场景在于:对于定义的一个类,在整个应用程序执行期间只有唯一的一个实例对象. 一,懒汉式: 其特点是延迟加载,即当需要用到此单一实 ...

  9. 区间第K大(一)

    Problem: 给定无序序列S:[b, e),求S中第K大的元素. Solution 1.裸排序 2.现将区间均分成两段,S1, S2,对S1,S2分别排序,然后

  10. php判断中文,英文, 数字

    exeg Warning  This function was DEPRECATED in PHP 5.3.0, and REMOVED in PHP 7.0.0. function checkStr ...