uvalive5713(次小生成树)

先求出最小生成树,然后n^2枚举两个点,在两个点之间连一条边,就形成了一个环,然后求出两点间人数的和除以那个环在树上的边的最大边,更新最大值.求最大权值边可以用熟练剖分.复杂度n^2logn.

#include <cstdio>
#include <cstring>
#include <vector>
#include<cmath>
#include <algorithm>
using namespace std;
#define Del(a,b) memset(a,b,sizeof(a))
const int N = 10005;
const int inf = 0x3f3f3f3f;
const int maxn = 1005;
int c[maxn];
double x[maxn],y[maxn];
double bmap[maxn][maxn];
int vis[maxn];
int pre[maxn];
double lowc[maxn];
int dep[N],siz[N],fa[N],id[N],son[N],top[N];
double val[maxn];
int num;
vector<int> v[N];
double res = -inf*1.0;
struct tree
{
    int x,y;
    double val;
    void read(int a,int b,double c){
        x = a;
        y = b;
        val = c;
    }
};
tree e[N];
inline double cla(int a,int b)
{
    return sqrt((x[a]-x[b])*(x[a]-x[b])+(y[a]-y[b])*(y[a]-y[b]));
}
inline double prim(double cost[maxn][maxn],int n)
{
    double ans = 0;
    for(int i=0;i<n;i++) pre[i] = 0;
    memset(vis,0,sizeof(vis));
    vis[0] = true;
    for(int i=1;i<n;i++) lowc[i] = cost[0][i];
    for(int i=1;i<n;i++)
    {
        double  minc = inf*1.0;
        int p = -1;
        for(int j=0;j<n;j++)
            if(!vis[j]&&minc>lowc[j])
            {
                minc = lowc[j];
                p = j;
            }
        if(minc==inf*1.0) return -1;
        ans += minc;
        e[i].read(pre[p]+1,p+1,minc);
        v[pre[p]+1].push_back(p+1);
        v[p+1].push_back(pre[p]+1);
        vis[p] = true;
        for(int j=0;j<n;j++)
        {
            if(!vis[j]&&lowc[j]>cost[p][j]){
                lowc[j] = cost[p][j];
                pre[j] = p;
            }
        }
    }
    return ans;
}
void dfs1(int u, int f, int d) {
    dep[u] = d;
    siz[u] = 1;
    son[u] = 0;
    fa[u] = f;
    for (int i = 0; i < v[u].size(); i++) {
        int ff = v[u][i];
        if (ff == f) continue;
        dfs1(ff, u, d + 1);
        siz[u] += siz[ff];
        if (siz[son[u]] < siz[ff])
            son[u] = ff;
    }
}
void dfs2(int u, int tp) {
    top[u] = tp;
    id[u] = ++num;
    if (son[u]) dfs2(son[u], tp);
    for (int i = 0; i < v[u].size(); i++) {
        int ff = v[u][i];
        if (ff == fa[u] || ff == son[u]) continue;
        dfs2(ff, ff);
    }
}
#define lson(x) ((x<<1))
#define rson(x) ((x<<1)+1)
struct Tree
{
    int l,r;
    double val;
};
Tree tree[4*N];
void pushup(int x) {
    tree[x].val = max(tree[lson(x)].val, tree[rson(x)].val);
}
void build(int l,int r,int v)
{
    tree[v].l=l;
    tree[v].r=r;
    if(l==r)
    {
        tree[v].val = val[l];
        return ;
    }
    int mid=(l+r)>>1;
    build(l,mid,v*2);
    build(mid+1,r,v*2+1);
    pushup(v);
}
void update(int o,int v,int val)  //log(n)
{
    if(tree[o].l==tree[o].r)
    {
        tree[o].val = val;
        return ;
    }
    int mid = (tree[o].l+tree[o].r)/2;
    if(v<=mid)
        update(o*2,v,val);
    else
        update(o*2+1,v,val);
    pushup(o);
}
double query(int x,int l, int r)
{
    if (tree[x].l >= l && tree[x].r <= r) {
        return tree[x].val;
    }
    int mid = (tree[x].l + tree[x].r) / 2;
    double ans = 0;
    if (l <= mid) ans = max(ans, query(lson(x),l,r));
    if (r > mid) ans = max(ans, query(rson(x),l,r));
    return ans;
}
double Yougth(int u, int v) {
    int tp1 = top[u], tp2 = top[v];
    double ans = 0;
    while (tp1 != tp2) {
        //printf("YES\n");
        if (dep[tp1] < dep[tp2]) {
            swap(tp1, tp2);
            swap(u, v);
        }
        ans = max(query(1,id[tp1], id[u]), ans);
        u = fa[tp1];
        tp1 = top[u];
    }
    if (u == v) return ans;
    if (dep[u] > dep[v]) swap(u, v);
    ans = max(query(1,id[son[u]], id[v]), ans);
    return ans;
}
void Clear(int n)
{
    for(int i=1;i<=n;i++)
        v[i].clear();
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        scanf("%lf%lf%d",&x[i],&y[i],&c[i]);
        for(int i=0;i<n;i++)
            for(int j=i+1;j<n;j++)
                bmap[i][j] = bmap[j][i] = cla(i,j);
        double l = prim(bmap,n); res = -inf*1.0;
        num = 0;
        dfs1(1,0,1);
        dfs2(1,1);
        for (int i = 1; i < n; i++) {
            if (dep[e[i].x] < dep[e[i].y]) swap(e[i].x, e[i].y);
            val[id[e[i].x]] = e[i].val;
        }
        build(1,num,1);
        for(int i=0;i<n;i++)
        {
            for(int j=i+1;j<n;j++)
            {
                int sum = c[i]+c[j];
                res = max(res,sum/(l-Yougth(i+1,j+1)));
            }
        }
        printf("%.2f\n",res);
        Clear(n);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值