题意:

秦始皇要修路使得所有的城市连起来,并且花费最少;有一个人,叫徐福,他可以修一条魔法路,不花费任何的钱与劳动力。

秦始皇想让修路的费用最少,但是徐福想要受益的人最多,所以他们经过协商,决定让 A / B 最大,A代表被魔法路连接的两个城市的人口总数,B代表修的路中非魔法路的总长度。

输出 A / B 的最大值。

思路:

A / B 最大,则A尽可能大,B尽可能小,所以首先把MST求出来。由于每个城市的人口有很大的偏差,所以必须枚举每一条边,计算连接的两个城市的人口,复杂度为O(n^2),所以每次替换边的复杂度必须是O(1)。

由于是稠密图,所以用prim算法,prim算法在O(n^2)的复杂度的时候,可以维护最小生成树上两点之间的最长边,这样就可以在过程中把两点间的最长边保存下来。这个是依靠已知的前驱节点实现的。复杂度为O(n^2)。

枚举每一条边时,如果这条边是MST中的边,那么就直接计算 A / B;如果这条边不在MST中,加入这条边就会成环,这时我们保存的信息就起作用了,成环之后把在MST中的连接这两点的最长边去掉,就是新的生成树的权值,且保证了B尽可能小。替换的时间复杂度为O(1)。

代码:

 #include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <math.h>
using namespace std;
const int maxn = ;
double path[maxn][maxn];
double g[maxn][maxn];
double dis[maxn];
bool vis[maxn];
bool used[maxn][maxn];
int peo[maxn];
int pre[maxn];
double ans;
struct point
{
int x,y;
}p[maxn]; double cal(int i,int j)
{
double x2 = (p[i].x - p[j].x) * (p[i].x - p[j].x);
double y2 = (p[i].y - p[j].y) * (p[i].y - p[j].y); return sqrt(x2 + y2);
} int prim(int n)
{
memset(vis,,sizeof(vis));
memset(path,,sizeof(path));
memset(used,,sizeof(used));
for (int i = ;i <= n;i++) dis[i] = 1e15; vis[] = ;
dis[] = ; int tot = ;
ans = ;
//double len = 0; for (int i = ;i <= n;i++)
{
pre[i] = ;
dis[i] = g[][i];
} for (int i = ;i < n;i++)
{
int u;
double d = 1e15; for (int j = ;j <= n;j++)
{
if (!vis[j] && dis[j] < d)
{
d = dis[j];
u = j;
}
} vis[u] = ; ans += d; //tot = max(peo[u] + peo[pre[u]],tot); used[u][pre[u]] = used[pre[u]][u] = ; for (int j = ;j <= n;j++)
{
if (vis[j] && j != u)
path[u][j] = path[j][u] = max(d,path[j][pre[u]]);
} for (int j = ;j <= n;j++)
{
if (!vis[j])
{
if (g[u][j] < dis[j])
{
dis[j] = g[u][j];
pre[j] = u;
}
}
}
} //printf("%.2f **\n",ans); return tot;
} int main()
{
int t; scanf("%d",&t); while (t--)
{
int n; scanf("%d",&n); for (int i = ;i <= n;i++)
{
scanf("%d%d%d",&p[i].x,&p[i].y,&peo[i]);
} for (int i = ;i <= n;i++)
{
for (int j = ;j <= n;j++)
{
g[i][j] = 1e15;
}
} for (int i = ;i <= n;i++)
{
for (int j = i+;j <= n;j++)
{
g[i][j] = g[j][i] = cal(i,j);
}
} prim(n); double ans1 = ; for (int i = ;i <= n;i++)
{
for (int j = i + ;j <= n;j++)
{
if (used[i][j])
{
int peop = peo[i] + peo[j];
ans1 = max(peop / (ans - g[i][j]),ans1); //printf("%d %d %d %.2f **\n",i,j,peop,ans - g[i][j]);
}
else
{
int peop = peo[i] + peo[j];
ans1 = max(peop / (ans - path[i][j]),ans1);
//printf("%d %d %d %.2f **\n",i,j,peop,ans - path[i][j]);
}
}
} printf("%.2f\n",ans1); //printf("%.2f",path[1][4]);
} return ;
}

uvalive 5731 Qin Shi Huang’s National Road System的更多相关文章

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

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

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

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

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

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

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

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

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

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

  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. idea出现无效的源发行版:11

    idea启动的时候报错: 点击->file->project structure->project

  2. java 网络编程(四)TCP通讯

    客户端: package cn.sasa.TcpDemo; import java.io.IOException; import java.io.InputStream; import java.io ...

  3. 使用poi读写excel文件

    使用poi库测试了一下读取excel文件,效果不错,跟大家分享一下. 第一列是数值型,第二列是字符型,代码如下: package poi; import java.io.FileInputStream ...

  4. 深入理解Java虚拟机笔记

    1. Java虚拟机所管理的内存 2. 对象创建过程 3. GC收集 4. HotSpot算法的实现 5. 垃圾收集器 6. 对象分配内存与回收细节 7. 类文件结构 8. 虚拟机类加载机制 9.类加 ...

  5. Docker 镜像(五)

    我们都知道,操作系统分为内核和用户空间.对于 Linux 而言,内核启动后,会挂载 root 文件系统为其提供用户空间支持.而 Docker 镜像(Image),就相当于是一个 root 文件系统.比 ...

  6. 【叶问】 MySQL常用的sql调优手段或工具有哪些

     MySQL常用的sql调优手段或工具有哪些1.根据执行计划优化   通常使用desc或explain,另外可以添加format=json来输出更详细的json格式的执行计划,主要注意点如下:     ...

  7. javascript篇-浅拷贝与深拷贝

    理解javascript 的浅拷贝与深拷贝,首先看一下js的数据类型: js有5种基本数据类型:undefined,null,boolean,number,string 还有一种复杂的数据类型(也叫引 ...

  8. java 根据word xml模板生成word

    这里用的是poi相关jar包以及freemarker插值技术实现,poi相关jar包这里不再述说 1,编辑word并保存为xml 2,把xml后缀改为ftl文件 3,前端代码 // alert(jso ...

  9. golang 的 TUI 及 GUI 库

    interactive prompt library: abiosoft/ishell                           https://github.com/abiosoft/is ...

  10. Mac提醒事项如何设置为24小时制