1. 程式人生 > >POJ 3621 Sightseeing Cows(最優比例環+SPFA檢測)

POJ 3621 Sightseeing Cows(最優比例環+SPFA檢測)

span fort exp ros 說明 6.0 lines choice stdio.h

Sightseeing Cows
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10306 Accepted: 3519

Description

Farmer John has decided to reward his cows for their hard work by taking them on a tour of the big city! The cows must decide how best to spend their free time.

Fortunately, they have a detailed city map showing the L

(2 ≤ L ≤ 1000) major landmarks (conveniently numbered 1.. L) and the P (2 ≤ P ≤ 5000) unidirectional cow paths that join them. Farmer John will drive the cows to a starting landmark of their choice, from which they will walk along the cow paths to a series of other landmarks, ending back at their starting landmark where Farmer John will pick them up and take them back to the farm. Because space in the city is at a premium, the cow paths are very narrow and so travel along each cow path is only allowed in one fixed direction.

While the cows may spend as much time as they like in the city, they do tend to get bored easily. Visiting each new landmark is fun, but walking between them takes time. The cows know the exact fun values Fi (1 ≤ Fi ≤ 1000) for each landmark i.

The cows also know about the cowpaths. Cowpath i connects landmark L

1i to L2i (in the direction L1i -> L2i ) and requires time Ti (1 ≤ Ti ≤ 1000) to traverse.

In order to have the best possible day off, the cows want to maximize the average fun value per unit time of their trip. Of course, the landmarks are only fun the first time they are visited; the cows may pass through the landmark more than once, but they do not perceive its fun value again. Furthermore, Farmer John is making the cows visit at least two landmarks, so that they get some exercise during their day off.

Help the cows find the maximum fun value per unit time that they can achieve.

Input

* Line 1: Two space-separated integers: L and P
* Lines 2..L+1: Line i+1 contains a single one integer: Fi
* Lines L+2..L+P+1: Line L+i+1 describes cow path i with three space-separated integers: L1i , L2i , and Ti

Output

* Line 1: A single number given to two decimal places (do not perform explicit rounding), the maximum possible average fun per unit time, or 0 if the cows cannot plan any trip at all in accordance with the above rules.

Sample Input

5 7
30
10
10
5
10
1 2 3
2 3 2
3 4 5
3 5 2
4 5 5
5 1 3
5 2 2

Sample Output

6.00

題目鏈接:POJ 3621

最優比例環的題,也是用01分數規劃寫,目的是找到一個環,使得該環上${\Sigma w_v \over \Sigma w_e}$最大,那麽我們設比例為r,當${\Sigma w_v \over \Sigma w_e}>r$時說明可以找到更大的r‘作為答案,由這個式子有可以得到:$\Sigma w_v - r * \Sigma w_e>0$,即存在左邊的式子結果>0即可,若要找存在一個數大於0,那麽肯定找這個數可能的最大值,那顯然把邊權重新賦值為$w_{vi}-r*w_{e}$,然後這式子跟SPFA找正環有什麽關系?可以發現$\Sigma w_v - r * \Sigma w_e$這個式子代表了這個環上用$w_{vi}-r*w_{ei}$作為新邊權的所有邊權之和,如果這個和大於0,那顯然這個環上存在正環,即有不存在最長路,那每一次SPFA找最長路看看是否存在即可,當然也把式子加個負號,然後用負環檢測也可以做

代碼:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <numeric>
#include <cstring>
#include <bitset>
#include <string>
#include <deque>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 1010;
const int M = 5010;
const double eps = 1e-6;
struct edge
{
    int to, nxt;
    double w;
    edge() {}
    edge(int _to, int _nxt, double _w): to(_to), nxt(_nxt), w(_w) {}
};
edge E[M];
int head[N], tot;
int vis[N], cnt[N];
double d[N];
int arr[N];

void init()
{
    CLR(head, -1);
    tot = 0;
}
inline void add(int s, int t, double w)
{
    E[tot] = edge(t, head[s], w);
    head[s] = tot++;
}
int spfa(int n, double g)
{
    queue<int>Q;
    for (int i = 1; i <= n; ++i)
    {
        d[i] = 0;
        vis[i] = 1;
        cnt[i] = 1;
        Q.push(i);
    }
    while (!Q.empty())//找正環
    {
        int u = Q.front();
        Q.pop();
        vis[u] = 0;
        for (int i = head[u]; ~i; i = E[i].nxt)
        {
            int v = E[i].to;
            double w = arr[u] - g * E[i].w;
            if (d[v] < d[u] + w)
            {
                d[v] = d[u] + w;
                if (!vis[v])
                {
                    vis[v] = 1;
                    Q.push(v);
                    if (++cnt[v] > n)
                        return 1;
                }
            }
        }
    }
    return 0;
}
int main(void)
{
    int n, m, a, b, i;
    double w;
    while (~scanf("%d%d", &n, &m))
    {
        init();
        for (i = 1; i <= n; ++i)
            scanf("%d", &arr[i]);
        for (i = 1; i <= m; ++i)
        {
            scanf("%d%d%lf", &a, &b, &w);
            add(a, b, w);
        }
        double L = 0, R = 1000;
        double ans = 0;
        while (fabs(R - L) >= eps)
        {
            double mid = (L + R) / 2.0;
            if (spfa(n, mid))
            {
                L = mid;
                ans = mid;
            }
            else
                R = mid;
        }
        printf("%.2f\n", ans);
    }
    return 0;
}

POJ 3621 Sightseeing Cows(最優比例環+SPFA檢測)