zoukankan      html  css  js  c++  java
  • [POJ 2728]Desert King(0-1分数规划/最优比率生成树)

    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
    ",mid);
        }
        return 0;
    }
  • 相关阅读:
    那一定都是你的错!- 一次FastDFS并发问题的排查经历
    程序员和烟民
    Spring Boot(十六):使用 Jenkins 部署 Spring Boot
    阿里Dubbo疯狂更新,关Spring Cloud什么事?
    从架构演进的角度聊聊Spring Cloud都做了些什么?
    FastDFS 集群 安装 配置
    中小型互联网公司微服务实践-经验和教训
    jvm系列(十一):JVM演讲PPT分享
    jvm系列(十):如何优化Java GC「译」
    Spring Boot (十五): Spring Boot + Jpa + Thymeleaf 增删改查示例
  • 原文地址:https://www.cnblogs.com/Zars19/p/6949561.html
Copyright © 2011-2022 走看看