Qin Shi Huang's National Road System HDU - 4081(树形dp+最小生成树)
Qin Shi Huang's National Road System
感觉这道题和hdu4756很像...
求最小生成树里面删去一边E1 再加一边E2 求该边两顶点权值和除以(最小生成树-E1)的最大值
其中(最小生成树-E1)必须是最小的
先跑一遍prim 跑完之后在最小生成树里面dp
dp[i][j] = i到j的路径中最大的那条边 最小生成树减去dp[i][j]肯定会最小
代码如下
- #include <cstdio>
- #include <algorithm>
- #include <cstring>
- #include <cmath>
- using namespace std;
- const double inf = 0x3f3f3f3f3f;
- const int maxn = ;
- struct Point {
- double x, y;
- int n;
- } point[maxn];
- struct Edge {
- int to;
- int next;
- } edge[maxn<<];
- int n, cnt, head[maxn], pre[maxn];
- double dis[maxn][maxn], lowc[maxn], sum, dp[maxn][maxn];
- bool vis[maxn];
- inline void addedge(int u, int v) {
- edge[cnt].to = v;
- edge[cnt].next = head[u];
- head[u] = cnt++;
- }
- inline double Distance(const Point& lhs, const Point& rhs) {
- return sqrt((lhs.x - rhs.x) * (lhs.x - rhs.x) + (lhs.y - rhs.y) * (lhs.y - rhs.y));
- }
- void prim() {
- sum = 0.0;
- memset(vis, , sizeof(vis));
- memset(pre, , sizeof(pre));
- for (int i = ; i < n; i++) lowc[i] = dis[][i];
- vis[] = true;
- for (int i = ; i < n; i++) {
- double minc = inf;
- int p = -;
- for (int j = ; j < n; j++) {
- if (!vis[j] && minc > lowc[j]) {
- minc = lowc[j];
- p = j;
- }
- }
- sum += minc;
- vis[p] = true;
- addedge(p, pre[p]);
- addedge(pre[p], p);
- for (int j = ; j < n; j++) {
- if (!vis[j] && lowc[j] > dis[p][j]) {
- lowc[j] = dis[p][j];
- pre[j] = p;
- }
- }
- }
- }
- void dfs(int u, int root) {
- vis[u] = true;
- for (int i = head[u]; ~i; i = edge[i].next) {
- int v = edge[i].to;
- if (!vis[v]) {
- dp[root][v] = max(dp[root][u], dis[u][v]);
- dfs(v, root);
- }
- }
- }
- int main() {
- int T;
- scanf("%d", &T);
- while (T--) {
- scanf("%d", &n);
- for (int i = ; i < n; i++) {
- scanf("%lf%lf%d", &point[i].x, &point[i].y, &point[i].n);
- }
- for (int i = ; i < n; i++) {
- for (int j = i + ; j < n; j++) {
- dis[i][j] = dis[j][i] = Distance(point[i], point[j]);
- }
- }
- memset(head, -, sizeof(head));
- cnt = ;
- prim();
- for (int i = ; i < n; i++) {
- memset(vis, ,sizeof(vis));
- dfs(i, i);
- }
- double ans = ;
- for (int i = ; i < n; i++) {
- for (int j = i + ; j < n; j++) {
- double temp = sum - dp[i][j];
- temp = (point[i].n + point[j].n) / temp;
- ans = max(ans, temp);
- }
- }
- printf("%.2f\n", ans);
- }
- return ;
- }
Qin Shi Huang's National Road System HDU - 4081(树形dp+最小生成树)的更多相关文章
- LA 5713 - Qin Shi Huang's National Road System(HDU 4081) MST
LA:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...
- [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 ...
- HDU 4081 Qin Shi Huang's National Road System 最小生成树+倍增求LCA
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4081 Qin Shi Huang's National Road System Time Limit: ...
- 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 ...
- HDU 4081—— Qin Shi Huang's National Road System——————【次小生成树、prim】
Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/3 ...
- HDU4081: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
Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/3 ...
随机推荐
- 前端入门18-JavaScript进阶之作用域链
声明 本系列文章内容全部梳理自以下几个来源: <JavaScript权威指南> MDN web docs Github:smyhvae/web Github:goddyZhao/Trans ...
- vue2.x 下载后台传过来的流文件(excel)后乱码问题
1.接口返回的流和头部: 2.下载流文件的代码 方法一:是用了插件 https://github.com/kennethjiang/js-file-download 方法二:是用了 blob 不管哪种 ...
- Bootstrap 实战之响应式个人博客 (二)
阅读本博文前请参考:Bootstrap 实战之响应式个人博客 (一) 一.博客 1.结构 整体博客详情页的结构共包括四部分: 导航栏 博客主体内容 右侧栏:全局搜索框,广告位,推荐阅读 页尾 其中导航 ...
- android找不到aar包
转载请标明出处,维权必究:https://www.cnblogs.com/tangZH/p/9939663.html 在做项目的时候引入aar包,编译的时候却提示错误(这个错误大概说的是...... ...
- onScrollChanged()
转载请标明出处:http://www.cnblogs.com/tangZH/p/8428100.html onScrollChanged里面有四个参数 @Overrideprotected void ...
- Andriod Studio安装教程
最近开设安卓课程,无奈于开发团队不再更新eclipse上sdk兼容问题,在eclipse上浪费了两天时间,换了Andriod Studio, Andriod Studio下载网址:http://www ...
- ASP.NET中弹出消息框的几种常见方法
在ASP.NET网站开发中,经常需要使用到alert消息框,尤其是在提交网页的时候,往往需要在服务器端对数据进行检验,并给出提示或警告. 这里,仅介绍几种不同的实现方法. 1.众所周知的方法是采用如下 ...
- Linux LVM学习总结——Insufficient Free Extents for a Logical Volume
如下所示,在创建LV的时候,偶尔会遇到"Volume group "xxxx" has insufficient free space (xxxx extents): x ...
- 【原】Java学习笔记024 - 包装类
package cn.temptation; public class Sample01 { public static void main(String[] args) { // 之前对于基本数据类 ...
- debian 9 开机启动
由于某些软件并没有增加开启启动的服务,很多时候需要手工添加,一般我们都是推荐添加命令到 /etc/rc.local 文件,但是 Debian 9 默认不带 /etc/rc.local 文件,而 rc. ...