训练指南 UVALive - 5713(最小生成树 + 次小生成树)
layout: post
title: 训练指南 UVALive - 5713(最小生成树 + 次小生成树)
author: "luowentaoaa"
catalog: true
mathjax: true
tags:
- 最小生成树
- 图论
- 训练指南
Qin Shi Huang's National Road System
题意
有n个城市,要修一些路使得任意两个城市都能连通。但是有人答应可以不计成本的帮你修一条路,为了使成本最低,你要慎重选择修哪一条路。假设其余的道路长度为B,那条别人帮忙修的道路两端城市中的总人口为B,要找一个使A/B最大的方案。
题意
先求最小生成树,处理出MST中任意两点之间的最长边。因为别人只答应给修一条路,所以枚举这条路,用这条路替换下一条MST中最长的边(在加入这条路后构成的环中),比较求得A/B的最大值。实际上是把求次小生成树的一些后续操作改改。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=998244353;
const int maxn=1e3+50;
const ll inf=0x3f3f3f3f3f3f3f3fLL;
int n,m,x[maxn],y[maxn],p[maxn];
int pa[maxn];
int findset(int x){return pa[x]!=x?pa[x]=findset(pa[x]):x;}
vector<int>G[maxn];
vector<double>C[maxn];
struct Edge{
int x,y;
double d;
bool operator < (const Edge& rhs)const{
return d<rhs.d;
}
};
Edge e[maxn*maxn];
double maxcost[maxn][maxn];
vector<int>nodes;
void dfs(int u,int fa,double facost){
//cout<<"dfs="<<u<<endl;
for(int i=0;i<nodes.size();i++){
int x=nodes[i];
maxcost[u][x]=maxcost[x][u]=max(maxcost[x][fa],facost);
}
nodes.push_back(u);
for(int i=0;i<G[u].size();i++){
if(G[u][i]==fa)continue;
dfs(G[u][i],u,C[u][i]);
}
}
double dis(int i,int j){
return sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
}
double MST(){
m=0;
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
e[m++]=(Edge){i,j,dis(i,j)};
sort(e,e+m);
for(int i=0;i<n;i++){pa[i]=i;G[i].clear();C[i].clear();}
int cnt=0;
double ans=0;
for(int i=0;i<m;i++){
int x=e[i].x,y=e[i].y,u=findset(x),v=findset(y);
double d=e[i].d;
if(u==v)continue;
pa[u]=v;
G[x].push_back(y);G[y].push_back(x);
C[x].push_back(d);C[y].push_back(d);
ans+=d;
if(++cnt==n-1)break;
}
return ans;
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
int t;
cin>>t;
while(t--){
cin>>n;
for(int i=0;i<n;i++)cin>>x[i]>>y[i]>>p[i];
double tot=MST();
memset(maxcost,0,sizeof(maxcost));
nodes.clear();
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]+p[j])/(tot-maxcost[i][j]));
}
cout<<fixed<<setprecision(2)<<ans<<endl;
}
return 0;
}
训练指南 UVALive - 5713(最小生成树 + 次小生成树)的更多相关文章
- 训练指南 UVALive - 3126(DAG最小路径覆盖)
layout: post title: 训练指南 UVALive - 3126(DAG最小路径覆盖) author: "luowentaoaa" catalog: true mat ...
- 训练指南 UVALive - 3415(最大点独立集)
layout: post title: 训练指南 UVALive - 3415(最大点独立集) author: "luowentaoaa" catalog: true mathja ...
- 训练指南 UVALive - 3989(稳定婚姻问题)
ayout: post title: 训练指南 UVALive - 3989(稳定婚姻问题) author: "luowentaoaa" catalog: true mathjax ...
- 训练指南 UVALive - 4043(二分图匹配 + KM算法)
layout: post title: 训练指南 UVALive - 4043(二分图匹配 + KM算法) author: "luowentaoaa" catalog: true ...
- 训练指南 UVALive - 4080(最短路Dijkstra + 边修改 + 最短路树)
layout: post title: 训练指南 UVALive - 4080(最短路Dijkstra + 边修改 + 最短路树) author: "luowentaoaa" ca ...
- 训练指南 UVALive - 3713 (2-SAT)
layout: post title: 训练指南 UVALive - 3713 (2-SAT) author: "luowentaoaa" catalog: true mathja ...
- 训练指南 UVALive - 4287 (强连通分量+缩点)
layout: post title: 训练指南 UVALive - 4287 (强连通分量+缩点) author: "luowentaoaa" catalog: true mat ...
- 训练指南 UVALive - 5135 (双连通分量)
layout: post title: 训练指南 UVALive - 5135 (双连通分量) author: "luowentaoaa" catalog: true mathja ...
- 训练指南 UVALive - 3523 (双联通分量 + 二分图染色)
layout: post title: 训练指南 UVALive - 3523 (双联通分量 + 二分图染色) author: "luowentaoaa" catalog: tru ...
随机推荐
- 【题解】NOI2014动物园
传送门:洛谷P2375 一直到写到这道题目才发现我一直都理解了假的KMP……fail数组:fail[i]为从1-i(包含i)在内的字符串,相同的最长前后缀长度. 那么我们可以先思考暴力:先求出所有的f ...
- [洛谷P2986][USACO10MAR]伟大的奶牛聚集Great Cow Gat…
题目大意:给你一棵树,每个点有点权,边有边权,求一个点,使得其他所有点到这个点的距离和最短,输出这个距离 题解:树形$DP$,思路清晰,转移显然 卡点:无 C++ Code: #include < ...
- [洛谷P4015]运输问题
题目大意:有m个仓库和n个商店.第i个仓库有 $a_{i}$ 货物,第j个商店需要$b_{j}$个货物.从第i个仓库运送每单位货物到第j个商店的费用为$c_{i,j}$.求出最小费用和最大费用 题 ...
- Java代码管理工具SVN系列
SVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS.CVS,它采用了分支管理系统,它的设计目标就是取代CVS.互联网上很多版本控制服务已从CVS迁移到Subversion ...
- 【TMD模拟赛】上低音号 链表
这道题一看有两个出发现点,一枚举点去找边界,想了一会就Pass了...,二是枚举相框,我们最起码枚举两个边界,然后发现平行边界更好处理,然而仍然只有30分,这个时候就来到了链表的神奇应用,我们枚举上界 ...
- bzoj4145 AMPPZ2014 The Prices 状压dp
这个题.......很可以,很小清晰......反正正经的东西我都没想到:重点在于——————我不会处理那个多出来的路费所以当时我就骚骚的弄了一颗树包状压其实这是一个类01背包的状压在每个状态用01背 ...
- JavaScript的大括号的语义
Javascript中大括号"{}"有四种语义作用: 语义1. 组织复合语句,这是最常见的: view source print? 1 if( condition ) { 2 ...
- 存储过程-----DECLARE---实用注意事项
定义与使用变量时需要注意以下几点: ◆ DECLARE语句必须用在DEGIN…END语句块中,并且必须出现在DEGIN…END语句块的最前面,即出现在其他语句之前. ◆ DECLARE定义的变量的作用 ...
- css3 新旧伸缩盒的异同
由于不需要理会IE浏览器,伸缩盒(flexible box)移动端开发中非常好用! 工作中使用APICLOUD开发手机App,老板要求兼容到安卓2.3(新版的需要安卓4.4以上),所以一直使用的是旧版 ...
- BigDecimal与Long、int之间的互换
在实际开发过程中BigDecimal是一个经常用到的数据类型,它和int Long之间可以相互转换. 转换关系如下代码展示: int 转换成 BigDecimal 数据类型 //int 转换成 big ...