标签:
题目链接: http://poj.org/problem?id=1860
题解:
#include <iostream> #include <cstdio> #include <cstring> #include <stdlib.h> #include <math.h> #include <queue> #include <algorithm> using namespace std; #define N 210 #define INF 0xfffffff double dist[N], V; int cnt, Head[N], num[N], vis[N]; int n, m, s; struct Edge { int v, next; double r, c; }e[N]; void Add(int u, int v, double r, double c) { e[cnt].v = v; e[cnt].r = r; e[cnt].c = c; e[cnt].next = Head[u]; Head[u] = cnt++; } bool spfa()///spfa模板; { memset(vis, 0, sizeof(vis)); memset(num, 0, sizeof(num)); queue<int>Q; vis[s] = 1; dist[s] = V; Q.push(s); num[s]++; while(Q.size()) { int p=Q.front(); Q.pop(); vis[p] = 0; for(int i=Head[p]; i!=-1; i=e[i].next) { int q = e[i].v; if(dist[q] < (dist[p] - e[i].c) * e[i].r)///注意松弛的变化; { dist[q] = (dist[p] - e[i].c) * e[i].r; if(!vis[q]) { vis[q] = 1; Q.push(q); num[q] ++; if(num[q]>n) return true;///存在正权回路; } } } } if(dist[s]>V)///最长路后,实现了增值; return true; return false; } int main() { int a, b; double rab, rba, cab, cba; while(scanf("%d%d%d%lf", &n, &m, &s, &V)!=EOF) { cnt = 0; memset(Head, -1, sizeof(Head)); memset(dist, 0, sizeof(dist)); for(int i=1; i<=m; i++) { scanf("%d%d%lf%lf%lf%lf", &a, &b, &rab, &cab, &rba, &cba); Add(a, b, rab, cab); Add(b, a, rba, cba); } if( spfa() ) printf("YES\n"); else printf("NO\n"); } return 0; }
Currency Exchange---poj1860 ( spfa, 回路,最长路)
标签:
原文地址:http://www.cnblogs.com/zhengguiping--9876/p/4798985.html