【SPFA】POJ1860-Currency Exchange

【题目大意】

给出每两种货币之间交换的手续费和汇率,求出从当前货币s开始交换,能否赚。

【思路】

反向运用SPFA,判断是否有正环。每次队首元素出队之后,判断一下到源点s的距离是否增大,增大则返回true。一开始判断正环的思路如果有一个元素入队超过n次,则说明存在正环。后来发现这个思路是不适用的,因为这个正环中并不一定包含源点..要注意的是:题目中的m并不是边数,边数应该是m*2!所以开数组的时候要尤其注意数组不能开小了。由于C++的判定中如果没有崩掉,不会返回RE只会返回WA,我纠结了好久才找出错因。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<queue>
 5 using namespace std;
 6 struct Rec
 7 {
 8     int ori,des;
 9     double len,val;
10 };
11 const int MAXN=100+5;
12 int first[MAXN],next[MAXN*2];
13 /*用于存储邻接表*/
14 double dis[MAXN];
15 /*存储从s出发到达当前货币的最大值*/
16 int vis[MAXN];
17 /*判断某一个点是否已经存在于队列中*/
18 Rec edge[MAXN*2];
19 int n,m,s;
20 double v;
21
22 bool SPFA()
23 {
24     memset(dis,0,sizeof(dis));
25     memset(vis,0,sizeof(vis));
26     queue<int> que;
27
28     dis[s]=v;
29     /*默认到持有货币的距离为持有货币的价值*/
30     vis[s]=1;
31     que.push(s);
32
33     while (!que.empty())
34     {
35         int pos=que.front();
36         int k=first[pos];
37         vis[pos]=0;
38         que.pop();
39         while (k!=-1)
40         {
41             if (dis[edge[k].des]<(dis[pos]-edge[k].len)*edge[k].val)
42             {
43                 dis[edge[k].des]=(dis[pos]-edge[k].len)*edge[k].val;
44                 if (!vis[edge[k].des])
45                 {
46                     vis[edge[k].des]=1;
47                     que.push(edge[k].des);
48                 }
49             }
50             k=next[k];
51         }
52         //if (dis[s]>v) return true;
53     }
54     return false;
55 }
56
57 int main()
58 {
59     scanf("%d%d%d%lf",&n,&m,&s,&v);
60     s--;
61     memset(first,-1,sizeof(first));
62
63     for (int i=0;i<m;i++)
64     {
65         int a,b;
66         double c1,r1,c2,r2;
67         scanf("%d%d%lf%lf%lf%lf",&a,&b,&r1,&c1,&r2,&c2);
68         a--;
69         b--;
70         edge[i*2].ori=edge[i*2+1].des=a;
71         edge[i*2].des=edge[i*2+1].ori=b;
72         edge[i*2].val=r1;
73         edge[i*2].len=c1;
74         edge[i*2+1].val=r2;
75         edge[i*2+1].len=c2;
76
77         next[i*2]=first[a];
78         first[a]=i*2;
79         next[i*2+1]=first[b];
80         first[b]=i*2+1;
81     }
82     m=m*2+2;
83     if (SPFA()) cout<<"YES"<<endl;
84                 else cout<<"NO"<<endl;
85     //system("pause");
86     return 0;
87 }
时间: 2024-08-08 09:07:10

【SPFA】POJ1860-Currency Exchange的相关文章

【POJ】1860 Currency Exchange

真是气skr人..没把d[]换成double...de了一上午的bug// 记得用G++提交啊 题目链接:http://poj.org/problem?id=1860 题意:告诉你n个点,m条路.起始点s,还有初始金额money.每条路表示从a->b的汇率和佣金以及b->a的汇率和佣金.你在该点所得是(本金-佣金)*汇率.问你这个人能不能赚钱. 题解:spfa套一下//.记得d[]换成double.具体的看看代码.QWQ. 代码: 1 #include<iostream> 2 #i

HDU1217 Arbitrage 【SPFA】

Arbitrage Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4460    Accepted Submission(s): 2032 Problem Description Arbitrage is the use of discrepancies in currency exchange rates to transform

HDU1224 Free DIY Tour 【SPFA】

Free DIY Tour Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3939    Accepted Submission(s): 1262 Problem Description Weiwei is a software engineer of ShiningSoft. He has just excellently fulf

boj 454 帮帮小叮当【SPFA】

题目链接:http://code.bupt.edu.cn/problem/p/454/ 454. 帮帮小叮当 时间限制5000 ms 内存限制 65536 KB 题目描述 小叮当刚刚学会了传送门的使用方法,可是它不小心跌落到二维空间一个 n * m 的矩阵格子世界的入口(1,1)处, 他得知出口在(n,m)处,每穿越一个格子门,它的体力值会下降. 又饿又累的他 IQ 已经降为负数了,聪明的你,能帮他规划一下路线,使得它体力值下降的最少吗? 每一行有且仅有一个传送门,但是小叮当上课睡着了,只学会了

HDU1142 A Walk Through the Forest 【SPFA】+【记忆化搜索】

A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5688    Accepted Submission(s): 2089 Problem Description Jimmy experiences a lot of stress at work these days, especial

POJ 2449 Remmarguts&#39; Date【SPFA】【A*】

Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 21978 Accepted: 5982 Description "Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly touching his little ducks' head, he to

POJ2253&amp;ZOJ1942--Frogger【SPFA】单源最短路变形

链接:http://poj.org/problem?id=2253 题意:一个青蛙在一块石头上,看到了另一个青蛙在另一块石头上,它想跳过去找它,如果距离太远它就需要借助别的石头当跳板,两块石头之间的青蛙距离被定义成两块石头之间所有路径中最大跳跃距离的最小值,求两个青蛙之间的青蛙距离. poj2263和它类似,链接:http://poj.org/problem?id=2263 解题报告:Here 这是最短路的变形,每两点之间都有路可以跳,更新最短路的值,权值记录成目前到这一点的最小青蛙距离就行了

HDU3339 In Action 【SPFA】+【01背包】

In Action Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4099    Accepted Submission(s): 1306 Problem Description Since 1945, when the first nuclear bomb was exploded by the Manhattan Project

POJ-1860 Currency Exchange 【spfa判负环】

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 be several points specializing in the

POJ1860 Currency Exchange【BellmanFord算法】【求正权回路】

Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 20994 Accepted: 7522 Description Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and perfor