题目:Qin Shi Huang's National Road System

Qin Shi Huang's National Road System

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2049    Accepted Submission(s): 746

Problem Description
During the Warring States Period of ancient China(476 BC to 221 BC), there were seven kingdoms in China ---- they were Qi, Chu, Yan, Han, Zhao, Wei and Qin. Ying Zheng was the king of the kingdom Qin. Through 9 years of wars, he finally conquered all six other kingdoms and became the first emperor of a unified China in 221 BC. That was Qin dynasty ---- the first imperial dynasty of China(not to be confused with the Qing Dynasty, the last dynasty of China). So Ying Zheng named himself "Qin Shi Huang" because "Shi Huang" means "the first emperor" in Chinese.

Qin Shi Huang undertook gigantic projects, including the first version of the Great Wall of China, the now famous city-sized mausoleum guarded by a life-sized Terracotta Army, and a massive national road system. There is a story about the road system:

There were n cities in China and Qin Shi Huang wanted them all be connected by n-1 roads, in order that he could go to every city from the capital city Xianyang.

Although Qin Shi Huang was a tyrant, he wanted the total length of all roads to be minimum,so that the road system may not cost too many people's life. A daoshi (some kind of monk) named Xu Fu told Qin Shi Huang that he could build a road by magic and that magic road would cost no money and no labor. But Xu Fu could only build ONE magic road for Qin Shi Huang. So Qin Shi Huang had to decide where to build the magic road. Qin Shi Huang wanted the total length of all none magic roads to be as small as possible, but Xu Fu wanted the magic road to benefit as many people as possible ---- So Qin Shi Huang decided that the value of A/B (the ratio of A to B) must be the maximum, which A is the total population of the two cites connected by the magic road, and B is the total length of none magic roads.

Would you help Qin Shi Huang?

A city can be considered as a point, and a road can be considered as a line segment connecting two points.

 
Input
The first line contains an integer t meaning that there are t test cases(t <= 10).

For each test case:

The first line is an integer n meaning that there are n cities(2 < n <= 1000).

Then n lines follow. Each line contains three integers X, Y and P ( 0 <= X, Y <= 1000, 0 < P < 100000). (X, Y) is the coordinate of a city and P is the population of that city.

It is guaranteed that each city has a distinct location.
 
Output
For each test case, print a line indicating the above mentioned maximum ratio A/B. The result should be rounded to 2 digits after decimal point.
 
Sample Input
2
4
1 1 20
1 2 30
200 2 80
200 1 100
3
1 1 20
1 2 30
2 2 40
 
Sample Output
65.00
70.00
 
Source
 
Recommend
lcy

题意:秦始皇修路要把所有的城市都连通,每个城市有相应的人口数,每条路有相应的修路费。

现在可以选一条magic路,修路费变为0,以A代表magic 路两端的人口数和。B代表总路费。

选一条路作为magic路,使A/B最大。

分析:不能用贪心,因为A与B相互制约。

要使A/B最大,那么B应该最小。故先求出n个点的最小生成树。再枚举

每一条边,假设最小生成树的值是B, 而枚举的那条边长度是edge[i][j],  如果这一条边已经

是属于最小生成树上的,那么最终式子的值是A/(B-edge[i][j])。如果这一条不属于最小生成

树上的, 那么添加上这条边,就会有n条边,那么就会使得有了一个环,为了使得它还是一

个生成树,就要删掉环上的一条边。 为了让生成树尽量少,那么就要删掉除了加入的那条边

以外,权值最大的那条路径。 假设删除的那个边的权值是Max[i][j], 那么就是A/(B-Max[i][j]).

即:如果把这条边当作magic road的话,那么这条边以及连接u v 的mst的边就组成了一个环了

当前这条边的权值是最大的,要使剩下的路的花费最小,那么肯定要把u v间的最长的一条边给删

去就行了,也就是找环中的第二大边了。

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std; struct node
{
double x,y;
double peo;
}city[1010];
int vis[1010],mark[1010][1010],pre[1010];
double maps[1010][1010],dis[1010],maxedge[1010][1010];
int n;
double sum,ans; double cal(int i,int j)
{
double xx=(city[i].x-city[j].x)*(city[i].x-city[j].x);
double yy=(city[i].y-city[j].y)*(city[i].y-city[j].y);
return sqrt(xx+yy);
}
void prim()
{
int i,j,v;
double minc;
sum=0;
memset(maxedge,0,sizeof(maxedge));
memset(pre,0,sizeof(pre));
//dis[1]=INF;
for(i=2;i<=n;i++)
{
dis[i]=maps[1][i];
pre[i]=1;
}
vis[1]=1;
for(i=1;i<n;i++)
{
minc=INF;
v=1;
for(j=1;j<=n;j++)
{
if(!vis[j] && dis[j]<minc)
{
minc=dis[j];
v=j;
}
}
sum+=minc;
mark[pre[v]][v]=mark[v][pre[v]]=1;
vis[v]=1;
for(j=1;j<=n;j++)
{
if(vis[j] && j!=v)
{
maxedge[j][v]=maxedge[v][j]=max(dis[v],maxedge[pre[v]][j]);
}
if(!vis[j] && maps[v][j]<dis[j])
{
dis[j]=maps[v][j];
pre[j]=v;
}
}
}
}
int main()
{
int T,i,j;
scanf("%d",&T);
while(T--)
{
memset(mark,0,sizeof(mark));
memset(vis,0,sizeof(vis));
memset(dis,0,sizeof(dis));
memset(city,0,sizeof(city));
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%lf%lf%lf",&city[i].x,&city[i].y,&city[i].peo);
for(j=1;j<i;j++)
maps[i][j]=maps[j][i]=cal(i,j);
}
prim();
ans=-1;
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(!mark[i][j])
ans=max(ans,(city[i].peo+city[j].peo)/(sum-maxedge[i][j]));
else
ans=max(ans,(city[i].peo+city[j].peo)/(sum-maps[i][j]));
}
}
printf("%.2lf\n",ans);
}
return 0;
}

感想:maxedge[j][v]=maxedge[v][j]=max(dis[v],maxedge[pre[v]][j]);

这一句开始写成maxedge[j][v]=maxedge[v][j]=max(dis[v],maps[pre[v]][j]);

wa了好多好多次。。。。T_T....

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 [次小生成树]

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

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

  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——————【次小生成树、prim】

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

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

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

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

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

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

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

随机推荐

  1. c++,纯虚函数与抽象类

    1.纯虚函数的定义: (1)虚函数被“初始化”为0的函数.声明纯虚函数的一般形式是virtual 函数类型 函数名(参数表列) =0;(2)纯虚函数没有函数体:(3)最后面的“=0”并不表示函数返回值 ...

  2. javascript面向对象——tabs实例

    面向过程—>面向对象 之前在未学习面向对象时,我们都是面向过程编程的.它的优点就是简单,明了,下面就来把面向过程的tabs切换改写成面向对象的方式. html: <div class=&q ...

  3. 基于visual Studio2013解决算法导论之055拓扑排序

     题目 拓扑排序 解决代码及点评 // 拓扑排序.cpp : 定义控制台应用程序的入口点. // // 深度优先.cpp : 定义控制台应用程序的入口点. // // 图的邻接表表示.cpp : ...

  4. 基于visual Studio2013解决算法导论之045斐波那契堆

     题目 斐波那契堆 解决代码及点评 // 斐波那契堆.cpp : 定义控制台应用程序的入口点. // #include<iostream> #include<cstdio> ...

  5. win8安装驱动提示文件哈希值不在指定的目录文件中,此文件可能已损坏或被篡改解决办法

    解决办法: 1. 按快捷键win+R 打开运行命令 2. (请先看完后面的再操作!!)运行输入 shutdown.exe /r /o /f /t 00 3. 点击确定 4. 系统将重启 5. 重启后点 ...

  6. FineReport实现Java报表主题分析的效果图

    Java报表-財务主题-EVA经济附加 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYmVzdF9yZXBvcnQ=/font/5a6L5L2T/font ...

  7. Amazon的AWS账单看起来不是很方便

    发了一个PDF格式的收据,只写了收取的费用,EC2下面的明细没有. DetailAmazon Simple Notification Service $0.00Charges $0.00Estimat ...

  8. 13 - NSURLConnection

    一.互联网 基本概念: HTTP协议 统一标准获取网络资源(其他设备上的东西) 本机 -> 远程服务器(计算机) URL(http有格式的字符串) 本机 <- 远程服务器(计算机) Fil ...

  9. zookeeper 之znode 节点

    <pre name="code" class="html">使用 ls 命令来查看当前 ZooKeeper 中所包含的内容: [zk: 10.77. ...

  10. Fedora20安装完Nvidia后启动一直黑屏解决办法。

    安装完Fedora20后,把Nvidia驱动装上后重起机器一直黑屏时,切换到命令行下:Alt+F2  登陆上去,然后直接更新: su -c ‘yum update’ ,再重起就OK了.