POJ1860 Currency Exchange 发表于 2017-09-23 | 分类于 algorithm 题目大意 POJ1860 给定一个带有点权和边权的图, 求是否存在正权回路. 思路 由于Bellman-Ford算法可以求图中是否存在负权回路. 因此可以将 dis数组的初始状态置为 0, 求是否存在正权回路. 代码123456789101112131415161718192021222324252627282930313233343536373839404142434445464748#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;}