• <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 閱讀(954) 評論(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无码永不| 国产精品女同久久久久电影院| WWW婷婷AV久久久影片| 久久www免费人成看国产片| 欧美久久久久久| 成人国内精品久久久久影院| 国产视频久久| 久久婷婷五月综合色高清| 大美女久久久久久j久久| 99久久国产综合精品女同图片 | 九九久久自然熟的香蕉图片| 国产2021久久精品| 色88久久久久高潮综合影院| 久久无码一区二区三区少妇| 国内精品久久久久| 久久综合给久久狠狠97色| 精品久久久久久久久久中文字幕 | 国内精品久久久久久99蜜桃 | 久久国产色AV免费看| 亚洲国产成人乱码精品女人久久久不卡 | 久久午夜无码鲁丝片| 亚洲精品乱码久久久久久蜜桃| 国产精品久久久久无码av| 国产亚洲精品久久久久秋霞| 亚洲七七久久精品中文国产| 国内精品久久久久久久涩爱| 久久国产色AV免费看| 无码AV波多野结衣久久| 久久国产AVJUST麻豆| 久久久久成人精品无码 | 色天使久久综合网天天| 久久久久久av无码免费看大片 | 久久成人国产精品免费软件| 久久中文字幕精品| 久久久久亚洲精品日久生情 | 久久久久亚洲AV无码观看 | 激情伊人五月天久久综合| 久久婷婷五月综合97色| 激情伊人五月天久久综合|