【poj2728】Desert King
【poj2728】Desert King
题意
最优比率生成树。
http://blog.csdn.net/ophunter_lcm/article/details/10113817
分析
Dinkelbach算法,通过迭代的思想进行参数搜索。
每一次的参数搜索,求出来的最小生成树又会生成一个更优的参数,然后用新的参数继续搜...以此类推。
为什么生成的参数会更优?
这是因为我们要最小化\(\sum C\over \sum D\),我们设答案为\({\sum C\over \sum D}<L\),这等价于\(\sum(C-DL)<0\)
我们满足以上等式,就是说明存在\(C,D\)满足\(\sum(C-DL)<0\),所以逆推回来就有\(L>{\sum C\over \sum D}\),且\(\sum C\over \sum D\)是可取的,所以就进一步的逼近了。
可以证明逼近最优解的次数为\(O(\log NC)\),即是一个\(\log\)级别的次数。
所以可以大胆地去用。
它跑得一般比二分还快。
但是适用的范围没有二分广。
总之具体问题具体分析。
为什么我的姿势这么挫,跑得这么慢...
#include <cstdio>
#include <cstring>
#include <cctype>
#include <climits>
#include <cmath>
#include <algorithm>
using namespace std;
#define rep(i,a,b) for (int i=(a);i<=(b);i++)
const int N=1024;
const int INF=INT_MAX;
const double MAX=1e10;
const double EPS=1e-4;
int n;
double x[N],y[N],z[N];
double d[N][N];
double h[N][N];
double ans;
double mp[N][N];
int vis[N]; double dis[N]; int src[N];
double sumD,sumH;
inline int cmp(double a,double b)
{
if (fabs(a-b)<EPS) return 0;
return a<b?-1:1;
}
inline double GetDis(double xi,double yi,double xj,double yj)
{
double dx=xi-xj,dy=yi-yj;
return sqrt(pow(dx,2)+pow(dy,2));
}
double Prim(void)
{
double sum=0;
memset(mp,0,sizeof mp);
memset(vis,0,sizeof vis); memset(dis,0,sizeof dis); memset(src,0,sizeof src);
sumD=sumH=0;
rep(i,1,n) rep(j,1,n)
mp[i][j]=h[i][j]-ans*d[i][j];
vis[1]=1; dis[1]=0; rep(i,2,n) dis[i]=mp[1][i],src[i]=1;
rep(tms,1,n-1)
{
double t=MAX; int frm=0,to=0;
rep(i,1,n) if (!vis[i])
if (dis[i]<t) t=dis[i],frm=src[i],to=i;
vis[to]=1;
sumD+=d[frm][to],sumH+=h[frm][to];
sum+=t;
rep(i,1,n) if (!vis[i])
if (mp[to][i]<dis[i]) dis[i]=mp[to][i],src[i]=to;
}
return sum;
}
int main(void)
{
#ifndef ONLINE_JUDGE
freopen("poj2728.in","r",stdin);
freopen("poj2728.out","w",stdout);
#endif
rep(tms,1,INF)
{
scanf("%d",&n); if (!n) break;
memset(x,0,sizeof x); memset(y,0,sizeof y); memset(z,0,sizeof z);
rep(i,1,n) scanf("%lf%lf%lf",x+i,y+i,z+i);
memset(d,0,sizeof d); memset(h,0,sizeof h);
rep(i,1,n) rep(j,1,n)
{
d[i][j]=GetDis(x[i],y[i],x[j],y[j]);
h[i][j]=fabs(z[i]-z[j]);
}
ans=MAX;
while (1)
{
double t=Prim();
if (cmp(t,0)!=0)
ans=sumH/sumD;
else break;
}
printf("%0.3lf\n",ans);
}
return 0;
}
【poj2728】Desert King的更多相关文章
- 【POJ2728】Desert King(分数规划)
[POJ2728]Desert King(分数规划) 题面 vjudge 翻译: 有\(n\)个点,每个点有一个坐标和高度 两点之间的费用是高度之差的绝对值 两点之间的距离就是欧几里得距离 求一棵生成 ...
- 【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 最优比率生成树
题目大意:给定一个 N 个点的无向完全图,边有两个不同性质的边权,求该无向图的一棵最优比例生成树,使得性质为 A 的边权和比性质为 B 的边权和最小. 题解:要求的答案可以看成是 0-1 分数规划问题 ...
- 【POJ 2728 Desert King】
Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 27109Accepted: 7527 Description David the ...
- 【HDOJ】【1512】Monkey King
数据结构/可并堆 啊……换换脑子就看了看数据结构……看了一下左偏树和斜堆,鉴于左偏树不像斜堆可能退化就写了个左偏树. 左偏树介绍:http://www.cnblogs.com/crazyac/arti ...
- POJ2728:Desert King——题解
http://poj.org/problem?id=2728 题目大意:求一棵生成树使得路费用和/路长之和最小(路的费用是两端点的高度差) 最小比率生成树. 我们还是01分数规划的思想将边权变为路费用 ...
- poj2728 Desert King【最优比率生成树】【Prim】【0/1分数规划】
含[最小生成树Prim]模板. Prim复杂度为$O(n^2),适用于稠密图,特别是完全图的最小生成树的求解. Desert King Time Limit: 3000MS Memory Li ...
- POJ2728 Desert King 【最优比率生成树】
POJ2728 Desert King Description David the Great has just become the king of a desert country. To win ...
- 【BZOJ】1087: [SCOI2005]互不侵犯King
[算法]状态压缩型DP [题解]http://www.cnblogs.com/xtx1999/p/4620227.html (orz) https://www.cnblogs.com/zbtrs/p/ ...
随机推荐
- 安卓开发之json解析
1.从网页获取json返回字符串 public class ReadNet extends AsyncTask<URL, Integer, String> { @Override ...
- AutoLayout +Masonary
1, Masonry介绍与使用实践(快速上手Autolayout) http://adad184.com/2014/09/28/use-masonry-to-quick-solve-autolayou ...
- 字符串转Json对象
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Web.S ...
- 2.建立exception包,建立Bank类,类中有变量double balance表示存款,Bank类的构造方法能增加存款,Bank类中有取款的发方法withDrawal(double dAmount),当取款的数额大于存款时,抛出InsufficientFundsException,取款数额为负数,抛出NagativeFundsException,
public class Bank { Double qian=0.0; double newBank(double a) { qian=qian+a; return qian; } double w ...
- HTML在IE中的条件注释
HTML在IE中的条件注释 HTML的条件注释在IE5中被首次引入,直到IE9.一直都是简单地判定用户浏览器(IE,非IE,IE版本)的一种手段,而在IE10的标准模式下,条件注释功能被停止支持(兼容 ...
- Dungeon Master bfs
time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u POJ 2251 Descriptio ...
- [转]Unity3D协程介绍 以及 使用
作者ChevyRay ,2013年9月28日,snaker7译 原文地址:http://unitypatterns.com/introduction-to-coroutines/ 在Unity中,协 ...
- background:linear-gradient()
文章一 http://www.runoob.com/css3/css3-gradients.html 文章二:http://www.w3cplus.com/content/css3-gradien ...
- CUBRID学习笔记 31 通过select创建表
语法 CREATE {TABLE | CLASS} <table_name> [( <column_definition> [,<table_constraint> ...
- Python基础学习笔记(七)常用元组内置函数
参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-tuples.html 3. http://www.liaoxue ...