• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            poj2728

            Desert King

            Time Limit: 3000MS Memory Limit: 65536K
            Total Submissions: 15105 Accepted: 4251

            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.

            Input

            There are several test cases. Each test case starts with a line containing a number N (2 <= N <= 1000), which is the number of villages. Each of the following N lines contains three integers, x, y and z (0 <= x, y < 10000, 0 <= z < 10000000). (x, y) is the position of the village and z is the altitude. The first village is the capital. A test case with N = 0 ends the input, and should not be processed.

            Output

            For each test case, output one line containing a decimal number, which is the minimum ratio of overall cost of the channels to the total length. This number should be rounded three digits after the decimal point.

            Sample Input

            4
            0 0 0
            0 1 1
            1 1 2
            1 0 3
            0
            

            Sample Output

            1.000

            題意:有n個村莊,村莊在不同坐標和海拔,現在要對所有村莊供水,只要兩個村莊之間有一條路即可,

                     建造水管距離為坐標之間的歐幾里德距離(好象是叫歐幾里德距離吧),費用為海拔之差

                     現在要求方案使得費用與距離的比值最小

            很顯然,這個題目是要求一棵最優比率生成樹,

            以前沒寫過這種題目

            怎么做呢

            0-1分數規劃,0-1分數規劃是分數規劃的一種特殊情況,分數規劃適用于求解最優化問題的,對于求最大的對應解,該理論也有效

            這是從網上找到的具體的最優比率生成樹的方法的講解

            ////////////////////

            概念

            有帶權圖G, 對于圖中每條邊e[i], 都有benifit[i](收入)和cost[i](花費), 我們要求的是一棵生成樹T, 它使得 ∑(benifit[i]) / ∑(cost[i]), i∈T 最大(或最小).

            這顯然是一個具有現實意義的問題.

             

            解法之一 0-1分數規劃

            設x[i]等于1或0, 表示邊e[i]是否屬于生成樹.

            則我們所求的比率 r = ∑(benifit[i] * x[i]) / ∑(cost[i] * x[i]), 0≤i<m .

            為了使 r 最大, 設計一個子問題---> 讓 z = ∑(benifit[i] * x[i]) - l * ∑(cost[i] * x[i]) = ∑(d[i] * x[i]) 最大 (d[i] = benifit[i] - l * cost[i]) , 并記為z(l). 我們可以興高采烈地把z(l)看做以d為邊權的最大生成樹的總權值.

             


            然后明確兩個性質:

             1.  z單調遞減

              證明: 因為cost為正數, 所以z隨l的減小而增大.

             2.  z( max(r) ) = 0

              證明: 若z( max(r) ) < 0, ∑(benifit[i] * x[i]) - max(r) * ∑(cost[i] * x[i]) < 0, 可化為 max(r) < max(r). 矛盾;

                      若z( max(r) ) >= 0, 根據性質1, 當z = 0 時r最大.

            到了這個地步, 七竅全已打通, 喜歡二分的上二分, 喜歡Dinkelbach的就Dinkelbach.

             

            復雜度

            時間 O( O(MST) * log max(r) )

            空間 O( O(MST) )


            /////////////////////////////

            關于分數規劃的學習我找到了一篇論文,里面有講分數規劃,特別詳細

            算法合集之《最小割模型在信息學競賽中的應用》

            黑書上說求最小生成樹有O(n)的方法,沒去找

            代碼很糾結

            迭代+prim

             1#include<stdio.h>
             2#include<string.h>
             3#include<math.h>
             4#define inf 0x7ffffff
             5#define eps 0.0001
             6#define MAX 1100
             7int n;
             8int x[MAX],y[MAX],z[MAX];
             9double dist[MAX][MAX],cost[MAX][MAX],dis[MAX];
            10short vis[MAX];
            11double b,a;
            12double prim(double p)
            13{
            14    int i,j,k,pre[MAX];
            15    double mincost,totcost,totdist,v;
            16    for(i=1; i<=n; i++)
            17    {
            18        pre[i]=1;
            19    }

            20    memset(vis,0,sizeof(vis));
            21    vis[1]=1;
            22    totcost=0;
            23    totdist=0;
            24    dis[1]=0;
            25    for(j=2; j<=n; j++)
            26    {
            27        dis[j]=cost[j][1]-p*dist[j][1];
            28    }

            29    for(i=2; i<=n; i++)
            30    {
            31        mincost=inf;
            32        for(j=2; j<=n; j++)
            33            if((!vis[j])&&(mincost>dis[j]))
            34            {
            35                mincost=dis[j];
            36                k=j;
            37            }

            38
            39        vis[k]=1;
            40        totcost+=cost[pre[k]][k];
            41        totdist+=dist[pre[k]][k];
            42        for(j=1; j<=n; j++)
            43        {
            44            if(!vis[j])
            45            {
            46                v=cost[k][j]-p*dist[k][j];
            47                if (v<dis[j])
            48                {
            49                    dis[j]=v;
            50                    pre[j]=k;
            51                }

            52            }

            53        }

            54    }

            55    return totcost/totdist;
            56}

            57int main()
            58{
            59    int i,j;
            60    while(scanf("%d",&n)!=EOF&&n!=0)
            61    {
            62        for(i=1; i<=n; i++)
            63            scanf("%d%d%d",&x[i],&y[i],&z[i]);
            64        for (i=1; i<=n; i++ )
            65        {
            66            for(j=i+1; j<=n; j++)
            67            {
            68                b=(x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]);
            69                dist[i][j]=sqrt(b);
            70                dist[j][i]=dist[i][j];
            71                cost[i][j]=z[i]-z[j];
            72                if (cost[i][j]<0)    cost[i][j]=-cost[i][j];
            73                cost[j][i]=cost[i][j];
            74            }

            75        }

            76        a=0;
            77        while(1)
            78        {
            79            b=prim(a);
            80            if(fabs(b-a)<eps)
            81                break;
            82            a=b;
            83        }

            84        printf("%.3lf\n",b);
            85    }

            86    return 0;
            87}

            88
            89
            90/*
            91    最有比率生成樹
            92    01分數規劃
            93    分數規劃的特例
            94*/

            95


            posted on 2012-03-12 23:47 jh818012 閱讀(956) 評論(0)  編輯 收藏 引用

            <2025年7月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            導航

            統計

            常用鏈接

            留言簿

            文章檔案(85)

            搜索

            最新評論

            • 1.?re: poj1426
            • 我嚓,,輝哥,,居然搜到你的題解了
            • --season
            • 2.?re: poj3083
            • @王私江
              (8+i)&3 相當于是 取余3的意思 因為 3 的 二進制是 000011 和(8+i)
            • --游客
            • 3.?re: poj3414[未登錄]
            • @王私江
              0ms
            • --jh818012
            • 4.?re: poj3414
            • 200+行,跑了多少ms呢?我的130+行哦,你菜啦,哈哈。
            • --王私江
            • 5.?re: poj1426
            • 評論內容較長,點擊標題查看
            • --王私江
            国产伊人久久| 99久久国语露脸精品国产| 狠狠色婷婷久久一区二区三区| 亚洲AV无码久久精品狠狠爱浪潮| 久久综合国产乱子伦精品免费| 久久99国产精品久久久| 久久精品亚洲精品国产欧美| 国产亚洲色婷婷久久99精品| 一本一道久久精品综合| 久久国产美女免费观看精品| 青青久久精品国产免费看| 无码人妻久久一区二区三区免费丨 | 品成人欧美大片久久国产欧美| 久久久精品人妻无码专区不卡| 伊人久久久AV老熟妇色| 久久青青草原国产精品免费| 中文字幕乱码久久午夜| 久久久久无码专区亚洲av| 三上悠亚久久精品| 日本精品久久久久影院日本| 国产精品女同久久久久电影院 | 精品综合久久久久久97| 91精品国产高清久久久久久91| 日韩久久久久久中文人妻| 一本大道久久东京热无码AV| 国产毛片久久久久久国产毛片| 99精品国产在热久久无毒不卡| 老男人久久青草av高清| 伊人色综合九久久天天蜜桃| 精品免费久久久久国产一区| 91久久国产视频| 久久国产乱子精品免费女| 国产国产成人精品久久| 久久亚洲精品国产精品| 久久久久久久人妻无码中文字幕爆 | 国产99久久久国产精品~~牛| 热久久这里只有精品| 亚洲国产精品久久久久网站 | 久久毛片免费看一区二区三区| 亚洲午夜久久久久久噜噜噜| 久久综合给合综合久久|