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/32768 K (Java/Others)

Total Submission(s): 1287    Accepted Submission(s): 475



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 

 

 

【思路】

  最小生成树+边交换。

  题目中要求:两城市P之和为A,其他城市路径长度为B,有A/B最小。

  简单的想可以求出最小生成树之后一次枚举n条边使徐福同学修路然后求一遍MST,时间为O(NMlogM)。

  类比于求次小生成树,我们可以做一遍MST得到总权值tot,预处理出maxcost[][]为两点之间在MST上的最长边。枚举两点ij使徐福修maxcost代表的边(这种情况一定对应着删边后的生成树总权值最小),此时有A=P[i]+P[j],有B=tot-maxcost[i][j],比较得ans。时间为O(N^2)。

 

【代码】

 

 1 #include<cstdio>
 2 #include<cmath>
 3 #include<vector>
 4 #include<cstring>
 5 #include<algorithm>
 6 using namespace std;
 7 
 8 const int maxn = 1000+10;
 9 struct Edge{
10     int v,next;
11     double w;
12 }e[maxn*maxn];
13 int en,front[maxn];
14 inline void AddEdge(int u,int v,double w) {
15     en++; e[en].v=v; e[en].w=w; e[en].next=front[u]; front[u]=en;
16 }
17 
18 int n,m;
19 int x[maxn],y[maxn],p[maxn];
20 double maxcost[maxn][maxn];
21 
22 struct Edge_Krus{
23     int u,v;
24     double w;
25     bool operator<(const Edge_Krus& rhs) const{
26        return w<rhs.w;
27     }
28 }edges[maxn*maxn];
29 int f[maxn];
30 inline int find(int x) {
31     return x==f[x]? x:f[x]=find(f[x]);
32 }
33 inline double dist(int i,int j) {
34     return sqrt((double)(x[i]-x[j])*(x[i]-x[j])+(double)(y[i]-y[j])*(y[i]-y[j]));
35 }
36 double Kruskal()
37 {
38     for(int i=1;i<=n;i++)
39        for(int j=i+1;j<=n;j++)
40           edges[m++]=(Edge_Krus) {i,j,dist(i,j)};
41     for(int i=1;i<=n;i++) f[i]=i;
42     sort(edges,edges+m);
43     int cnt=0;
44     double res=0;
45     for(int i=0;i<m;i++) {
46         int x=find(edges[i].u),y=find(edges[i].v);
47         if(x!=y) {
48             f[x]=y;
49             res += edges[i].w;
50             AddEdge(edges[i].u,edges[i].v,edges[i].w);
51             AddEdge(edges[i].v,edges[i].u,edges[i].w);
52             if(++cnt==n-1) break; 
53         }
54     }
55     return res;
56 }
57 
58 vector<int> nodes;
59 void dfs(int u,int fa,double facost) {
60     for(int i=0;i<nodes.size();i++) {
61         int x=nodes[i];
62         maxcost[x][u]=maxcost[u][x]=max(maxcost[x][fa],facost);
63     }
64     nodes.push_back(u);
65     for(int i=front[u];i>=0;i=e[i].next) {
66         int v=e[i].v;
67         if(v!=fa) dfs(v,u,e[i].w);
68     }
69 }
70 
71 int main() {
72     int T;
73     scanf("%d",&T);
74     while(T--)
75     {
76         en=-1; m=0;
77         memset(front,-1,sizeof(front));
78         
79         scanf("%d",&n);
80         for(int i=1;i<=n;i++) scanf("%d%d%d",&x[i],&y[i],&p[i]);
81         
82         double tot=Kruskal();
83         
84         nodes.clear();
85         memset(maxcost,0,sizeof(maxcost));
86         dfs(1,-1,0);
87         
88         double ans=0;
89         for(int i=1;i<=n;i++)
90            for(int j=i+1;j<=n;j++)
91               ans=max(ans,(double)(p[i]+p[j])/(tot-maxcost[i][j]));
92         printf("%.2lf\n",ans);
93     }
94     return 0;
95 }

 

基于Spring Boot搭建的一个多功能在线学习系统的实现细节。系统分为管理员和用户两个主要模块。管理员负责视频、文件和文章资料的管理以及系统运营维护;用户则可以进行视频播放、资料下载、参与学习论坛并享受个性化学习服务。文中重点探讨了文件下载的安全性和性能优化(如使用Resource对象避免内存溢出),积分排行榜的高效实现(采用Redis Sorted Set结构),敏感词过滤机制(利用DFA算法构建内存过滤树)以及视频播放的浏览器兼容性解决方案(通过FFmpeg调整MOOV原子位置)。此外,还提到了权限管理方面自定义动态加载器的应用,提高了系统的灵活性和易用性。 适合人群:对Spring Boot有一定了解,希望深入理解其实际应用的技术人员,尤其是从事在线教育平台开发的相关从业者。 使用场景及目标:适用于需要快速搭建稳定高效的在线学习平台的企业或团队。目标在于提供一套完整的解决方案,涵盖从资源管理到用户体验优化等多个方面,帮助开发者更好地理解和掌握Spring Boot框架的实际运用技巧。 其他说明:文中不仅提供了具体的代码示例和技术思路,还分享了许多实践经验教训,对于提高项目质量有着重要的指导意义。同时强调了安全性、性能优化等方面的重要性,确保系统能够应对大规模用户的并发访问需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值