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 ...
随机推荐
- Android Studio 学习(七)通知
导入support- -v4 1.进入 file-project structure 2.左边选择app 3.右边选择dependencies 4.左下角可以看到一个加号,点击选择Library de ...
- JavaScript是如何工作的: Web推送通知的机制
摘要: 如何在Web端推送消息? 这是专门探索 JavaScript 及其所构建的组件的系列文章的第9篇. 如果你错过了前面的章节,可以在这里找到它们: JavaScript是如何工作的:引擎,运行时 ...
- Dynamics CRM教程:图表的Top设置及导出修改和导入
关注本人微信和易信公众号: 微软动态CRM专家罗勇,回复144或者20150412可方便获取本文,同时可以在第一时间得到我发布的最新的博文信息,follow me! 上一篇博客制作的图表放在Dashb ...
- android 开发之 ListView 与Adapter 应用实践
在开发android中,ListView 的应用显得非常频繁,只要需要显示列表展示的应用,可以说是必不可少,下面是记录开发中应用到ListView与Adapter 使用的实例: ListView 所在 ...
- 使用 Dashboard - 每天5分钟玩转 Docker 容器技术(174)
上一节我们完成了 Kubernetes Dashboard 的安装,本节就来实践一下. Dashboard 界面结构 Dashboard 的界面很简洁,分为三个大的区域. 顶部操作区在这里用户可以搜索 ...
- C#事件与委托详解【精华 多看看】
Delegate delegate是C#中的一种类型,它实际上是一个能够持有对某个方法的引用的类.与其它的类不同,delegate类能够拥有一个签名(signature),并且它"只能持有与 ...
- 20180903 - Python Pip 工具下载whl包与离线安装
20180903 - Python Pip 工具下载whl包与离线安装 1. 我的Blog 博客园 https://www.cnblogs.com/piggybaba 个人网站 http://pigg ...
- 【原】Java学习笔记008 - 方法(函数)
package cn.temptation; public class Sample01 { public static void main(String[] args) { // 方法/函数 Met ...
- SQLServer之创建DML AFTER INSERT触发器
DML AFTER INSERT触发器创建原理 触发器触发时,系统自动在内存中创建deleted表或inserted表,内存中创建的表只读,不允许修改,触发器执行完成后,自动删除. insert触发器 ...
- Business Intelligence Tools We Recommend 1/4 – Metabase
May 24, 2018 by Arturs Oganesyan-Peel BI is useful. It’s pretty. But it never really matters unless ...