POJ 1860(spfa)

http://poj.org/problem?id=1860

题意:汇率转换,与之前的2240有点类似,不同的是那个题它去换钱的时候,是不需要手续费的,这个题是需要手续费的,这是个很大的不同。

思路:还是转换成为最短路的问题,主要的困难也就是关于它的松弛方程。dist [edge[i].v] < (dist[ tmp ] - edge[ i ].r) * edge[ i ].c  。这个就是松弛方程,当它的钱的数目增多的时候松弛。

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <queue>
 4
 5 using namespace std;
 6
 7
 8 int m,n,p,head[ 10005 ],pos,num[ 10005 ];
 9 bool vis[ 10005 ];
10 double val,dist[ 10005 ];
11
12 struct note{
13     int v,next;
14     double r,c;
15 }edge[ 10005 ];
16
17 void add(int x,int v,double r,double c)
18 {
19     edge[ pos ].v = v;
20     edge[ pos ].r = r;
21     edge[ pos ].c = c;
22     edge[ pos ].next = head[ x ];
23     head[ x ] = pos ++;
24 }
25
26 bool spfa()
27 {
28     queue<int >s;
29     s.push(p);
30     dist[ p ] = val;
31     vis[ p ] = true;
32     num[ p ] ++;
33     while(!s.empty())
34     {
35         int tmp = s.front();
36         s.pop();
37         vis[ tmp ] = false;
38         for( int i = head[ tmp ] ; i != -1 ; i = edge[ i ].next)
39         {
40             if( dist[ edge[ i ].v ] < (dist[ tmp ] - edge[ i ].c ) * edge[ i ].r )
41             {
42                 dist[ edge[ i ].v ] = (dist[ tmp ] - edge[ i ].c ) * edge[ i ].r;
43                 if( !vis[ edge[ i ].v ] )
44                 {
45                     s.push( edge[ i ].v );
46                     vis[ edge[ i ].v ] = true;
47                     num[ edge[ i ].v ] ++;     //可能构成正权回路,这个是用来判断的。但次数比m次要大的时候,就肯定是增多的。可以直接return 。
48                     if( num[edge[ i ].v] > m)
49                         return true;
50                 }
51             }
52         }
53     }
54     if( dist[ p ] > val ) return true;   //如果之后的钱比之前的多,也return true.
55     return false;
56 }
57
58 int main()
59 {
60    // freopen("in.txt","r",stdin);
61     int a,b;
62     double r1,c1,r2,c2;
63     pos = 1;
64     memset( head , -1 , sizeof( head ) );
65     memset( dist , 0 , sizeof( dist ) );
66     memset( vis , false ,sizeof( vis ) );
67     memset( num , 0 , sizeof( num ) );
68     scanf("%d%d%d%lf",&m,&n,&p,&val);
69     for( int i = 1 ; i <= n ; i++ )
70     {
71         scanf("%d%d%lf%lf%lf%lf",&a,&b,&r1,&c1,&r2,&c2);
72         add(a,b,r1,c1);
73         add(b,a,r2,c2);
74     }
75     if(spfa()) printf("YES\n");
76     else printf("NO\n");
77     return 0;
78 }
时间: 2024-08-27 14:48:46

POJ 1860(spfa)的相关文章

POJ Wormholes (SPFA)

http://poj.org/problem?id=3259 Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFOR

poj 1860 Currency Exchange(SPFA)

题目链接:http://poj.org/problem?id=1860 Description Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can b

POJ 2253-Frogger (Prim)

题目链接:Frogger 题意:两只青蛙,A和B,A想到B哪里去,但是A得弹跳有限制,所以不能直接到B,但是有其他的石头作为过渡点,可以通过他们到达B,问A到B的所有路径中,它弹跳最大的跨度的最小值 PS:最小生成树过的,刚开始用Dijstra做,Kao,精度损失的厉害,对于Dijksra的变形不大会变啊,看了Discuss有人用最小生成树过,我一划拉,还真是,敲了,就过了,等会研究研究最短路的各种变形,有模板不会变,不会灵活应用,渣渣就是渣渣. ME               Time 10

poj 1861(最小生成树)

Description Andrew is working as system administrator and is planning to establish a new network in his company. There will be N hubs in the company, they can be connected to each other using cables. Since each worker of the company must have access

HDU 2680Choose the best route (SPFA)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2680 百度之星编程大赛--您报名了吗? 杭电ACM 2014暑期集训队--选拔安排~ Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis

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

3.1最短路之单源最短路(SPFA) 松弛:常听人说松弛,一直不懂,后来明白其实就是更新某点到源点最短距离. 邻接表:表示与一个点联通的所有路. 如果从一个点沿着某条路径出发,又回到了自己,而且所经过的边上的权和小于0, 就说这条路是一个负权回路. 回归正题,SPFA是bellman-ford的一种改进算法,由1994年西安交通大学段凡丁提出.它无法处理带有负环的图,判断方法:如果某个点进入队列的次数超过N次则存在负环. SPFA的两种写法,bfs和dfs,bfs判别负环不稳定,相当于限深度搜索

POJ 2352 (stars)

[题意描述] 就是给定n个星星的x,y坐标,y坐标按照从小到大的顺序进行排列,x坐标随机排列.下面求对于每个星星而言,其它星星的x,y的坐标都小于等于该星星的数目,然后输出所有的情况. [思路分析] 我们这道题可以采用树状数组求解,将x+1作为树状数组的底标. [AC代码] #include<iostream> #include<bitset> #include<cstdio> #include<cstring> #include<algorithm&

poj Sudoku(数独) DFS

Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13665   Accepted: 6767   Special Judge Description Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller squares 3x3 as shown on the Figure.

POJ 3419 (rmq)

这道题是rmq,再加上一个解决溢出. 刚开始我也想过用rmq,虽然不知道它叫什么,但是我知道应该这样做.可是后来没想到这道题的特殊性,也就是解决溢出的方法,就放弃了. rmq可以用线段树,也可以用dp.  这道题都可以过的,而且线段树要快一些. #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; #define m