LA:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3714

HDU:http://acm.hdu.edu.cn/showproblem.php?pid=4081

题目大意:

秦始皇要在n个城市之间修筑一条道路使得任意两个城市均可连通。有个道士可以用法力帮忙修一条路。秦始皇希望其他的道路总长B最短且用法术连接的两个城市的人口之和A尽量大,因此下令寻找一个A / B的最大方案。

思路:

先求出MST,然后枚举用法术修哪一条边。那么,法术修的边显然应该是MST中连接两个点 u ,v中最大的一条边。

所以对MST使用DFS,当访问一个新结点cur时候,考虑所有已经访问过的老结点x,更新f(x,u)=max( f(x,v) ,w(u,v) );其中v是u的老结点。

这样,枚举任意的两个城市,ans = max(ans, (p[i].num+p[j].num) / (mstlen - dp[i][j]));   p[i].num为城市i人数,mstlen为最小生成树的长度,dp[i][j]就是上面说的连接i 和j 最大的一条边。

下次直接用vector的了,好多的len什么的。。。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
const int MAXN=1000+10;
int n;
int p_len,e_len,len;//点的数目,各个点距离的数目,MST的邻接表边数,dfs的顶点数
int fa[MAXN],head[MAXN];
double dp[MAXN][MAXN],mstlen;
int find(int cur)
{
return cur==fa[cur]? cur : fa[cur]=find(fa[cur]);
}
struct point //记录各个城市的坐标和人口
{
int x,y;
int num;
}p[MAXN];
struct edge //化坐标为距离求MST
{
int from,to;
double dis;
bool operator<(const edge &x)const{
return dis<x.dis;
}
}e[MAXN*MAXN];
struct edge2 //MST邻接表
{
int to,next;
double val;
}e2[MAXN*2]; void add(int from,int to,double val)
{
e2[len].to=to;
e2[len].val=val;
e2[len].next=head[from];
head[from]=len++;
}
//当访问一个新结点cur时候,考虑所有已经访问过的老结点x,更新f(x,u)=max( f(x,v) ,w(u,v) );
//其中v是u的老结点。
vector<int> nodes;
void dfs(int cur,int fa,double dis)
{
for(int i = 0; i < nodes.size(); i++) {
int x = nodes[i];
dp[cur][x] = dp[x][cur] = max(dp[x][fa], dis);
}
nodes.push_back(cur);
for(int i=head[cur];i!=-1;i=e2[i].next)
{
int id=e2[i].to;
if(id != fa) dfs(id,cur,e2[i].val);
} }
//求MST,并且记录
void kruskal()
{
memset(head,-1,sizeof(head));
for(int i=0;i<n;i++)
fa[i]=i; int &len=e_len;
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
{
e[len].from=i;
e[len].to=j;
e[len++].dis = sqrt( (double)(p[i].y-p[j].y)*(p[i].y-p[j].y)
+(double)(p[i].x-p[j].x)*(p[i].x-p[j].x) );
} int cnt=0;
mstlen=0;
sort(e,e+len);
for(int i=0;i<len;i++)
{
int x=e[i].from,y=e[i].to;
int root_x=find(x),root_y=find(y);
if(root_x==root_y) continue;
mstlen+=e[i].dis;
fa[root_x]=root_y;
add(x,y,e[i].dis); //建立MST的邻接表
add(y,x,e[i].dis);
if(++cnt > n-1) break;
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
len=e_len=p_len=0;
memset(dp,0,sizeof(dp));
nodes.clear(); scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].num); kruskal(); dfs(0,-1,0);
double ans = -1;
for(int i = 0; i < n; i++)
for(int j = i+1; j < n; j++) {
ans = max(ans, (p[i].num+p[j].num) / (mstlen - dp[i][j]));
}
printf("%.2lf\n", ans);
}
return 0;
}

LA 5713 - Qin Shi Huang's National Road System(HDU 4081) MST的更多相关文章

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

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

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

  3. UVALive 5713 Qin Shi Huang's National Road System秦始皇修路(MST,最小瓶颈路)

    题意: 秦始皇要在n个城市之间修路,而徐福声可以用法术位秦始皇免费修1条路,每个城市还有人口数,现要求徐福声所修之路的两城市的人口数之和A尽量大,而使n个城市互通需要修的路长B尽量短,从而使得A/B最 ...

  4. UVALive 5713 Qin Shi Huang's National Road System(次小生成树)

    题意:对于已知的网络构建道路,使城市两两之间能够互相到达.其中一条道路是可以免费修建的,问需要修建的总长度B与免费修建的道路所连接的两城市的人口之和A的比值A/B最大是多少. 因为是求A/B的最大值, ...

  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 (次小生成树的变形)

    题目:Qin Shi Huang's National Road System Qin Shi Huang's National Road System Time Limit: 2000/1000 M ...

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

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

  9. HDU4081 Qin Shi Huang's National Road System 2017-05-10 23:16 41人阅读 评论(0) 收藏

    Qin Shi Huang's National Road System                                                                 ...

随机推荐

  1. 4.Maven之(四)Maven命令

    转自:https://blog.csdn.net/u012152619/article/details/51473410

  2. javap 命令 反编译代码

    javap 命令 javap -c classname 一个好的分析class二进制文件的 链接 http://blog.csdn.net/pwlazy/article/details/7954169

  3. C/C++(基础-常量,类型转换)

    字符 char ascII码表 #include<stdio.h> int main() { unsigned char ch; for(ch = 0;ch < 128;ch++) ...

  4. ErrorSet

    1.获取路径的失误: 例子是对一个列表项的悬浮操作: ~(function() { var lists = $(".footer_log>li"); lists.each(f ...

  5. Easy mock - 安装配置和基本使用

    Easy-mock easy-mock是一款比较好用的接口模拟工具, 使用之前我们需要安装和配置 需要下载的内容有以下 Node Redis MongoDB Node和Redis一路点下一步就行, M ...

  6. 【2017 Multi-University Training Contest - Team 5】Rikka with Graph

    [Link]:http://acm.hdu.edu.cn/showproblem.php?pid=6090 [Description] 给你n个点; 让你在这n个点上最多连m条无向边; 使得 ∑ni= ...

  7. ActiveMQ学习总结(2)——ActiveMQ入门实例教程

    1.下载ActiveMQ 去官方网站下载:http://activemq.apache.org/ 2.运行ActiveMQ 解压缩apache-activemq-5.5.1-bin.zip,然后双击a ...

  8. java匿名内部类使用场景列举

    示例一: package com;      interface Operation {       double operateTwoIntNum(int a, int b);   }      p ...

  9. Vue render: h => h(App) $mount

    $mount()手动挂载 当Vue实例没有el属性时,则该实例尚没有挂载到某个dom中: 假如需要延迟挂载,可以在之后手动调用vm.$mount()方法来挂载.例如: new Vue({ //el: ...

  10. JAVA MessageDigest(MD5加密等)

    转自http://blog.csdn.net/hudashi/article/details/8394158 一.概述 java.security.MessageDigest类用于为应用程序提供信息摘 ...