标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4139 Accepted Submission(s): 1440
求完最小生成树之后 , 记录树上的n-1条边。
然后删去最小生成树上的边。
再dfs出两棵分割开来的子树中的population最大的两个点,把它们连起来作为免费边再取答案。
#include <iostream> #include <algorithm> #include <cstdio> #include <cmath> #include <cstring> #include <vector> using namespace std ; typedef long long LL ; const int N = 1010; const int M = 1000010; struct edge { int u , v , p ; double w ; bool operator < ( const edge &a ) const { if( w != a.w )return w < a.w ; else return p > a.p ; } }e[M]; int n , m , fa[N] ; bool vis[M] ; double x[N] , y[N] ; int p[N] , tot ; int find( int k ) { return k == fa[k] ? k : find(fa[k]); } double mst() { double ans = 0 ; sort( e , e + tot ); memset( vis , false , sizeof vis ) ; for( int i = 0 ; i <= n ; ++i ) fa[i] = i ; for( int i = 0 ; i < tot ; ++i ) { int fu = find( e[i].u ) , fv = find( e[i].v ); if( fu == fv ) continue ; fa[fv] = fu; ans += e[i].w ; vis[i] = true ; } return ans ; } vector<int>g[N]; int dfs( int u , int fa ) { int ans = p[u] ; for( int i = 0 ; i < g[u].size() ; ++i ) { int v = g[u][i] ; if( v == fa ) continue ; ans = max( ans , dfs( v , u ) ); } return ans ; } void Work() { double cost = mst() , ans = 0; for( int i = 0 ; i <= n ; ++i ) g[i].clear(); for( int i = 0 ; i < tot ; ++i ) if( vis[i] ){ g[e[i].u].push_back(e[i].v); g[e[i].v].push_back(e[i].u); } for( int i = 0 ; i < tot ; ++i ) if( vis[i] ) { int a = dfs( e[i].u , e[i].v ) , b = dfs( e[i].v , e[i].u ) ; ans = max( ans , ( 1.0*(a+b)/(cost-e[i].w) ) ); } printf("%.2lf\n",ans); } int main () { //freopen("in.txt","r",stdin); int _ ; scanf("%d",&_); while( _-- ) { scanf("%d",&n); for( int i = 0 ; i < n ; ++i ) { scanf("%lf%lf%d",&x[i],&y[i],&p[i]); } tot = 0 ; for( int i = 0 ; i < n ; ++i ) { for( int j = i + 1 ; j < n ; ++j ) { e[tot].u = i , e[tot].v = j ; e[tot].p = p[i] + p[j] ; e[tot].w = sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])); tot++; } } Work(); } }
hdu 4081 Qin Shi Huang's National Road System(次小生成树)
标签:
原文地址:http://www.cnblogs.com/hlmark/p/4291461.html