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

[POJ 2728]Desert King(0-1分数规划/最优比率生成树)

时间:2017-06-06 10:55:59      阅读:313      评论:0      收藏:0      [点我收藏+]

标签:country   not   应该   class   out   com   channel   des   har   

Description

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 watered. As the dominate ruler and the symbol of wisdom in the country, he needs to build the channels in a most elegant way. 

After days of study, he finally figured his plan out. He wanted the average cost of each mile of the channels to be minimized. In other words, the ratio of the overall cost of the channels to the total length must be minimized. He just needs to build the necessary channels to bring water to all the villages, which means there will be only one way to connect each village to the capital. 

His engineers surveyed the country and recorded the position and altitude of each village. All the channels must go straight between two villages and be built horizontally. Since every two villages are at different altitudes, they concluded that each channel between two villages needed a vertical water lifter, which can lift water up or let water flow down. The length of the channel is the horizontal distance between the two villages. The cost of the channel is the height of the lifter. You should notice that each village is at a different altitude, and different channels can‘t share a lifter. Channels can intersect safely and no three villages are on the same line. 

As King David‘s prime scientist and programmer, you are asked to find out the best solution to build the channels.

Solution

最优比率生成树,其实知道0-1分数规划应该就很好做了

稠密图的话用prim会比较好

学过kruskal就没再写过prim的我去重学了一遍╮(╯_╰)╭

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#define MAXN 1005
#define eps 1e-10
using namespace std;
int n,x[MAXN],y[MAXN],h[MAXN];
double dis[MAXN],map[MAXN][MAXN],f[MAXN][MAXN],cost[MAXN][MAXN];
bool vis[MAXN];
bool check(double mid)
{
    memset(vis,0,sizeof(vis));
    memset(dis,127,sizeof(dis));
    dis[1]=0;double res=0;
    for(int i=1;i<=n;i++)
    {
        double mincost=1e20;int k=-1;
        for(int j=1;j<=n;j++)
        if(!vis[j]&&mincost>dis[j])mincost=dis[j],k=j;
        vis[k]=1,res+=mincost;
        for(int j=1;j<=n;j++)
        if(!vis[j]&&dis[j]>cost[k][j]-mid*map[k][j])dis[j]=cost[k][j]-mid*map[k][j];
    }
    if(res>0)return false;
    return true;
}
int main()
{
    while(~scanf("%d",&n)&&n)
    {
        for(int i=1;i<=n;i++)
        scanf("%d%d%d",&x[i],&y[i],&h[i]);
        for(int i=1;i<=n;i++)
        for(int j=1;j<=i;j++)
        {
            map[i][j]=map[j][i]=(double)sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
            cost[i][j]=cost[j][i]=abs(h[i]-h[j]);
        }
        double l=0,r=100,mid;
        while(r-l>eps)
        {
            mid=(l+r)/2;
            if(check(mid))r=mid;
            else l=mid;
        }
        printf("%.3lf\n",mid);
    }
    return 0;
}

 

[POJ 2728]Desert King(0-1分数规划/最优比率生成树)

标签:country   not   应该   class   out   com   channel   des   har   

原文地址:http://www.cnblogs.com/Zars19/p/6949561.html

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