POJ 1860 Currency Exchange(SPFA)

判断是否存在负环/正环最短路/最长路问题,把每种货币当作节点,兑换货币就是图上的有向边,使用SPFA查找是否会让起点进入一个正环就行了

C++ Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <queue>
#define MAX 101
#define INF 1e9

using namespace std;

double rate[MAX][MAX];
double com[MAX][MAX];
queue<int> q;
bool isInqueue[MAX];
double dis[MAX];
int n, m, start;
double v;
int c1, c2;

bool spfa(){
dis[start] = v;
q.push(start);
isInqueue[start] = true;
while(!q.empty()){
int cur = q.front();
q.pop();
isInqueue[cur] = false;
for(int i = 1; i <= n; i++){
if(dis[i] < (dis[cur] - com[cur][i])*rate[cur][i]){
dis[i] = (dis[cur] - com[cur][i])*rate[cur][i];
if(!isInqueue[i]){
q.push(i);
isInqueue[i] = true;
}
}
}
if(dis[start] > v)
return true;
}
return false;
}

int main(){
while(~scanf("%d%d%d%lf", &n, &m, &start, &v)){
memset(rate, 0, sizeof(rate));
memset(com, 0, sizeof(com));
memset(isInqueue, 0, sizeof(isInqueue));
for(int i = 0; i < MAX; i++){
dis[i] = -INF;
}
while(!q.empty()){
q.pop();
}
for(int i = 0; i < m; i++){
scanf("%d%d", &c1, &c2);
scanf("%lf%lf%lf%lf", &rate[c1][c2], &com[c1][c2], &rate[c2][c1], &com[c2][c1]);
}
bool isIncrease = spfa();
if(isIncrease){
printf("YES\n");
}else{
printf("NO\n");
}
}
return 0;
}