Zhao70's Blog

POJ1860 Currency Exchange

题目大意

POJ1860

给定一个带有点权和边权的图, 求是否存在正权回路.

思路

由于Bellman-Ford算法可以求图中是否存在负权回路. 因此可以将 dis数组的初始状态置为 0, 求是否存在正权回路.

代码

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
#include <iostream>
#include <vector>
using namespace std;
struct Edge{
Edge(int a_, int b_, double r_, double c_)
: a(a_), b(b_), rate(r_), commi(c_){}
int a, b;
double rate, commi;
};
const int arrSize = 210;
int main(){
int nEdge, nVertex, amoCur;
double vCur;
cin >> nVertex >> nEdge >> amoCur >> vCur;
vector<Edge> edge;
for(int i = 0; i < nEdge; ++i){
int a, b;
cin >> a >> b;
double r, c;
cin >> r >> c;
edge.push_back(Edge(a, b, r, c));
cin >> r >> c;
edge.push_back(Edge(b, a, r, c));
}
double dis[arrSize];
for(int i = 0; i < nEdge * 2; i++)
dis[i] = 0;
dis[amoCur] = vCur;
for(int i = 0; i <= nVertex; i++){
bool flag = true;
for(int j = 0; j < edge.size(); j++){
if(dis[edge[j].b] < (dis[edge[j].a] - edge[j].commi) * edge[j].rate){
dis[edge[j].b] = (dis[edge[j].a] - edge[j].commi) * edge[j].rate;
flag = false;
}
}
if(flag)
break;
if(i == nVertex){
cout << "YES" << endl;
return 0;
}
}
cout << "NO" << endl;
return 0;
}