题目链接

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 【次小生成树】的更多相关文章

  1. 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 ...

  2. 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 ...

  3. HDU 4081 Qin Shi Huang's National Road System [次小生成树]

    题意: 秦始皇要建路,一共有n个城市,建n-1条路连接. 给了n个城市的坐标和每个城市的人数. 然后建n-2条正常路和n-1条魔法路,最后求A/B的最大值. A代表所建的魔法路的连接的城市的市民的人数 ...

  4. 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: ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. hdu 4081 Qin Shi Huang's National Road System(次小生成树prim)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4081 题意:有n个城市,秦始皇要修用n-1条路把它们连起来,要求从任一点出发,都可以到达其它的任意点. ...

随机推荐

  1. 【BIEE】01_下载安装BIEE(Business Intelligence)11g 11.1.1.9.0

    环境准备 安装文件 如果操作系统是64位,则下载64位版本,我安装的系统是64位的 1.下载所有安装文件 1.1 Oracle Database 11g R2 下载地址: http://www.ora ...

  2. 【VBA】获取Excle的安装路径

    在VBA中,如何获取Excle的安装路径呢?请看以下代码: Sub 获取Excle的安装路径() MsgBox "Excle的安装路径为:" & Application.P ...

  3. Roboware 下打包成so 文件并引用

      一.生成.so文件 在ros中编译.so文件,如同在vs中编译C++版的dll文件.具体步骤如下: 步骤1: 首先建立.h文件和一个.cpp文件(该.cpp文件就是此次封装的内容)   步骤2: ...

  4. rabbit 函数参数详解

    http://blog.csdn.net/chwshuang/article/details/50512057 http://www.cnblogs.com/LiangSW/p/6224333.htm ...

  5. PowerShell-将CSV导入SQL Server

    $database = 'foxdeploy' $server = '.' $table = 'dbo.powershell_test' Import-CSV .\yourcsv.csv | ForE ...

  6. 【LeetCode-面试算法经典-Java实现】【015-3 Sum(三个数的和)】

    [015-3 Sum(三个数的和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an array S of n integers, are there ...

  7. android性能优化学习笔记(加快应用程序启动速度:)

    一:安卓中应用程序的启动方式有两种: 冷启动:后台没有该应用进程,系统会重新创建一个进程分配给该应用(所以会先创建和初始化Application类,再创建和初始化MainActivity,包括测量,布 ...

  8. list集合转换成json类型

    public String gettext(HttpServletRequest request,HttpServletResponse response){ List<xuanhuan_> ...

  9. mock测试类的时候,添加@InjectMocks

    1.在单元测试某个类的时候,引入该类的时,添加注解@InjectMocks 2.该类的变量,需要添加注解:@Mock 3.类中需要第三方协作者时,通常会用到get和set方法注入.通过spring框架 ...

  10. Springboot Maven 多模块项目中 @Service跨模块引用失败的问题

    子模块中引用另一个子模块中的Service, @Autowired失败. 添加了模块之间的依赖没解决. 组以后在启动类上加上 @SpringBootApplication(scanBasePackag ...