uvalive 5731 Qin Shi Huang’s National Road System
题意:
秦始皇要修路使得所有的城市连起来,并且花费最少;有一个人,叫徐福,他可以修一条魔法路,不花费任何的钱与劳动力。
秦始皇想让修路的费用最少,但是徐福想要受益的人最多,所以他们经过协商,决定让 A / B 最大,A代表被魔法路连接的两个城市的人口总数,B代表修的路中非魔法路的总长度。
输出 A / B 的最大值。
思路:
A / B 最大,则A尽可能大,B尽可能小,所以首先把MST求出来。由于每个城市的人口有很大的偏差,所以必须枚举每一条边,计算连接的两个城市的人口,复杂度为O(n^2),所以每次替换边的复杂度必须是O(1)。
由于是稠密图,所以用prim算法,prim算法在O(n^2)的复杂度的时候,可以维护最小生成树上两点之间的最长边,这样就可以在过程中把两点间的最长边保存下来。这个是依靠已知的前驱节点实现的。复杂度为O(n^2)。
枚举每一条边时,如果这条边是MST中的边,那么就直接计算 A / B;如果这条边不在MST中,加入这条边就会成环,这时我们保存的信息就起作用了,成环之后把在MST中的连接这两点的最长边去掉,就是新的生成树的权值,且保证了B尽可能小。替换的时间复杂度为O(1)。
代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <math.h>
using namespace std;
const int maxn = ;
double path[maxn][maxn];
double g[maxn][maxn];
double dis[maxn];
bool vis[maxn];
bool used[maxn][maxn];
int peo[maxn];
int pre[maxn];
double ans;
struct point
{
int x,y;
}p[maxn]; double cal(int i,int j)
{
double x2 = (p[i].x - p[j].x) * (p[i].x - p[j].x);
double y2 = (p[i].y - p[j].y) * (p[i].y - p[j].y); return sqrt(x2 + y2);
} int prim(int n)
{
memset(vis,,sizeof(vis));
memset(path,,sizeof(path));
memset(used,,sizeof(used));
for (int i = ;i <= n;i++) dis[i] = 1e15; vis[] = ;
dis[] = ; int tot = ;
ans = ;
//double len = 0; for (int i = ;i <= n;i++)
{
pre[i] = ;
dis[i] = g[][i];
} for (int i = ;i < n;i++)
{
int u;
double d = 1e15; for (int j = ;j <= n;j++)
{
if (!vis[j] && dis[j] < d)
{
d = dis[j];
u = j;
}
} vis[u] = ; ans += d; //tot = max(peo[u] + peo[pre[u]],tot); used[u][pre[u]] = used[pre[u]][u] = ; for (int j = ;j <= n;j++)
{
if (vis[j] && j != u)
path[u][j] = path[j][u] = max(d,path[j][pre[u]]);
} for (int j = ;j <= n;j++)
{
if (!vis[j])
{
if (g[u][j] < dis[j])
{
dis[j] = g[u][j];
pre[j] = u;
}
}
}
} //printf("%.2f **\n",ans); return tot;
} int main()
{
int t; scanf("%d",&t); while (t--)
{
int n; scanf("%d",&n); for (int i = ;i <= n;i++)
{
scanf("%d%d%d",&p[i].x,&p[i].y,&peo[i]);
} for (int i = ;i <= n;i++)
{
for (int j = ;j <= n;j++)
{
g[i][j] = 1e15;
}
} for (int i = ;i <= n;i++)
{
for (int j = i+;j <= n;j++)
{
g[i][j] = g[j][i] = cal(i,j);
}
} prim(n); double ans1 = ; for (int i = ;i <= n;i++)
{
for (int j = i + ;j <= n;j++)
{
if (used[i][j])
{
int peop = peo[i] + peo[j];
ans1 = max(peop / (ans - g[i][j]),ans1); //printf("%d %d %d %.2f **\n",i,j,peop,ans - g[i][j]);
}
else
{
int peop = peo[i] + peo[j];
ans1 = max(peop / (ans - path[i][j]),ans1);
//printf("%d %d %d %.2f **\n",i,j,peop,ans - path[i][j]);
}
}
} printf("%.2f\n",ans1); //printf("%.2f",path[1][4]);
} return ;
}
uvalive 5731 Qin Shi Huang’s National Road System的更多相关文章
- UValive 5713 Qin Shi Huang's National Road System
Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/3 ...
- UVALive 5713 Qin Shi Huang's National Road System秦始皇修路(MST,最小瓶颈路)
题意: 秦始皇要在n个城市之间修路,而徐福声可以用法术位秦始皇免费修1条路,每个城市还有人口数,现要求徐福声所修之路的两城市的人口数之和A尽量大,而使n个城市互通需要修的路长B尽量短,从而使得A/B最 ...
- UVALive 5713 Qin Shi Huang's National Road System(次小生成树)
题意:对于已知的网络构建道路,使城市两两之间能够互相到达.其中一条道路是可以免费修建的,问需要修建的总长度B与免费修建的道路所连接的两城市的人口之和A的比值A/B最大是多少. 因为是求A/B的最大值, ...
- hdu 4081 Qin Shi Huang's National Road System (次小生成树)
Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/3 ...
- hdu 4081 Qin Shi Huang's National Road System (次小生成树的变形)
题目:Qin Shi Huang's National Road System Qin Shi Huang's National Road System Time Limit: 2000/1000 M ...
- HDU 4081 Qin Shi Huang's National Road System 次小生成树变种
Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/3 ...
- Qin Shi Huang's National Road System HDU - 4081(树形dp+最小生成树)
Qin Shi Huang's National Road System HDU - 4081 感觉这道题和hdu4756很像... 求最小生成树里面删去一边E1 再加一边E2 求该边两顶点权值和除以 ...
- [hdu P4081] Qin Shi Huang’s National Road System
[hdu P4081] Qin Shi Huang’s National Road System Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- HDU4081 Qin Shi Huang's National Road System 2017-05-10 23:16 41人阅读 评论(0) 收藏
Qin Shi Huang's National Road System ...
随机推荐
- linux常用查看文件或日志命令
常见查看文件内容命令汇总如下: cat filename 查看日志,会打开整个文件,直接跑到最后面 tac filename 查看日志,会打开整 ...
- <<Sklearn 与 TensorFlow 机器学习实用指南>>
地址 https://github.com/apachecn/hands-on-ml-zh 目录结构 零.前言 第一部分 机器学习基础 一.机器学习概览 二.一个完整的机器学习项目 三.分类 四.训练 ...
- Es6 的类(class)
首先根据es5的类(原型对象)的基本点做参照. 序号 基本点 es5 >es6 1 实例属性(方法) √ √ 2 原型属性(方法) 或 公共属性(方法) √ √ 3 es5的私有变量 或 私有属 ...
- js禁用页面上右键菜单、选中和复制
有时候我们不想页面上的内容被人复制走,那么就可以使用js对页面进行设置,禁止右键菜单.禁止选中.禁止复制等功能可以有效的达到这个效果,js代码如下所示: /** * 禁用右键菜单 */ documen ...
- jquery的widget源代码剖析
dialog_obj(别名): Widget_obj(别名): 调用widget方法 $.widget("ui.dialog",dialog_obj); // jquery.ui. ...
- UIDatePicker封装
#import <UIKit/UIKit.h> #import <objc/runtime.h> @protocol datePickerViewDelegate <NS ...
- SQLite数据库管理工具(SQLiteStudio)v3.1.1
http://www.pc6.com/softview/SoftView_86552.html
- 有关java反射机制 小结
111111111111 Class<?> cls = Class.forName("包名.类名"); Object o = cls.newInstance(); 22 ...
- [geos]Geometry基本的几何对象
读取shp中的点,读取shp中的线, (1)读取shp中的多边形,修改属性字段的值. 类库版本:geos3.6.2,shapelib1.3 定义类变量: GeometryFactory::unique ...
- Python几种数据结构内置方法的时间复杂度
参考:https://blog.csdn.net/baoli1008/article/details/48059623 注:下文中,’n’代表容器中元素的数量,’k’代表参数的值,或者参数的数量. 1 ...