这道题在LRJ的书上看到,今天回过头来继续看这题,发现很多东西都已经明白了。

题意:有N个城市,每个城市有一个坐标和人口。

现在要建一些边使得他们都联通,花费就是这些边的长度,然后有一条边可以免费。问免费一条边之后,使得免费的该条边的两个城市的人口/剩下来的边的长度 ,这个比值最大。

思路:首先做一遍MST,求出MST之后,我们枚举每条边,看这条边是否可以删除,也就是免费。

那么删除一条边之后对MST有什么影响呢。

首先我们假设免费(删除)了一条边a -> b ,权值是c 。假设这条边是MST上的边,那么我们只能删除这条边 。

假设这条边不是MST上的边,那么我们可以删除a -> b权值最大的一条边,因为我们是要使得剩下来的长度最小。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <stack>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#define ll long long
#define inf 1<<30
#define PI acos(-1.0)
#define mem(a , b) memset(a , b ,sizeof(a))
using namespace std ; struct kdq {
int s , e ;
double l ;
bool operator < (const kdq & fk)const {
return l > fk.l ;
}
} ;
inline void RD(int &ret) {
char c;
int flag = 1 ;
do {
c = getchar();
if(c == '-')flag = -1 ;
} while(c < '0' || c > '9') ;
ret = c - '0';
while((c=getchar()) >= '0' && c <= '9')
ret = ret * 10 + ( c - '0' );
ret *= flag ;
}
#define N 1111
double ed[N][N] , edM[N][N] ;
int x[N] , y[N], pop[N] ;
int n ;
double mst = 0 ;
double getdis(int i ,int j) {
return sqrt(1.0 * (x[i] - x[j]) * (x[i] - x[j]) + 1.0 * (y[i] - y[j]) * (y[i] - y[j])) ;
}
void init() {
cin >> n ;
for (int i = 1; i <= n ; i ++ ) {
RD(x[i]) ; RD(y[i]) ; RD(pop[i]) ;
}
for (int i = 1 ; i <= n ; i ++ ) {
for (int j = 1; j <= n ; j ++ ) {
if(i == j)ed[i][j] = 0 ;
else ed[i][j] = getdis(i , j) ;
}
}
mst = 0 ;
}
double dis[N] ;
int vis[N] ;
bool ok[N][N] ;
vector<int>E[N] ; void prim() {
priority_queue<kdq>qe ;
for (int i = 1 ; i <= n ; i ++ ) {
dis[i] = ed[1][i] ;
E[i].clear() ;
}
mem(vis ,0) ;
mem(ok ,0) ;
for (int i = 1 ; i <= n ; i ++ ) {
qe.push((kdq) {1 , i , ed[1][i]}) ;
}
dis[1] = 0 , vis[1] = 1 ;
while(!qe.empty()) {
kdq tp = qe.top() ;
qe.pop() ;
if(vis[tp.e])continue ;
mst += ed[tp.s][tp.e] ;
vis[tp.e] = 1 ;
ok[tp.s][tp.e] = ok[tp.e][tp.s] = 1 ;
E[tp.s].push_back(tp.e) ;
E[tp.e].push_back(tp.s) ;
for (int i = 1 ; i <= n ; i ++ ) {
if(!vis[i] && dis[i] > ed[tp.e][i]) {
dis[i] = ed[tp.e][i] ;
qe.push((kdq){tp.e , i , ed[tp.e][i]}) ;
}
}
}
}
void dfs(int root ,int fa ,int now ,double MAX) {
edM[root][now] = MAX ;
int sz = E[now].size() ;
for (int i = 0 ; i < sz ; i ++ ) {
int e = E[now][i] ;
if(e == fa)continue ;
dfs(root , now , e , max(MAX , ed[now][e])) ;
}
}
void solve() {
init() ;
prim() ;
for (int i = 1 ; i <= n ; i ++ ) {
dfs(i , -1 , i , 0) ;
}
double ans = -1 ;
for (int i = 1 ; i <= n ; i ++ ) {
for (int j = 1 ; j < i ; j ++ ) {
if(ok[i][j])
ans = max(ans , (pop[i] + pop[j]) * 1.0 / (mst - ed[i][j])) ;
else
ans = max(ans , (pop[i] + pop[j]) * 1.0 / (mst - edM[i][j])) ;
}
}
printf("%.2f\n",ans) ;
}
int main() {
int _ ;cin >> _ ;while(_ -- )solve() ;
return 0;
}

HDU 4081 MST的更多相关文章

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

  2. Qin Shi Huang's National Road System HDU - 4081(树形dp+最小生成树)

    Qin Shi Huang's National Road System HDU - 4081 感觉这道题和hdu4756很像... 求最小生成树里面删去一边E1 再加一边E2 求该边两顶点权值和除以 ...

  3. HDU - 4081 Qin Shi Huang's National Road System 【次小生成树】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4081 题意 给出n个城市的坐标 以及 每个城市里面有多少人 秦始皇想造路 让每个城市都连通 (直接或者 ...

  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(次小生成树prim)

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

  6. Hdu 4081 最小生成树

    Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/3 ...

  7. hdu 4756 MST+树形dp ****

    题意:给你n(n = 1000)个二维点,第一个点是power plant,还有n - 1个点是dormitories.然后现在知道有一条寝室到寝室的边是不能连的,但是我们不知道是哪条边,问这种情况下 ...

  8. HDU 4081 Qin Shi Huang's National Road System 最小生成树

    分析:http://www.cnblogs.com/wally/archive/2013/02/04/2892194.html 这个题就是多一个限制,就是求包含每条边的最小生成树,这个求出原始最小生成 ...

  9. HDU 4081 Qin Shi Huang's National Road System

    步骤是先求最小生成树,然后选两个不同的点,遍历所有的这样的点,选出两点人口比较大,而且连通两点的边的最大边比较大的情况. 因此要对i,j点连接起来的边进行遍历. #include<stdio.h ...

随机推荐

  1. openstack vm image

    1,openstack 基于iso生成镜像

  2. Oleg Sych - » Pros and Cons of T4 in Visual Studio 2008

    Oleg Sych - » Pros and Cons of T4 in Visual Studio 2008 Pros and Cons of T4 in Visual Studio 2008 Po ...

  3. apache FtpServer 整合spring部署

    我们在项目中可能会出现这样的需求,使用ftp上传很大的文件后对需要对文件进行相应的逻辑处理,这时我们可以使用apache ftpServer来处理这段逻辑,只要我们做相应的部署和编写我们的逻辑代码,这 ...

  4. Linux MySql安装步骤

    本文将以MySQL 5.5.47为例,以CentOS 6.5为平台,讲述MySQL数据库的安装和设置. 源码包方式安装 1.新建MySql用户和用户组 groupadd mysql useradd - ...

  5. HDMI相关知识

    HDMI热插拔检测原理 HDMI(19Pin)/DVI(16 pin)的功能是热插拔检测(HPD),这个信号将作为主机系统是否对HDMI/DVI是否发送TMDS信号的依据.HPD是从显示器输出送往计算 ...

  6. 怎么查看chrome网络日志

    最近在分析一个页面访问慢的问题,在分析的除了wireshark抓包等手段外,还用到了chrome的日志辅助分析 使用 chrome://net-internals/#events 可以打开日志追踪窗口 ...

  7. DNA Sorting(排序)

    欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) DNA Sorting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: ...

  8. 教训:TOJ[4081] God Le wants to know the directory

    以前的字符串题本来就弱..2年不写就更弱了.嗯.留作教训 God Le is the most talented ACMer in the TJU-ACM team. When he wants to ...

  9. typedef和define的作用域

    typedef: 如果放在所有函数之外,它的作用域就是从它定义开始直到文件尾: 如果放在某个函数内,定义域就是从定义开始直到该函数结尾: #define: 不管是在某个函数内,还是在所有函数之外,作用 ...

  10. Struct和Class的区别

    转载至:http://blog.csdn.net/yuliu0552/article/details/6717915 C++中的struct对C中的struct进行了扩充,它已经不再只是一个包含不同数 ...