Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >hdu 4081 Qin Shi Huang's National Road System (次小生成树)

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

作者头像
Gxjun
发布于 2018-03-26 07:52:39
发布于 2018-03-26 07:52:39
1K00
代码可运行
举报
文章被收录于专栏:mlml
运行总次数:0
代码可运行

Qin Shi Huang's National Road System

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

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

2011 Asia Beijing Regional Contest

代码:

       n个城市,求解max{ A/b } b为次小生成树!  

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
  1 //#define LOCAL
  2 #include<cstdio>
  3 #include<cstdlib>
  4 #include<cstring>
  5 #include<cmath>
  6 #include<iostream>
  7 #include<algorithm>
  8 using namespace std;
  9 const int maxn=1005;
 10 const int inf=0x3f3f3f3f;
 11 struct node
 12 {
 13  int x,y,p;
 14  double dist(const node &cc){
 15    return sqrt((double)(x-cc.x)*(x-cc.x)+(y-cc.y)*(y-cc.y));
 16  }
 17 }sac[maxn];
 18 
 19 bool vis[maxn];
 20 bool road[maxn][maxn];
 21 int pre[maxn];
 22 double maxc[maxn][maxn];
 23 double lowcost[maxn];
 24 double map[maxn][maxn];
 25 double res;
 26 void prim(int st,int en){
 27 
 28   memset(vis,0,sizeof(vis));
 29   memset(road,0,sizeof(road));
 30   memset(maxc,0,sizeof maxc);
 31   for(int i=0;i<en;i++){
 32        lowcost[i]=map[st][i];
 33      pre[i]=st;;
 34   }
 35   vis[st]=1;
 36   res=0;
 37   for(int i=0;i<en;i++)
 38   {
 39       double larger=inf;
 40       int pp=-1;
 41       for(int j=0;j<en;j++)
 42     {
 43         if(!vis[j]&&larger>lowcost[j])
 44         {
 45             larger=lowcost[j];
 46             pp=j;
 47         }
 48     }
 49     if(-1==pp)continue;
 50       road[pp][pre[pp]]=road[pre[pp]][pp]=1;
 51      res+=lowcost[pp];
 52      vis[pp]=1;
 53     for(int i=0;i<en;i++)
 54     {
 55 
 56         if(!vis[i]&&lowcost[i]>map[pp][i]){
 57             lowcost[i]=map[pp][i];
 58             pre[i]=pp;
 59         }
 60         //求解生成树的最大边
 61          if(vis[i]&&i!=pp){
 62           maxc[i][pp]=maxc[pp][i]=max(maxc[i][pre[pp]],lowcost[pp]);
 63         }
 64     }
 65   }
 66   return ;
 67 }
 68 
 69 int main()
 70 {
 71   #ifdef LOCAL
 72      freopen("test.in","r",stdin);
 73   #endif
 74    int tt,nn;
 75    scanf("%d",&tt);
 76   while(tt--){
 77       scanf("%d",&nn);
 78   //    memset(map,0,sizeof(map));
 79     for(int i=0;i<nn;i++){
 80        scanf("%d%d%d",&sac[i].x,&sac[i].y,&sac[i].p);
 81         map[i][i]=0;
 82        for(int j=i-1;j>=0;--j){
 83          map[i][j]=map[j][i]=sac[i].dist(sac[j]);
 84        }
 85     }
 86     prim(0,nn);
 87     double ans=0.0;
 88    for(int i=0;i<nn;i++){
 89       for(int j=0;j<nn;j++){
 90           if(i!=j)
 91         {
 92             double tol_p=sac[i].p+sac[j].p;
 93            if(road[i][j])
 94                 ans=max(tol_p/(res-map[i][j]),ans);
 95            else
 96                 ans=max(tol_p/(res-maxc[i][j]),ans);
 97         }
 98       }
 99    }
100    printf("%.2lf\n",ans);
101   }
102   return 0;
103 }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2014-11-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
hdu--(1025)Constructing Roads In JGShining's Kingdom(dp/LIS+二分)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16047    Accepted Submission(s): 4580 Problem Description JGShining's kingdom consists of 2n(n is no more
Gxjun
2018/03/26
6000
hdu--(1025)Constructing Roads In JGShining's Kingdom(dp/LIS+二分)
【PAT】甲级1003 - Emergency(最短路)
时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.
FishWang
2025/08/27
900
HDUOJ----1301 Jungle Roads
Jungle Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot
Gxjun
2018/03/21
5610
HDUOJ----1301 Jungle Roads
HDU 4081 (次小生成树变形)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4081        题意是有n个点,分别输入n个点的坐标和这个点的权值,然后要给这些点建边,建边的权值
Ch_Zaqdt
2019/01/10
5150
HDUOJ-----(1162)Eddy's picture(最小生成树)
Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6821    Accepted Submission(s): 3444 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to bec
Gxjun
2018/03/26
5420
UVA 10600 ACM Contest and Blackout(次小生成树)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem
Ch_Zaqdt
2019/01/10
4610
hdu 3367(Pseudoforest ) (最大生成树)
Pseudoforest Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1844    Accepted Submission(s): 704 Problem Description In graph theory, a pseudoforest is an undirected graph in which every connecte
Gxjun
2018/03/26
1.1K0
DP 60题 -2 HDU1025 Constructing Roads In JGShining's Kingdom
JGShining's kingdom consists of 2n(n is no more than 500,000) small cities which are located in two parallel lines. Half of these cities are rich in resource (we call them rich cities) while the others are short of resource (we call them poor cities). Each poor city is short of exactly one kind of resource and also each rich city is rich in exactly one kind of resource. You may assume no two poor cities are short of one same kind of resource and no two rich cities are rich in one same kind of resource. With the development of industry, poor cities wanna import resource from rich ones. The roads existed are so small that they're unable to ensure the heavy trucks, so new roads should be built. The poor cities strongly BS each other, so are the rich ones. Poor cities don't wanna build a road with other poor ones, and rich ones also can't abide sharing an end of road with other rich ones. Because of economic benefit, any rich city will be willing to export resource to any poor one. Rich citis marked from 1 to n are located in Line I and poor ones marked from 1 to n are located in Line II. The location of Rich City 1 is on the left of all other cities, Rich City 2 is on the left of all other cities excluding Rich City 1, Rich City 3 is on the right of Rich City 1 and Rich City 2 but on the left of all other cities ... And so as the poor ones. But as you know, two crossed roads may cause a lot of traffic accident so JGShining has established a law to forbid constructing crossed roads. For example, the roads in Figure I are forbidden.
风骨散人Chiam
2020/10/28
3610
DP 60题 -2 HDU1025 Constructing Roads In JGShining's Kingdom
hdu1043
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<vector> #include<cmath> #include<map> #include<string> #define inf 1<<30 #define eps 1e-7 #define LD long double #define LL long long #define maxn 1000000005
@坤的
2018/06/04
4630
【POJ 1679】The Unique MST(次小生成树)
找出最小生成树,同时用Max[i][j]记录i到j的唯一路径上最大边权。然后用不在最小生成树里的边i-j来替换,看看是否差值为0。
饶文津
2020/06/02
2860
POJ 2594 Treasure Exploration
Description Have you ever read any book about treasure exploration? Have you ever see any film about
attack
2018/04/11
5940
HDUOJ --2544最短路(基础)
输入包括多组数据。每组数据第一行是两个整数N、M(N<=100,M<=10000),N表示成都的大街上有几个路口,标号为1的路口是商店所在地,标号为N的路口是赛场所在地,M则表示在成都有几条路。N=M=0表示输入结束。接下来M行,每行包括3个整数A,B,C(1<=A,B<=N,1<=C<=1000),表示在路口A与路口B之间有一条路,我们的工作人员需要C分钟的时间走过这条路。 输入保证至少存在1条商店到赛场的路线。 Output 对于每组输入,输出一行,表示工作人员从商店走到赛场的最短时间 Sample I
Gxjun
2018/03/22
5860
HDUOJ---1102Constructing Roads
Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 12173    Accepted Submission(s): 4627 Problem Description There are N villages, which are numbered from 1 to N, and you should bu
Gxjun
2018/03/22
5100
畅通工程再续(最小生成树)
这道题被坑的难受,很基础的题目,但是还是wa的郁闷,主要的错误是不懂的分析,导致变量的定义出错,记录k点的最短边要double的却依旧写int导致wa的找不出错了 #include<stdio.h> #include<math.h> #include<string.h> const int MAXN=1100; const int INF=0x7fffffff; struct Node { int x,y; } node[MAXN*2]; double map[MAXN][MAXN]; int
用户1624346
2018/04/17
6730
hdu----(2586)How far away ?(DFS/LCA/RMQ)
How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5492    Accepted Submission(s): 2090 Problem Description There are n houses in the village and some bidirectional roads connecting
Gxjun
2018/03/26
5740
POJ2421 Constructing Roads 最小生成树
有N个村庄,编号从1到N,您应该修建一些道路,使每两个村庄可以相互连接。我们说两个村庄A和B是连通的,当且仅当A和B之间有一条道路,或者存在一个村庄C使得A和C之间有一条道路,并且C和B连通时。 我们知道,一些村庄之间已经存在一些道路,您的工作是建造一些道路,以使所有村庄都连接起来,并且所有道路的长度都应最小。
风骨散人Chiam
2020/10/28
3860
逮捕罪犯(树)- HDU 3069
Country ALPC has n cities, and the cities are connected by undirected roads. Furthermore, there is exactly one route between each pair of cities. Now some criminals break the prison and the police do not know where they are. And the criminals can stay in a city or move on the roads. The police office has made a decision to send policemen to arrest the breakers. If a police and a criminal at a same point at the same time the criminal is under arrest, no matter in a city or on a road. The police office wants to minimize the number of policemen to arrest all the criminals. Now the map of Country ALPC is given, please tell the police office the least number of policemen required.
ACM算法日常
2018/08/07
2720
逮捕罪犯(树)- HDU 3069
Tricks Device (hdu 5294 最短路+最大流)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 124 Accepted Submission(s): 27
全栈程序员站长
2022/07/10
2410
最小生成树判断唯一
题意:若最小生成树唯一则输出权值和,若不唯一输出Not Not Unique! 运用prim算法将最小生成树求出,然后在依次枚举删除最小生成树中的每一条边,判断是否还能构成一个新的最小生成树,且权值和与初始的权值和相等,若能构成则不唯一 #include<stdio.h> #include<stdlib.h> #include<vector> using namespace std; /*看了很久才相处为什么要用这个stl 假设v,u都为最小生成树中的点,但是 v,u所扩展出来的最小生成树边却不一定相等 所
用户1624346
2018/04/11
1K0
CodeForces 666B World Tour(spfa+枚举)
B. World Tour time limit per test 5 seconds memory limit per test 512 megabytes input standard input output standard output A famous sculptor Cicasso goes to a world tour! Well, it is not actually a world-wide. But not everyone should have th
ShenduCC
2018/04/26
5900
CodeForces 666B World Tour(spfa+枚举)
相关推荐
hdu--(1025)Constructing Roads In JGShining's Kingdom(dp/LIS+二分)
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验