Sightseeing Cows(POJ-3621)

Problem 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 L1i 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

The meaning of problems: given a directed graph, each edge and each point has its own weight, find a ring of n points m edges, and such that each bit value of each loop of the ratio of the maximum value Collage , find this ratio

Ideas: the optimal ratio of ring 01 issues planning score, re-using the SPFA sides of the negative determination in the ring assignment process to answer using dichotomy dichotomy

Specific ideas: Click here

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
LL quickPow(LL a,LL b){ LL res=1; while(b){if(b&1)res*=a; a*=a; b>>=1;} return res; }
LL multMod(LL a,LL b,LL mod){ a%=mod; b%=mod; LL res=0; while(b){if(b&1)res=(res+a)%mod; a=(a<<=1)%mod; b>>=1; } return res%mod;}
LL quickMultPowMod(LL a, LL b,LL mod){ LL res=1,k=a; while(b){if((b&1))res=multMod(res,k,mod)%mod; k=multMod(k,k,mod)%mod; b>>=1;} return res%mod;}
LL quickPowMod(LL a,LL b,LL mod){ LL res=1; while(b){if(b&1)res=(a*res)%mod; a=(a*a)%mod; b>>=1; } return res; }
LL getInv(LL a,LL mod){ return quickPowMod(a,mod-2,mod); }
LL GCD(LL x,LL y){ return !y?x:GCD(y,x%y); }
LL LCM(LL x,LL y){ return x/GCD(x,y)*y; }
const double EPS = 1E-6;
const int MOD = 1000000000+7;
const int N = 1000+5;
const int dx[] = {0,0,-1,1,1,-1,1,1};
const int dy[] = {1,-1,0,0,-1,1,-1,1};
using namespace std;

struct Edge {
    int to, next;
    double cost; //边权
} edge[N * 10];
int head[N], tot;
int n, m;
double value[N]; //点权
double dis[N];
bool vis[N];
int cnt[N]; //进队次数
void addEdge(int x, int y, double w) {
    edge[tot].to = y;
    edge[tot].next = head[x];
    edge[tot].cost = w;
    head[x] = tot++;
}
int SPFA(double x) {
    memset(dis, 0x43, sizeof(dis));
    memset(cnt, 0, sizeof(cnt));
    memset(vis, false, sizeof(vis));
    dis[1] = 0;
    cnt[1]++;

    queue<int> Q;
    Q.push(1);
    while (!Q.empty()) {
        int u = Q.front();
        Q.pop();
        vis[u] = 0;
        for (int i = head[u]; i != -1; i = edge[i].next) {
            int v = edge[i].to;
            double val = edge[i].cost * x - value[v]; //重赋值
            if (dis[v] > dis[u] + val) {
                dis[v] = dis[u] + val;
                if (!vis[v]) {
                    vis[v] = true;
                    cnt[v]++;
                    if (cnt[v] >= n)
                        return true;
                    Q.push(v);
                }
            }
        }
    }
    return false;
}
int main() {
    tot = 0;
    memset(head, -1, sizeof(head));

    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++) //点权
        scanf("%lf", &value[i]);
    for (int i = 1; i <= m; i++) {
        int x, y;
        double w;
        scanf("%d%d%lf", &x, &y, &w);
        addEdge(x, y, w);
    }

    double left = 0, right = 10000;
    while (right - left >= EPS) {
        double mid = (left + right) / 2.0;
        if (SPFA(mid)) //存在负环,更改下界
            left = mid;
        else
            right = mid;
    }
    printf("%.2f\n", left);

    return 0;
}

 

Released 1871 original articles · won praise 702 · Views 1.94 million +

Guess you like

Origin blog.csdn.net/u011815404/article/details/102672998