码迷,mamicode.com
首页 > 其他好文 > 详细

01分数规划POJ3621(最优比例生成环)

时间:2015-04-30 16:06:09      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

Sightseeing Cows
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8218   Accepted: 2756

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
题意:给出n个城市和m条道路,道路是有向边,然后给出经过n个城市的每个城市可以获得的欢乐度,给出每条道路需要花费的旅行时间,要求某个人从任一点出发,然后走一圈(至少经过两个点)回到最初的起点,问平均单位时间的欢乐值最高是多少,若不能走一圈,则输出0.
分析:设单位时间欢乐值r=sigma(Vi)/sigma(Ej);其中Vi属于该环的城市的欢乐值,Ej是该环的道路的所花费的时间,设最大平均值是R,即r<=R
即:sigma(Vi)/sigma(Ej)<=R,即:sigma(Vi)-sigma(Ej)*R<=0;
也就是说对于函数H(r)=sigma(Vi)-sigma(Ej)*r,当H(r)==0的时候可以取得最优值
所以接下来就是二分枚举r值,以P[u]-r*edge[i].w作为边权,用dfs判断正环,如果存在正环,则二分增加r,否则即不存在环,应该缩小r,二分逐渐逼近找到一个环且该环的权值和==0,此时枚举的r就是R
程序:
#include"stdio.h"
#include"string.h"
#include"math.h"
#define M 2009
#define eps 1e-10
struct node
{
    int u,v,w,next;
}edge[M*20];
int t,head[M],use[M],p[M];
double dis[M];
void init()
{
    t=0;
    memset(head,-1,sizeof(head));
}
void add(int u,int v,int w)
{
    edge[t].u=u;
    edge[t].v=v;
    edge[t].w=w;
    edge[t].next=head[u];
    head[u]=t++;
}
int dfs(int u,double r)
{
    use[u]=1;
    for(int i=head[u];~i;i=edge[i].next)
    {
        int v=edge[i].v;
        if(dis[v]<dis[u]+p[u]-r*edge[i].w)
        {
            dis[v]=dis[u]+p[u]-r*edge[i].w;
            if(use[v])
                return 1;
            if(dfs(v,r))
                return 1;
        }
    }
    use[u]=0;
    return 0;
}
int solve(int n,double r)
{
    memset(use,0,sizeof(use));
    memset(dis,0,sizeof(dis));
    for(int i=1;i<=n;i++)
    {
        if(dfs(i,r))
            return 1;
    }
    return 0;
}
int main()
{
    int n,m,a,b,c;
    while(scanf("%d%d",&n,&m)!=-1)
    {
        init();
        for(int i=1;i<=n;i++)
            scanf("%d",&p[i]);
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            add(a,b,c);
        }
        double l=0,r=10000000;
        double mid;
        double ans=0;
        while(r-l>eps)
        {
            mid=(l+r)/2;
            int msg=solve(n,mid);
            if(msg)
            {
                ans=mid;
                l=mid;
            }
            else
                r=mid;
        }
        printf("%.2lf\n",ans);
    }
    return 0;
}

  

01分数规划POJ3621(最优比例生成环)

标签:

原文地址:http://www.cnblogs.com/mypsq/p/4468968.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!