Desert King

poj2728:http://poj.org/problem?id=2728

题意:给你n的点,每一个点会有一个坐标(x,y),然后还有一个z值,现在上你求一棵生成树,是的这棵生成树的所有边的费用/所有边的距离最小,其中,边费用是指两点之z差值的绝对值,边距离是指两点之间的距离。

题解:这一题就是求最小比率生成树。采用的解法就是0-1分数规划。

其中设最后的比率是l

1,z(l)是单调递减的。

2,z(max(l))=0;这可以采用反证法进行证明。

3,因为是完全图,所以要采用prime求最小生成树。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 using namespace std;
 7 const int N=1003;
 8 const double inf=10000000000.0;
 9 double mp[N][N],cost[N][N];
10 double dist[N];
11 double xx[N],yy[N],zz[N];
12 int n;
13 bool vis[N];
14 bool solve(double x){
15     for(int i=1;i<=n;i++){
16         vis[i]=0;
17         dist[i]=cost[i][1]-mp[i][1]*x;
18     }
19     vis[1]=1;
20     dist[1]=0;
21     double cost2=0;
22     for(int i=1;i<n;i++){
23         double minn=inf;
24         int k=-1;
25         for(int j=1;j<=n;j++){
26             if(!vis[j]&&dist[j]<minn){
27                 minn=dist[j];
28                 k=j;
29             }
30         }
31         if(k!=-1){
32             cost2+=dist[k];
33             vis[k]=1;
34             for(int j=1;j<=n;j++){
35                 if(!vis[j]){
36                      double tt=cost[k][j]-mp[k][j]*x;
37                      if(dist[j]>tt){
38                         dist[j]=tt;
39                      }
40                 }
41             }
42         }
43     }
44   if(cost2>=0)return true;
45   return false;
46
47 }
48 int main(){
49     while(~scanf("%d",&n)&&n){
50        for(int i=1;i<=n;i++){
51          scanf("%lf%lf%lf",&xx[i],&yy[i],&zz[i]);
52        }
53        for(int i=1;i<=n;i++){
54          for(int j=i+1;j<=n;j++){
55             double temp=(xx[i]-xx[j])*(xx[i]-xx[j])+(yy[i]-yy[j])*(yy[i]-yy[j]);
56             mp[i][j]=mp[j][i]=sqrt(temp);
57             cost[i][j]=cost[j][i]=abs(zz[i]-zz[j]);
58          }
59        }
60       double l=0,r=100,ans=0;
61       while(abs(r-l)>1e-4){
62         double mid=(r+l)/2;
63         if(solve(mid)){
64             l=mid;
65             ans=mid;
66         }
67         else
68             r=mid;
69       }
70      printf("%.3f\n",ans);
71     }
72 }

时间: 2024-07-30 01:07:37

Desert King的相关文章

01分数规划+prim POJ2728 Desert King

Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 26009   Accepted: 7219 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 coun

POJ 2728 Desert King 最优比率生成树

Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 20978   Accepted: 5898 [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 co

ZOJ 2728 Desert King(最优比例生成树)

Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 20586   Accepted: 5777 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 coun

poj 2728 Desert King

Desert King http://poj.org/problem?id=2728 Time Limit: 3000MS   Memory Limit: 65536K       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 t

POJ 题目2728 Desert King(最优比率生成树)

Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 21923   Accepted: 6123 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 coun

poj2728 Desert King --- 01分数规划 二分水果。。

这题数据量较大,普通的求MST是会超时的. d[i]=cost[i]-ans*dis[0][i] 据此二分. 但此题用Dinkelbach迭代更好 #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> using namespace std; #define N 1010 double mp[N][N],c[N][N],x

Desert King (poj 2728 最优比率生成树 0-1分数规划)

Language: Default Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 22113   Accepted: 6187 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

【POJ 2728】Desert King

Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 21361   Accepted: 5974 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 coun

poj 2728 Desert King(最小比率生成树,迭代法)

引用别人的解释: 题意:有n个村庄,村庄在不同坐标和海拔,现在要对所有村庄供水,只要两个村庄之间有一条路即可, 建造水管距离为坐标之间的欧几里德距离(好象是叫欧几里德距离吧),费用为海拔之差 现在要求方案使得费用与距离的比值最小 很显然,这个题目是要求一棵最优比率生成树, 概念 有带权图G, 对于图中每条边e[i], 都有benifit[i](收入)和cost[i](花费), 我们要求的是一棵生成树T, 它使得 ∑(benifit[i]) / ∑(cost[i]), i∈T 最大(或最小). 这

poj 2728 Desert King(最优比率生成树,01分数规划)

http://poj.org/problem?id=2728 大致题意:有n个村庄,输入每个村庄的位置和高度,这n个村庄要连在一起,村与村之间的长度为他们之间的欧几里得距离,花费是两村之间的高度差,要求连在一起的花费和与距离和之比的最小值. 思路:明显的最优比率生成树.二分答案λ,每条边重新赋权c[i] - λd[i] ,因为要求比值最小,那么对于所有的生成树,它们的f[λ]必须>=0,所以只需求得基于最小生成树的f'[λ],当f'[λ] = 0时即找到了正解λ*. 二分: #include <