Desert King(01分数规划问题)(最优斜率生成树)
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions:33847 | Accepted: 9208 |
Description
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
Output
Sample Input
4
0 0 0
0 1 1
1 1 2
1 0 3
0
Sample Output
1.000
题意
有带权图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) )
C++代码
二分法
/*
*@Author: Agnel-Cynthia
*@Language: C++
*/
//#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=+;
const int M=+;
const int MOD=1e9+;
const double PI = acos(-1.0);
const double EXP = 1E-;
const int INF = 0x3f3f3f3f;
//int t,n,m,k,p,l,r,u,v;
const int maxn = ;
//ll a[maxn],b[maxn]; struct node
{
int x , y ,z ;
}edge[maxn]; int n ; double mp[maxn][maxn]; double dis(double x1 ,double y1,double x2,double y2){
return sqrt(1.0*(x1-x2) * (x1 - x2) + 1.0 * (y1 - y2) * (y1 - y2));
} void creat(){
for(int i = ;i <= n ;i ++){
for(int j = ;j <= n ; j++){
mp[i][j] = dis(edge[i].x,edge[i].y,edge[j].x,edge[j].y);
}
}
} double d[maxn];
bool vis[maxn]; double prime(double mid){
memset(vis,,sizeof vis);
for(int i = ;i <= n ; i++){
d[i] = abs(edge[].z - edge[i].z) - mp[][i] * mid;
}
vis[] = true;
double ans = ;
for(int i = ;i < n ; i++){
int v = -;double MIN = INF;
for(int j = ;j <= n ; j++){
if(MIN >= d[j] && !vis[j]){
v = j;
MIN = d[j];
}
}
if(v == -)
break;
vis[v] = true;
ans += MIN;
for(int j = ;j <= n ; j++){
if(!vis[j] && (fabs(edge[v].z - edge[j].z) - mp[v][j] * mid) < d[j])
d[j] = (fabs(edge[v].z - edge[j].z) - mp[v][j] * mid);
}
}
return ans ;
} int main()
{
#ifdef DEBUG
freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
#endif
// ios::sync_with_stdio(false);
// cin.tie(0);
// cout.tie(0);
while(cin >> n && n){
for(int i = ;i <= n ; i++){
cin >> edge[i].x >> edge[i].y >> edge[i].z;
}
double l = , r = 40.0;
double mid = ;
creat();
while(fabs(r - l) > EXP){
mid = (l + r) / ;
if(prime(mid) >= )
l = mid;
else
r = mid;
}
printf("%.3lf\n",mid );
}
#ifdef DEBUG
printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
//cout << "Hello world!" << endl;
return ;
}
Dinkelbach
/*
*@Author: Agnel-Cynthia
*@Language: C++
*/
//#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=+;
const int M=+;
const int MOD=1e9+;
const double PI = acos(-1.0);
const double EXP = 1E-;
const int INF = 0x3f3f3f3f;
//int t,n,m,k,p,l,r,u,v;
//ll a[maxn],b[maxn]; #define Rep(i,l,r) for(i=(l);i<=(r);i++)
#define rep(i,l,r) for(i=(l);i< (r);i++)
#define Rev(i,r,l) for(i=(r);i>=(l);i--)
#define rev(i,r,l) for(i=(r);i> (l);i--)
#define Each(i,v) for(i=v.begin();i!=v.end();i++)
#define r(x) read(x) int CH , NEG ;
template <typename TP>inline void read(TP& ret) {
ret = NEG = ; while (CH=getchar() , CH<'!') ;
if (CH == '-') NEG = true , CH = getchar() ;
while (ret = ret*+CH-'' , CH=getchar() , CH>'!') ;
if (NEG) ret = -ret ;
}
#define maxn 1010LL
#define infi 100000000LL
#define eps 1E-8F
#define sqr(x) ((x)*(x)) template <typename TP>inline bool MA(TP&a,const TP&b) { return a < b ? a = b, true : false; }
template <typename TP>inline bool MI(TP&a,const TP&b) { return a > b ? a = b, true : false; } int n;
int x[maxn], y[maxn], h[maxn];
double v[maxn][maxn], c[maxn][maxn]; bool vis[maxn];
double w[maxn];
double rv[maxn];///
inline double prim(double M) {
int i, j, k;
double minf, minw;
double sumc = , sumv = ;///
memset(vis,,sizeof vis);
Rep (i,,n) w[i] = v[][i]-M*c[][i],
rv[i] = v[][i];///
vis[] = true, minf = ;
rep (i,,n) {
minw = infi;
Rep (j,,n) if (!vis[j] && w[j]<minw)
minw = w[j], k = j;
sumv += rv[k], sumc += rv[k]-w[k];///
minf += minw, vis[k] = true;
Rep (j,,n) if (!vis[j])
if (MI(w[j],v[k][j]-M*c[k][j]))
rv[j] = v[k][j];///
}
return sumv*M/sumc;///
return minf;
} int main() {
int i, j;
double L, M, R;
double maxv, maxc, minv, minc;
while (scanf("%d", &n)!=EOF && n) {
Rep (i,,n)
scanf("%d%d%d", &x[i], &y[i], &h[i]);
maxv = maxc = -infi, minv = minc = infi;
rep (i,,n) Rep (j,i+,n) {
c[i][j] = c[j][i] = sqrt(sqr((double)x[i]-x[j])+sqr((double)y[i]-y[j]));
v[i][j] = v[j][i] = abs((double)h[i]-h[j]);
MA(maxv,v[i][j]), MI(minv,v[i][j]);
MA(maxc,c[i][j]), MI(minc,c[i][j]);
}
L = minv/maxc, R = maxv/minc;
while (true) {///
R = prim(L);///
if (fabs(L-R) < eps) break;///
L = R;///
}///
/*while (R-L > 1E-6) { // L:minf>0 R:minf<=0
M = (L+R)/2.0;
if (prim(M) > eps) L = M;
else R = M;
}*/
printf("%.3f\n", R);
}
//END: getchar(), getchar();
return ;
}
Desert King(01分数规划问题)(最优斜率生成树)的更多相关文章
- POJ 2728 Desert King 01分数规划,最优比率生成树
一个完全图,每两个点之间的cost是海拔差距的绝对值,长度是平面欧式距离, 让你找到一棵生成树,使得树边的的cost的和/距离的和,比例最小 然后就是最优比例生成树,也就是01规划裸题 看这一发:ht ...
- POJ 2728 Desert King ★(01分数规划介绍 && 应用の最优比率生成树)
[题意]每条路径有一个 cost 和 dist,求图中 sigma(cost) / sigma(dist) 最小的生成树. 标准的最优比率生成树,楼教主当年开场随手1YES然后把别人带错方向的题Orz ...
- POJ 2728 Desert King (01分数规划)
Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissions:29775 Accepted: 8192 Descr ...
- poj2728 Desert King——01分数规划
题目:http://poj.org/problem?id=2728 第一道01分数规划题!(其实也蛮简单的) 这题也可以用迭代做(但是不会),这里用了二分: 由于比较裸,不作过多说明了. 代码如下: ...
- 【POJ2728】Desert King - 01分数规划
Description David the Great has just become the king of a desert country. To win the respect of his ...
- poj2728 Desert King --- 01分数规划 二分水果。。
这题数据量较大.普通的求MST是会超时的. d[i]=cost[i]-ans*dis[0][i] 据此二分. 但此题用Dinkelbach迭代更好 #include<cstdio> #in ...
- POJ 2728 Desert King | 01分数规划
题目: http://poj.org/problem?id=2728 题解: 二分比率,然后每条边边权变成w-mid*dis,用prim跑最小生成树就行 #include<cstdio> ...
- 【POJ2728】Desert King(分数规划)
[POJ2728]Desert King(分数规划) 题面 vjudge 翻译: 有\(n\)个点,每个点有一个坐标和高度 两点之间的费用是高度之差的绝对值 两点之间的距离就是欧几里得距离 求一棵生成 ...
- POJ 3621 Sightseeing Cows 01分数规划,最优比例环的问题
http://www.cnblogs.com/wally/p/3228171.html 题解请戳上面 然后对于01规划的总结 1:对于一个表,求最优比例 这种就是每个点位有benefit和cost,这 ...
- 【转】[Algorithm]01分数规划
因为搜索关于CFRound277.5E题的题解时发现了这篇文章,很多地方都有值得借鉴的东西,因此转了过来 原文:http://www.cnblogs.com/perseawe/archive/2012 ...
随机推荐
- Mybatis mysql 一个搜索框多个字段模糊查询 几种方法
第一种 or 根据搜索框给定的关键词,模糊搜索用户名和账号都匹配的用户集合 <select id="list" parameterType="com.user.Us ...
- 【canvas学习笔记六】状态保存和变换
save()和restore() save() 保存当前状态,将当前canvas的状态存入栈中. restore() 恢复之前save的一个状态,将之前的状态从栈中弹出. 保存的当前状态包含以下信息: ...
- Spring Boot教程(十六)属性配置文件详解(1)
相信很多人选择Spring Boot主要是考虑到它既能兼顾Spring的强大功能,还能实现快速开发的便捷.我们在Spring Boot使用过程中,最直观的感受就是没有了原来自己整合Spring应用时繁 ...
- 组件内导航之beforeRouteUpdate的使用
使用场景: 组件复用:路由跳转: beforeRouteUpdate (to, from, next) { // 在当前路由改变,但是该组件被复用时调用 // 举例来说,对于一个带有动态参数的路径 / ...
- Long类源码浅析
1.Long类和Integer相类似,都是基本类型的包装类,类中的方法大部分都是类似的: 关于Integer类的浅析可以参看:Integer类源码浅析 2.这里主要介绍一下LongCache类,该缓存 ...
- 20165218 《网络对抗技术》Exp7 网络欺诈防范
Exp7 网络欺诈防范 基础问题回答 通常在什么场景下容易受到DNS spoof攻击 公共网络下或者同一局域网内: 在日常生活工作中如何防范以上两攻击方法 钓鱼网站 查验可信第三方 核对网站域名 查询 ...
- C# Socket-TCP异步编程原理详解附源码
目录 目录异步原理主要方法源码Server源码:Client源码实验效果(广播为例)参考博客 TOC 异步原理 套接字编程原理:延续文件作用思想,打开-读写-关闭的模式. C/S编程模式如下: Ø 服 ...
- mysql中查看ef或efcore生成的sql语句
http://www.solves.com.cn/it/sjk/MYSQL/2019-07-01/1336.html 涉及命令 1.开启general log模式 MySQL>set globa ...
- MVC Areas的使用
在网上查了一些资料 关于这个写的都很简单,没得实际应用. 参考了一下别人的代码,写篇博文记录一下. 首先目录结构: 然后主要是 BaseAreaRegistration 文件内容 public cla ...
- MySql 使用递归函数时遇到的级联删除问题
以下两段SQL的写法看似相同,结果效果却是不同的 写法A: DELETE OM_ORGANIZATION, OM_POSITION FROM OM_ORGANIZATION LEFT JOIN OM_ ...