(最优比率环) poj3621

Sightseeing Cows

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8197   Accepted: 2750

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 valuesFi (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

Source

USACO 2007 December Gold

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#include<map>
#define INF 100000000
using namespace std;
vector<int> e[1010],c[1010];
const double eps=1e-5;
int l,p;
int in[1010];
double dist[1010];
int w[1010];
bool vis[1010];
bool spfa(double x)
{
    queue<int> q;
    for(int i=1;i<=l;i++)
        dist[i]=INF,vis[i]=0,in[i]=0;
    dist[1]=0;
    vis[1]=1;
    q.push(1);
    in[1]=1;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=0;
        for(int i=0;i<e[u].size();i++)
        {
            int v=e[u][i];
            double y=x*c[u][i]-w[v];
            if(dist[v]>dist[u]+y)
            {
                dist[v]=dist[u]+y;
                if(!vis[v])
                {
                    vis[v]=1;
                    q.push(v);
                    in[v]++;
                    if(in[v]>l)
                        return true;
                }
            }

        }
    }
    return false;
}
int main()
{
    int x,y,z;
    scanf("%d%d",&l,&p);
    for(int i=1;i<=l;i++)
        scanf("%d",&w[i]);
    for(int i=1;i<=p;i++)
    {
        scanf("%d%d%d",&x,&y,&z);
        e[x].push_back(y);
        c[x].push_back(z);
    }
    double l,r,mid,ans=0;
    l=0,r=1e4;
    while(r-l>=eps)
    {
        mid=(l+r)*0.5;
        if(spfa(mid))
        {
            ans=mid;
            l=mid;
        }
        else
            r=mid;
    }
    printf("%.2lf\n",ans);
    return 0;
}

  

时间: 2024-10-16 03:42:37

(最优比率环) poj3621的相关文章

【poj3621】最优比率环

题意: 给定n个点,每个点有一个开心度F[i],每个点有m条单向边,每条边有一个长度d,要求一个环,使得它的 开心度的和/长度和 这个比值最大.n<=1000,m<=5000 题解: 最优比率环,很像以前做过的一题最优比率生成树.首先二分一个答案r=sigma(xi*fi)/sigma(xi*di),设z=r*sigma(xi*di)-sigma(xi*fi),若能找到一个环满足z<=0,则代表sigma(xi*fi)>=r*sigma(xi*di),则r可以比当前的r要大,则l=

[转]01分数规划算法 ACM 二分 Dinkelbach 最优比率生成树 最优比率环

01分数规划 前置技能 二分思想最短路算法一些数学脑细胞?问题模型1 基本01分数规划问题 给定nn个二元组(valuei,costi)(valuei,costi),valueivaluei是选择此二元组获得的价值(非负),costicosti是选择此二元组付出的代价(非负),设xi(xi∈{0,1})xi(xi∈{0,1})代表第ii个二元组的选与不选,最大(小)化下式 maximize(or minimize)   r=∑valuei?xi∑costi?ximaximize(or minim

POJ3621 Sightseeing Cows 最优比率环 二分法

题目链接:http://poj.org/problem?id=3621 Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10552   Accepted: 3613 Description Farmer John has decided to reward his cows for their hard work by taking them on a tour of the big ci

【bzoj1690/Usaco2007 Dec】奶牛的旅行——分数规划 最优比率环

Description 作为对奶牛们辛勤工作的回报,Farmer John决定带她们去附近的大城市玩一天.旅行的前夜,奶牛们在兴奋地讨论如何最好地享受这难得的闲暇. 很幸运地,奶牛们找到了一张详细的城市地图,上面标注了城市中所有L(2 <= L <= 1000)座标志性建筑物(建筑物按1..L顺次编号),以及连接这些建筑物的P(2 <= P <= 5000)条道路.按照计划,那天早上Farmer John会开车将奶牛们送到某个她们指定的建筑物旁边,等奶牛们完成她们的整个旅行并回到出

POJ 3621 Sightseeing Cows [最优比率环]

感觉去年9月的自己好$naive$ http://www.cnblogs.com/candy99/p/5868948.html 现在不也是嘛 裸题,具体看学习笔记 二分答案之后判负环就行了 $dfs$版超快 #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <queue> using namespace std; typed

POJ2728最优比率生成树解题报告

题目描述: David the Great has just become the king of a desert country. To win the respect of his people, he decided to build channels all over his country to bring water to every village. Villages which are connected to his capital village will be water

(最优比例环)POJ 3621 - Sightseeing Cows

题意: 在一个有向加权图中找到一个环,使这个环点权和/边权和 最大 分析: 一开始还没做过最优比率生成树,但是看到过,两题都A不了. 后来看了题解,索性一起撸掉. 这题可以用类似最优比率生成树的方法做,二分答案. 具体: 这个题解讲的很详细了. 代码: 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <vector> 5 #include <queue

最优比率生成树

最优比率生成树题意与最小生成树基本相同,但由单一边权的最小值转化为第一边权的总和与第二边权的总和比值的最小值,这导致算法发生巨大变化,以致于需要采用二分的方法,并进行一系列复杂的判定……(好吧,是我看来)    对于给定的有向图,要求求出一颗子树G,使其各边收益总和与花费的总和比值尽可能小,即Σ(benifit[i])/Σ(cost[i]) i∈G,我们可以二分答案λ的上下界[0,∞)(事实上上界取2^就好了),当λ为最优解时f(λ)=Σ(benifit[i])-λ*Σ(cost[i])=Σ(d

POJ 3621 Sightseeing Cows(最优比例环+SPFA检测)

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