青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

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個村莊,村莊在不同坐標和海拔,現(xiàn)在要對所有村莊供水,只要兩個村莊之間有一條路即可,

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

         現(xiàn)在要求方案使得費用與距離的比值最小

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

以前沒寫過這種題目

怎么做呢

0-1分數(shù)規(guī)劃,0-1分數(shù)規(guī)劃是分數(shù)規(guī)劃的一種特殊情況,分數(shù)規(guī)劃適用于求解最優(yōu)化問題的,對于求最大的對應解,該理論也有效

這是從網(wǎng)上找到的具體的最優(yōu)比率生成樹的方法的講解

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

概念

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

這顯然是一個具有現(xiàn)實意義的問題.

 

解法之一 0-1分數(shù)規(guī)劃

設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為正數(shù), 所以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, 根據(jù)性質1, 當z = 0 時r最大.

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

 

復雜度

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

空間 O( O(MST) )


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

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

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

黑書上說求最小生成樹有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分數(shù)規(guī)劃
93    分數(shù)規(guī)劃的特例
94*/

95


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


只有注冊用戶登錄后才能發(fā)表評論。
網(wǎng)站導航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


<2025年11月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

導航

統(tǒng)計

常用鏈接

留言簿

文章檔案(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
  • 評論內容較長,點擊標題查看
  • --王私江
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            久久久久久自在自线| 亚洲激情视频在线| 久久蜜臀精品av| 欧美一区日本一区韩国一区| 一区福利视频| 亚洲黑丝在线| 国产精品视频久久一区| 久久综合伊人| 欧美日韩中文字幕日韩欧美| 久久久99爱| 欧美日本一区二区视频在线观看| 午夜激情亚洲| 欧美成人dvd在线视频| 欧美在线视频播放| 久久中文字幕一区| 欧美日韩国产成人在线| 欧美日韩伦理在线免费| 亚洲一区二区视频在线| 亚洲欧美一区二区三区在线| 91久久黄色| 午夜精品久久| 亚洲美女中文字幕| 久久精品视频免费| 亚洲尤物在线| 欧美h视频在线| 久久久久久9| 欧美午夜在线一二页| 欧美chengren| 国产一区二区激情| 一区二区久久久久久| 亚洲国内自拍| 久久超碰97人人做人人爱| 亚洲综合不卡| 亚洲人午夜精品| 亚洲国产成人高清精品| 亚洲尤物影院| 一区二区三区高清视频在线观看| 久久―日本道色综合久久| 午夜精品www| 国产精品r级在线| 日韩视频一区二区在线观看 | 亚洲国产精品第一区二区三区| 亚洲视频axxx| 亚洲激情校园春色| 久久夜色精品国产亚洲aⅴ | 一区二区三区导航| 久久全国免费视频| 久久免费的精品国产v∧| 国产精品欧美风情| 亚洲午夜一区二区| 午夜激情一区| 亚洲大片一区二区三区| 亚洲黄色尤物视频| 欧美成人精品在线观看| 亚洲第一成人在线| 99一区二区| 欧美先锋影音| 午夜激情久久久| 久久久久高清| 伊人成综合网伊人222| 久久久久国内| 欧美激情无毛| 亚洲无吗在线| 国产伦精品一区二区三区视频孕妇 | 美腿丝袜亚洲色图| 黑丝一区二区三区| 久久久蜜桃一区二区人| 欧美激情精品久久久久久免费印度| 一区二区三区在线免费播放| 蜜桃久久精品乱码一区二区| 欧美激情视频一区二区三区在线播放 | 亚洲性图久久| 国产精品日韩在线播放| 亚洲欧美日韩国产一区二区| 久久精品国产一区二区三区| 一区二区在线不卡| 欧美人牲a欧美精品| 在线综合视频| 久久男人资源视频| 美女久久一区| 一区二区三区成人精品| 国产精品久久久久久久久久直播| 亚洲自拍偷拍网址| 久久综合久久久| 99视频一区| 国产精品丝袜久久久久久app| 欧美一区激情视频在线观看| 欧美激情一区二区三区全黄 | 亚洲国产精品嫩草影院| 久久艳片www.17c.com| 中文成人激情娱乐网| 性欧美xxxx大乳国产app| 欧美日本一道本在线视频| 国产精品久久久久久久浪潮网站 | 亚洲摸下面视频| 欧美激情综合| 亚洲精品自在久久| 久久深夜福利免费观看| 国产精品视频观看| 亚洲毛片播放| 亚洲福利国产精品| 欧美一区二区三区喷汁尤物| 亚洲欧美中文日韩v在线观看| 久久久久久久久久码影片| 久久色在线播放| 美女成人午夜| 亚洲午夜女主播在线直播| 欧美夫妇交换俱乐部在线观看| 一本久久知道综合久久| 亚洲第一级黄色片| 国产欧美一区二区精品仙草咪 | 宅男噜噜噜66一区二区| 影音先锋日韩精品| 国产欧美日韩在线观看| 欧美日韩国产影片| 老司机aⅴ在线精品导航| 亚洲专区在线| 亚洲图片欧洲图片日韩av| 欧美国产日韩a欧美在线观看| 久久精品日韩欧美| 欧美一区二区成人6969| 一区二区三区欧美视频| 亚洲三级电影全部在线观看高清| 国语自产在线不卡| 国产视频久久网| 国产精品伊人日日| 欧美日韩中文字幕在线| 欧美另类一区| 欧美日韩高清免费| 欧美激情一二三区| 久久伊人精品天天| 久久福利资源站| 欧美一区不卡| 久久―日本道色综合久久| 欧美日本在线视频| 亚洲欧美中文字幕| 国产午夜精品全部视频播放 | 欧美影院在线播放| 亚洲网站在线| 亚洲男女自偷自拍图片另类| 亚洲专区一二三| 亚洲字幕在线观看| 亚洲一级二级| 亚洲综合激情| 精品电影一区| 亚洲国产日韩在线| 亚洲国产91| 亚洲乱码国产乱码精品精天堂| 亚洲国产精品久久久久婷婷884| 影音先锋欧美精品| 亚洲人线精品午夜| 亚洲一区二区免费视频| 欧美一区激情视频在线观看| 久久久久九九九九| 欧美aa国产视频| 欧美好吊妞视频| 亚洲精品在线免费| 一区二区三区欧美在线| 中文日韩欧美| 香蕉久久国产| 美女脱光内衣内裤视频久久网站| 久久久久久久999| 欧美大片91| 亚洲日本一区二区| 99热免费精品在线观看| 亚洲欧美一区二区激情| 久久亚洲国产成人| 欧美日韩在线看| 国产一区二区三区在线播放免费观看| 国产综合色在线| 一本色道久久综合亚洲精品不卡| 亚洲欧美日韩视频一区| 久久久综合香蕉尹人综合网| 欧美电影免费| 国产精品视频免费观看| 一区二区三区在线视频播放| 日韩一级二级三级| 久久福利影视| 亚洲日本aⅴ片在线观看香蕉| 亚洲在线网站| 欧美www视频在线观看| 国产精品日产欧美久久久久| 亚洲国产综合在线| 午夜免费日韩视频| 亚洲第一天堂av| 欧美亚洲一区二区三区| 欧美激情第三页| 国产午夜精品美女视频明星a级| 亚洲激情综合| 校园激情久久| 欧美77777| 亚洲欧美怡红院| 欧美日韩aaaaa| 1000精品久久久久久久久| 亚洲欧美日韩精品久久奇米色影视| 久久永久免费| 亚洲欧美国产视频| 欧美日韩一区二区视频在线| 精品9999| 久久gogo国模裸体人体|