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条路把它们连起来,要求从任一点出发,都可以到达其它的任意点. ...
随机推荐
- jira报错,此域不支持您输入的日期
jira报错,此域不支持您输入的日期 解决方法: 使用20117-1-1这样的格式输入,不要用选择日期.具体原因未知.
- C# 接口的隐式与显示实现说明
以前在用到接口时,从来没注意到接口分为隐式实现与显示实现.昨天在浏览博客时看到相关内容,现在根据自己的理解记录一下,方便日后碰到的时候温习温习. 通俗的来讲,"显示接口实现"就是使 ...
- jvm性能调优常用命令
说明和名词解释: ① 只有进行的运行用户才可以调用命令查看相关信息 ② [pid] 为需要查看的进程的端口号 ③ [file] 为需要导出到的文件的具体地址 ④ [tid] 进程中线程的id 1 ...
- java web 通配符* ? $1 $2 $3
匹配通配符 * 匹配0-n个字符,但不包括“/”.即,“*”只匹配一级目录或文件中的零个或多个字符. ** 匹配0-n个字符,包括“/”.即,“**”匹配多级目录或文件. ? 匹配0-1个字符,但不包 ...
- Windows 10 优化
---恢复内容开始--- 0x00 使开始菜单,任务栏,和操作中心透明 --关闭 右下角开始菜单,选择设置,打开个性化菜单,找到颜色一栏.向下滑至最低端,使开始菜单,任务栏,和操作中心透明选项关闭 0 ...
- MBA人物俞洪敏:亿万富翁的生活表
我的智商非常一般,就是比别人勤奋.我的脑袋不属于特别笨的那种,但肯定也不是顶尖聪明的类型.在北大的50个同学当中,我的智商应该属于中下水平,这说明我不是顶尖高智商. 我的勤奋一般人跟不上.我平均每天工 ...
- 篇三、开发前知识补充:Android的长度单位和屏幕分辨率,这个也是转载~~
这篇文章有点早,不过很实用.单位的实用看最后的红色标注的部分. 屏幕分辨率基础 1.术语和概念 术语 说明 备注 Screen size(屏幕尺寸) 指的是手机实际的物理尺寸,比如常用的2.8英寸,3 ...
- Google Code Jam 2014 资格赛:Problem C. Minesweeper Master
Problem Minesweeper is a computer game that became popular in the 1980s, and is still included in so ...
- 设计模式之前之UML
UML,让系统可视化,让规格和设计文档化的表现方法.下面来简单介绍一下这个UML.
- java数据结构-Vector
1 Vector基础实现为数组 object[] synchronized线程安全 2 扩容使用 System.arraycopy(original, 0, copy, 0,Math.min(ori ...