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;
    }
  • 相关阅读:
    页面进度条插件pace.js
    cropper.js头像剪裁
    数组每一项与之前项累加
    css实现波浪纹,水波纹动画
    在echart的map上把symbol替换成gif通过convertToPixel坐标转换成px定位到页面,没有做echart的缩放情况下
    vue折叠面板的收缩动画效果,实现点击按钮展开和收起,并有个高度过渡的动画效果
    vue项目中应用自定义的字体
    vue中监听滚动条事件,通过事件控制其他div滚动,固定表头和列
    好久没写博客了
    在nuxt中使用富文本编辑器quill
  • 原文地址:https://www.cnblogs.com/Zars19/p/6949561.html
Copyright © 2011-2022 走看看