HDU - 4081 Qin Shi Huang's National Road System 【次小生成树】
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=4081
题意
给出n个城市的坐标 以及 每个城市里面有多少人
秦始皇想造路 让每个城市都连通 (直接或者间接都可以)
然后 有一个特别厉害的大臣 可以造一条魔法路 不用耗费资金 但是要求 这条路链接的两座城市的人要尽量多
定义了一个 value = A/B
A = 魔法路链接的两座城市的总人数
B = 除了魔法路,其他路的总权值
求出最大的value
思路
首先我会想到最小生成树
然后我想让value 最大 我就想 能不能 去枚举每一条边 当做 魔法路
因为 求最小生成树 要保证 value 尽量小
然后枚举每一条边的时候
如果这条边已经在最小生成树里面了 那么此时的ans = (A[i] + A[j] / ans - G[i][j])
B = 答案 - 这条边
这个应该比较好理解
如果没有在最小生成树里面 ,那么此时的B = ans - Max[i][j]
为什么是这样呢。。
Max[i][j] 表示 从i - j 的最大边
因为 我们枚举的这条边 没有在 这条边 所以加入这条边之后 i - j 之间 就会形成一个环路
比如说 是这样的
自然就可以发现 我们在 2 - 4 之间已经在最小生成树里面的边 可以拆掉一条
那么拆哪一条呢,, 为了对答案有更大贡献,自然是边权最大的那条
AC代码
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <list>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>
#define CLR(a, b) memset(a, (b), sizeof(a))
#define pb push_back
#define bug puts("***bug***");
#define fi first
#define se second
#define stack_expand #pragma comment(linker, "/STACK:102400000,102400000")
#define syn_close ios::sync_with_stdio(false);cin.tie(0);
//#define bug
//#define gets gets_s
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <string, int> psi;
typedef pair <string, string> pss;
typedef pair <double, int> pdi;
const double PI = acos(-1.0);
const double E = exp(1.0);
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
const int maxn = 1e3 + 10;
const int MOD = 142857;
int n;
double G[maxn][maxn];
struct node
{
int x, y, p;
node() {}
node(int x, int y, int p) : x(x), y(y), p(p) {}
void read()
{
scanf("%d%d%d", &x, &y, &p);
}
}point[maxn];
double dis(int a, int b)
{
double d1 = (point[a].x - point[b].x) * (point[a].x - point[b].x) * 1.0;
double d2 = (point[a].y - point[b].y) * (point[a].y - point[b].y) * 1.0;
return sqrt(d1 + d2);
}
int used[maxn][maxn];
double Max[maxn][maxn];
double lowcost[maxn];
int pre[maxn];
int visit[maxn];
double ans;
int findMin()
{
double Maxc = INF * 1.0;
int flag = 0;
for (int i = 1; i <= n; i++)
{
if (visit[i] == 0 && lowcost[i] < Maxc)
{
Maxc = lowcost[i];
flag = i;
}
}
return flag;
}
void prime()
{
for (int i = 1; i <= n; i++)
{
visit[i] = 0;
lowcost[i] = INF * 1.0;
used[i][i] = 0;
Max[i][i] = -INF * 1.0;
for (int j = i + 1; j <= n; j++)
{
Max[i][j] = Max[j][i] = -INF * 1.0;
used[i][j] = used[j][i] = 0;
}
}
for (int i = 1; i <= n; i++)
{
lowcost[i] = Max[1][i] = G[1][i];
pre[i] = 1;
}
pre[1] = -1;
visit[1] = 1;
ans = 0.0;
for (int i = 2; i <= n; i++)
{
int k = findMin();
visit[k] = 1;
ans += lowcost[k];
used[pre[k]][k] = used[k][pre[k]] = 1;
for (int j = 1; j <= n; j++)
{
if (visit[j] == 1 && j != k)
Max[j][k] = Max[k][j] = max(Max[j][pre[k]], lowcost[k]);
if (visit[j] == 0 && lowcost[j] > G[k][j])
{
lowcost[j] = G[k][j];
pre[j] = k;
}
}
}
}
double smst()
{
double Maxc = 0.0;
for (int i = 1; i <= n; i++)
for (int j = i + 1; j <= n; j++)
if (used[i][j] == 0)
Maxc = max(Maxc, (point[i].p + point[j].p) * 1.0 / (ans - Max[i][j]));
else
Maxc = max(Maxc, (point[i].p + point[j].p) * 1.0 / (ans - G[i][j]));
return Maxc;
}
void clear()
{
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
G[i][j] = 0.0;
}
int main()
{
int t;
cin >> t;
while (t--)
{
scanf("%d", &n);
clear();
for (int i = 1; i <= n; i++)
point[i].read();
for (int i = 1; i <= n; i++)
for (int j = i + 1; j <= n; j++)
G[i][j] = G[j][i] = dis(i, j);
prime();
printf("%.2lf\n", smst());
}
}
/*
先跑最小生成树
然后去枚举每一条边 当做 magic road
如果该边本来就在最小生成树当中 那么我就删去这条边 维护答案
如果该边本来没有在最小生成树当中,那么我就删去i - j 中的最大边 维护答案
*/
HDU - 4081 Qin Shi Huang's National Road System 【次小生成树】的更多相关文章
- 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 [次小生成树]
题意: 秦始皇要建路,一共有n个城市,建n-1条路连接. 给了n个城市的坐标和每个城市的人数. 然后建n-2条正常路和n-1条魔法路,最后求A/B的最大值. A代表所建的魔法路的连接的城市的市民的人数 ...
- 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——————【次小生成树、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 —— 次小生成树变形
题目链接:https://vjudge.net/problem/HDU-4081 Qin Shi Huang's National Road System Time Limit: 2000/1000 ...
- hdu 4081 Qin Shi Huang's National Road System 树的基本性质 or 次小生成树思想 难度:1
During the Warring States Period of ancient China(476 BC to 221 BC), there were seven kingdoms in Ch ...
- hdu 4081 Qin Shi Huang's National Road System(次小生成树prim)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4081 题意:有n个城市,秦始皇要修用n-1条路把它们连起来,要求从任一点出发,都可以到达其它的任意点. ...
随机推荐
- springMVC+json构建restful风格的服务
首先.要知道什么是rest服务,什么是rest服务呢? REST(英文:Representational State Transfer,简称REST)描写叙述了一个架构样式的网络系统.比方 web 应 ...
- kvo&kvc
Key Value Coding Key Value Coding是cocoa的一个标准组成部分,它能让我们可以通过name(key)的方式访问property, 不必调用明确的property ac ...
- 转 拉姆达表达式,委托、匿名方法、Lambda表达式的演进
总结:Lambda表达式的语法:(参数列表=>执行语句) 无参数格式 :()=>{执行语句} 有参数格式:x=> x % 2 == 0 1.假设给我们一个泛型对象List<T& ...
- Unity多个场景叠加或大场景处理方法小结
本文章由cartzhang编写.转载请注明出处. 全部权利保留. 文章链接: http://blog.csdn.net/cartzhang/article/details/47614153 作者:ca ...
- 源代码分析:onAttach, onMeasure, onLayout, onDraw 的顺序。
从前文<源代码解析:dialog, popupwindow, 和activity 的第一个view是怎么来的?>中知道了activity第一个view或者说根view或者说mDecorVi ...
- Nginx访问日志和错误日志的拆分(Logstash)
>> from zhuhaiqing.info input { file { type =>> "nginx-access" path =>> ...
- amchart 图表设置
官网:https://www.amcharts.com/demos/ 属性介绍:https://docs.amcharts.com/3/javascriptcharts/AmLegend 安装 bow ...
- Redis源码阅读-Dict哈希字典
Dict和Java中的HashMap很相似,都是数组开链法解决冲突. 但是Redis为了高性能, 有很多比较微妙的方法,例如 数组的大小总是2的倍数,初始大小是4. rehash并不是一次就执行完,而 ...
- Java学习笔记之equals和Objects.equals
equals 相信大家就知道,就是比较,我们平时也会在自己定义的类中加入自己重写的equals用来比较两个类是否相同,例如这样 public class Person { private String ...
- CLR内存回收总结,代龄机制
关键字:对象可达图,代龄机制,终止化对象. 代龄机制: 0代满了之后,GC开始回收,剩下的对象升级为1代.(只有不可达的对象才会被回收.) 0代再回收几次之后,1代的对象慢慢增多然后达到阈值,GC同时 ...