POJ2728 Desert King
一道生成树+\(0/1\)分数规划
原题链接
设每条边的距离为\(dis[x]\),两点高度差为\(h[x]\),该图的生成树为\(T\),则题目实际求的就是\(\dfrac{\sum\limits_{x\in T}h[x]}{\sum\limits_{x\in T}dis[x]}\)的最小值。
这就是经典的\(0/1\)分数规划问题。
这里我用的是二分法。二分答案,记为\(mid\)。将图上的边权全部改为\(h[x]-mid\times dis[x]\),然后在上面跑最小生成树并计算边权和,如果非负说明\(mid\)过小,否则过大。
另外,虽然理论二分上界为\(10^7\),不过数据水,上界开四五十基本就能过了。
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int N = 1010;
struct dd {
int x, y, z;
};
dd a[N];
int n;
double D[N][N], dis[N];
bool v[N];
int re()
{
int x = 0;
char c = getchar();
bool p = 0;
for (; c<'0' || c>'9'; c = getchar())
p |= c == '-';
for (; c >= '0'&&c <= '9'; c = getchar())
x = x * 10 + (c - '0');
return p ? -x : x;
}
inline double minn(double x, double y)
{
return x < y ? x : y;
}
inline int jd(int x)
{
return x < 0 ? -x : x;
}
inline double co(int x, int y, double mid)
{
return -mid * D[x][y] + jd(a[x].z - a[y].z);
}
double judge(double mid)
{
double s = 0;
int i, j, x;
memset(v, 0, sizeof(v));
memset(dis, 66, sizeof(dis));
dis[1] = 0;
for (i = 1; i <= n; i++)
{
x = 0;
for (j = 1; j <= n; j++)
if (!v[j] && (dis[j] < dis[x] || !x))
x = j;
if (!x)
break;
v[x] = 1;
s += dis[x];
for (j = 1; j <= n; j++)
if (!v[j])
dis[j] = minn(dis[j], co(x, j, mid));
}
return s;
}
int main()
{
int i, j;
double l, r, mid;
while (1)
{
n = re();
if (!n)
return 0;
for (i = 1; i <= n; i++)
{
a[i].x = re();
a[i].y = re();
a[i].z = re();
}
for (i = 1; i < n; i++)
for (j = i + 1; j <= n; j++)
D[i][j] = D[j][i] = sqrt(1.0*(a[i].x - a[j].x)*(a[i].x - a[j].x) + 1.0*(a[i].y - a[j].y)*(a[i].y - a[j].y));
l = 0;
r = 1e7;
while (l + 1e-4 < r)
{
mid = (l + r) / 2;
if (judge(mid) >= 0)
l = mid;
else
r = mid;
}
printf("%.3f\n", l);
}
return 0;
}
POJ2728 Desert King的更多相关文章
- POJ2728 Desert King 【最优比率生成树】
POJ2728 Desert King Description David the Great has just become the king of a desert country. To win ...
- poj2728 Desert King【最优比率生成树】【Prim】【0/1分数规划】
含[最小生成树Prim]模板. Prim复杂度为$O(n^2),适用于稠密图,特别是完全图的最小生成树的求解. Desert King Time Limit: 3000MS Memory Li ...
- POJ2728 Desert King —— 最优比率生成树 二分法
题目链接:http://poj.org/problem?id=2728 Desert King Time Limit: 3000MS Memory Limit: 65536K Total Subm ...
- [POJ2728] Desert King 解题报告(最优比率生成树)
题目描述: David the Great has just become the king of a desert country. To win the respect of his people ...
- poj2728 Desert King --- 01分数规划 二分水果。。
这题数据量较大.普通的求MST是会超时的. d[i]=cost[i]-ans*dis[0][i] 据此二分. 但此题用Dinkelbach迭代更好 #include<cstdio> #in ...
- POJ2728 Desert King 最优比率生成树
题目 http://poj.org/problem?id=2728 关键词:0/1分数规划,参数搜索,二分法,dinkelbach 参考资料:http://hi.baidu.com/zzningxp/ ...
- 【最优比率生成树】poj2728 Desert King
最优比率生成树教程见http://blog.csdn.net/sdj222555/article/details/7490797 个人觉得很明白易懂,但他写的代码略囧. 模板题,但是必须Prim,不能 ...
- poj2728 Desert King——01分数规划
题目:http://poj.org/problem?id=2728 第一道01分数规划题!(其实也蛮简单的) 这题也可以用迭代做(但是不会),这里用了二分: 由于比较裸,不作过多说明了. 代码如下: ...
- poj2728 Desert King(最小生成树+01分数规划=最优比率生成树)
题意 n个点完全图,每个边有两个权值,求分数规划要求的东西的最小值. (n<=1000) 题解 心态炸了. 堆优化primT了. 普通的就过了. 我再也不写prim了!!!! 咳咳 最优比率生成 ...
随机推荐
- tensorflow serving 之minist_saved_model.py解读
最近在学习tensorflow serving,但是就这样平淡看代码可能觉得不能真正思考,就想着写个文章看看,自己写给自己的,就像自己对着镜子演讲一样,写个文章也像自己给自己讲课,这样思考的比较深,学 ...
- EUREKA原理总结
Eureka高可用架构 https://github.com/Netflix/eureka/wiki/Eureka-at-a-glance 上图中主要的名称说明: Register:EurekaCli ...
- led 的 platform驱动实现
http://www.linuxidc.com/Linux/2014-03/97756.htm http://blog.sina.com.cn/s/blog_6b94d5680101cn0t.html ...
- 联想电脑 Wifi开关开不了
"VirtualBox Host-Only Network" 没有有效的IP配置 未修复 自己电脑显示 控制面板>网络和Internet>网络连接 VirtualBo ...
- 数据持久化PlayerPrefs
1.Unity3D中的数据持久化是以键值对的形式存储的,可以看作是一个字典 2.Unity3D中的值是通过键名来读取的,当值不存在时,返回默认值 3.在Unity中只支持int.float.strin ...
- 【C++】预处理过程与语句总结
转载请保留: http://www.cnscn.org(CNS电脑与英语学习网) Author: cnscn http://www.cnscn.org 1)预处理 根据已放置在文件中的预处理指令来修改 ...
- UltraEdit窗口布局重新设置
解决办法:工具栏中的视图-->环境-->左边小框里选择“编程员”,再点选择环境 转载:https://blog.csdn.net/u011650048/article/details/18 ...
- char、varchar、nchar、nvarchar的区别(转)
转自:http://tech.diannaodian.com/dw/data/sql/2011/1005/135556.html 对于程序中的string型字段,SQLServer中有char.var ...
- javascript 页面导出功能
javascript 页面导出功能 <a class="btn" href="javascript:void(0);" onclick="win ...
- computed
Vue.js在模板表达式中限制了,绑定表达式最多只能有一条表达式,但某些数据需要一条以上的表达式运算实现,此时就可以将此数据放在计算属性(computed)当中. Vuejs中关于computed ...