poj2728 Desert King【最优比率生成树】【Prim】【0/1分数规划】

本文介绍了一种使用分数规划和Prim算法解决特定问题的方法。通过二分查找和修改边权,实现了最小化成本与距离比的目标,适用于解决具有特定结构的网络优化问题。
含【最小生成树Prim】模板。
Prim复杂度为$O(n^2),适用于稠密图,特别是完全图的最小生成树的求解。
 
Desert King
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions:31622 Accepted: 8670

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

Source

 

题意:

有$n$个城市,每个城市有对应的坐标和海拔。两个城市之间的距离是坐标的距离,路的花费是海拔之差。

现在要建$n-1$条路,使得花费之和比距离之和最小。

思路:

一道0/1分数规划的题目。

0/1分数规划模型指:给定整数$a_1,a_2,...,a_n$,以及$b_1,b_2,...,b_n$,求一组解$x_i(1\leq i \leq n,x_i=0或1)$,

使得$\frac{\sum_{i=1}^{n} a_i*x_i}{\sum_{i=1}^{n}b_i*x_i}$最大化。

这种类型的题目我们可以通过二分答案来做。

因为如果对于$mid$,存在一组解,使得$\sum_{i=1}^{n}(a_i - mid * b_i) * x_i \geq 0$那么我们可以变形得到

存在一组解,使得$\frac{\sum_{i=1}^{n} a_i*x_i}{\sum_{i=1}^{n}b_i*x_i} \geq mid$

也就是说,$mid$比我们实际的答案要小。

反之,如果对于任意一组解,都有$\sum_{i=1}^{n}(a_i - mid * b_i) * x_i <0$那么我们可以得到

任意的解都有$\frac{\sum_{i=1}^{n} a_i*x_i}{\sum_{i=1}^{n}b_i*x_i} < mid$

也就是说,$mid$比我们实际的答案要大。

所以我们每次只需要计算$\sum_{i=1}^{n}(a_i - mid * b_i)*x_i$的最大值,如果最大值非负,令$st = mid$,否则$ed = mid$

 

对于这道题,也是类似。我们二分最终的答案。

每一次重新建图,城市和城市之间的边变为$cost - mid * length$

然后在新图上跑最小生成树。

如果最小生成树的值非负,说明实际答案比$mid$要大,令$st = mid$

由于这道题是完全图,而点的个数只有$1000$所以用prim比较好

 

WA了好久,后来发现是对d赋初值的问题。

对于double的数组赋初值$+\infty$不能用$0x3f$而应该用$0x7f$,就改了这里就过了。

  1 #include<iostream>
  2 //#include<bits/stdc++.h>
  3 #include<cstdio>
  4 #include<cmath>
  5 //#include<cstdlib>
  6 #include<cstring>
  7 #include<algorithm>
  8 //#include<queue>
  9 #include<vector>
 10 //#include<set>
 11 //#include<climits>
 12 //#include<map>
 13 using namespace std;
 14 typedef long long LL;
 15 #define N 100010
 16 #define pi 3.1415926535
 17 #define inf 0x3f3f3f3f
 18 
 19 const int maxn = 1005;
 20 const double eps = 1e-7;
 21 int n;
 22 struct city{
 23     double x, y, height;
 24 }c[maxn];
 25 double dist[maxn][maxn], cost[maxn][maxn], g[maxn][maxn];
 26 
 27 double get_dist(int i, int j)
 28 {
 29     return sqrt((c[i].x - c[j].x) * (c[i].x - c[j].x) + (c[i].y - c[j].y) * (c[i].y - c[j].y));
 30 }
 31 
 32 
 33 double d[maxn];
 34 bool vis[maxn];
 35 void prim()
 36 {
 37     memset(d, 0x7f, sizeof(d));
 38     memset(vis, 0, sizeof(vis));
 39     /*for(int i = 1; i <= n; i++){
 40         d[i] = (double)inf;
 41         vis[i] = 0;
 42     }*/
 43     d[1] = 0;
 44     for(int i = 1; i <= n; i++){
 45         int x = 0;
 46         for(int j = 1; j <= n; j++){
 47             if(!vis[j] && (x == 0 || d[j] < d[x]))x = j;
 48         }
 49         vis[x] = 1;
 50         for(int y = 1; y <= n; y++){
 51             if(!vis[y])d[y] = min(d[y], g[x][y]);
 52         }
 53     }
 54 }
 55 
 56 void getgraph(double mid)
 57 {
 58     for(int i = 1; i <= n; i++){
 59         for(int j = i; j <= n; j++){
 60             g[i][j] = g[j][i] = cost[i][j] - mid * dist[i][j];
 61         }
 62     }
 63 }
 64 
 65 bool check(double mid)
 66 {
 67     getgraph(mid);
 68     prim();
 69     double res = 0;
 70     for(int i = 1; i <= n; i++){
 71         res += d[i];
 72     }
 73     return res >= 0;
 74 }
 75 
 76 
 77 int main()
 78 {
 79     while(scanf("%d", &n) != EOF && n){
 80         for(int i = 1; i <= n; i++){
 81             scanf("%lf%lf%lf", &c[i].x, &c[i].y, &c[i].height);
 82         }
 83 
 84         for(int i = 1; i <= n; i++){
 85             for(int j = i; j <= n; j++){
 86                 dist[i][j] = dist[j][i] = get_dist(i, j);
 87                 cost[i][j] = cost[j][i] = fabs(c[i].height - c[j].height);
 88             }
 89         }
 90         double st = 0.0, ed = 100.0;
 91         while(ed - st >= eps){
 92             double mid = (st + ed) / 2;
 93             if(check(mid))st = mid;
 94             else ed = mid;
 95         }
 96         printf("%.3f\n", ed);
 97     }
 98 
 99     return 0;
100 }

 

转载于:https://www.cnblogs.com/wyboooo/p/9988567.html

1)普通用户端(全平台) 音乐播放核心体验: 个性化首页:基于 “听歌历史 + 收藏偏好” 展示 “推荐歌单(每日 30 首)、新歌速递、相似曲风推荐”,支持按 “场景(通勤 / 学习 / 运动)” 切换推荐维度。 播放页功能:支持 “无损音质切换、倍速播放(0.5x-2.0x)、定时关闭、歌词逐句滚动”,提供 “沉浸式全屏模式”(隐藏冗余控件,突出歌词与专辑封面)。 多端同步:自动同步 “播放进度、收藏列表、歌单” 至所有登录设备(如手机暂停后,电脑端打开可继续播放)。 音乐发现与管理: 智能搜索:支持 “歌曲名 / 歌手 / 歌词片段” 搜索,提供 “模糊匹配(如输入‘晴天’联想‘周杰伦 - 晴天’)、热门搜索词推荐”,结果按 “热度 / 匹配度” 排序。 歌单管理:创建 “公开 / 私有 / 加密” 歌单,支持 “批量添加歌曲、拖拽排序、一键分享到社交平台”,系统自动生成 “歌单封面(基于歌曲风格配色)”。 音乐分类浏览:按 “曲风(流行 / 摇滚 / 古典)、语言(国语 / 英语 / 日语)、年代(80 后经典 / 2023 新歌)” 分层浏览,每个分类页展示 “TOP50 榜单”。 社交互动功能: 动态广场:查看 “关注的用户 / 音乐人发布的动态(如‘分享新歌感受’)、好友正在听的歌曲”,支持 “点赞 / 评论 / 转发”,可直接点击动态中的歌曲播放。 听歌排行:个人页展示 “本周听歌 TOP10、累计听歌时长”,平台定期生成 “全球 / 好友榜”(如 “好友中你本周听歌时长排名第 3”)。 音乐圈:加入 “特定曲风圈子(如‘古典音乐爱好者’)”,参与 “话题讨论(如‘你心中最经典的钢琴曲’)、线上歌单共创”。 (2)音乐人端(创作者中心) 作品管理: 音乐上传:支持 “无损音频(FLAC/WAV)+ 歌词文件(LRC)+ 专辑封面” 上传,填写 “歌曲信息
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值