layout: post

title: 训练指南 UVALive - 5713(最小生成树 + 次小生成树)

author: "luowentaoaa"

catalog: true

mathjax: true

tags:

- 最小生成树

- 图论

- 训练指南


Qin Shi Huang's National Road System

UVALive - 5713

题意

有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(最小生成树 + 次小生成树)的更多相关文章

  1. 训练指南 UVALive - 3126(DAG最小路径覆盖)

    layout: post title: 训练指南 UVALive - 3126(DAG最小路径覆盖) author: "luowentaoaa" catalog: true mat ...

  2. 训练指南 UVALive - 3415(最大点独立集)

    layout: post title: 训练指南 UVALive - 3415(最大点独立集) author: "luowentaoaa" catalog: true mathja ...

  3. 训练指南 UVALive - 3989(稳定婚姻问题)

    ayout: post title: 训练指南 UVALive - 3989(稳定婚姻问题) author: "luowentaoaa" catalog: true mathjax ...

  4. 训练指南 UVALive - 4043(二分图匹配 + KM算法)

    layout: post title: 训练指南 UVALive - 4043(二分图匹配 + KM算法) author: "luowentaoaa" catalog: true ...

  5. 训练指南 UVALive - 4080(最短路Dijkstra + 边修改 + 最短路树)

    layout: post title: 训练指南 UVALive - 4080(最短路Dijkstra + 边修改 + 最短路树) author: "luowentaoaa" ca ...

  6. 训练指南 UVALive - 3713 (2-SAT)

    layout: post title: 训练指南 UVALive - 3713 (2-SAT) author: "luowentaoaa" catalog: true mathja ...

  7. 训练指南 UVALive - 4287 (强连通分量+缩点)

    layout: post title: 训练指南 UVALive - 4287 (强连通分量+缩点) author: "luowentaoaa" catalog: true mat ...

  8. 训练指南 UVALive - 5135 (双连通分量)

    layout: post title: 训练指南 UVALive - 5135 (双连通分量) author: "luowentaoaa" catalog: true mathja ...

  9. 训练指南 UVALive - 3523 (双联通分量 + 二分图染色)

    layout: post title: 训练指南 UVALive - 3523 (双联通分量 + 二分图染色) author: "luowentaoaa" catalog: tru ...

随机推荐

  1. Codeforces Round #531 (Div. 3) ABCDEF题解

    Codeforces Round #531 (Div. 3) 题目总链接:https://codeforces.com/contest/1102 A. Integer Sequence Dividin ...

  2. 2015年网易校招Java开发工程师(技术架构)在线笔试题

    1.  程序和进程的本质区别是? A.在外存和内存存储 B.非顺序和顺序执行机器指令 C.独占使用和分时使用计算机资源 D.静态和动态特征 参考答案分析: 进程与应用程序的区别: 进程(Process ...

  3. AWS CLI command example

    1.list ec2 instance-id, instance status, type, ip address, name aws ec2 describe-instances --query ' ...

  4. node安装

    由于Node.js平台是在后端运行JavaScript代码,所以,必须首先在本机安装Node环境. 安装Node.js 目前Node.js的最新版本是7.6.x.首先,从Node.js官网下载对应平台 ...

  5. html 表格获取单行

    参考:http://www.jb51.net/article/63161.htm function cell(btn_id) { {#var x=document.getElementById('#' ...

  6. python3 多态,绑定方法与非绑定方法

    多态:同一种事物的不同形态(一个抽象类有多个子类,因而多态的概念依赖于继承) 1. 序列类型有多种形态:字符串,列表,元组. 2. 动物有多种形态:人,狗,猪 多态性:多态性是指具有不同功能的函数可以 ...

  7. Linux内核官方文档atomic_ops.txt【摘自Linux 内核文档】

    摘自Linux内核文档 Documentation/atomic_ops.txt,不是本人原创 Semantics and Behavior of Atomic and Bitmask Operati ...

  8. 如何让footer一直在网页底部(不使用绝对定位并且网页不论长度多长)

    html: <html> <head> <link rel="stylesheet" href="layout.css" ... ...

  9. MYSQL数据库的数据完整性

    #转载请联系 数据库中存储的数据应该符合我们的预期, 这就是数据完整性. 那么如何实现数据完整性? 我们通过以下两方面来实现数据的完整性: 数据类型: 存储在数据库中的所有数据值均正确的状态.如果数据 ...

  10. 精确解释Unicode

    来自:http://blog.csdn.net/gqqnb/article/details/6266542 ---------------------------------------------- ...