hdu4756 Install Air Conditioning

20 篇文章 0 订阅
4 篇文章 0 订阅

次小生成树的变形

dfs得到最佳替代边

假设两个各自连通的部分分别为树A,树B

用dp[i][j]表示树A(i点所在的树) 到 树B(j点所在的树)的最近距离(此然就是最佳替代边)

注意0号节点相连的边不会出错

//#pragma comment(linker, "/STACK:102400000,102400000")
//HEAD
#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <string>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
//LOOP
#define FF(i, a, b) for(int i = (a); i < (b); ++i)
#define FD(i, b, a) for(int i = (b) - 1; i >= (a); --i)
#define FE(i, a, b) for(int i = (a); i <= (b); ++i)
#define FED(i, b, a) for(int i = (b); i>= (a); --i)
#define REP(i, N) for(int i = 0; i < (N); ++i)
#define CLR(A,value) memset(A,value,sizeof(A))
#define CPY(a, b) memcpy(a, b, sizeof(a))
#define FC(it, c) for(__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
//STL
#define SZ(V) (int)V.size()
#define PB push_back
#define EQ(a, b) (fabs((a) - (b)) <= 1e-10)
#define ALL(c) (c).begin(), (c).end()
//INPUT
#define RI(n) scanf("%d", &n)
#define RII(n, m) scanf("%d%d", &n, &m)
#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define RIV(n, m, k, p) scanf("%d%d%d%d", &n, &m, &k, &p)
#define RV(n, m, k, p, q) scanf("%d%d%d%d%d", &n, &m, &k, &p, &q)
#define RS(s) scanf("%s", s)
//OUTPUT
#define WI(n) printf("%d\n", n)
#define WS(s) printf("%s\n", s)
typedef long long LL;
typedef unsigned long long ULL;
typedef vector <int> VI;
const int INF = 100000000;
const double eps = 1e-10;
const int maxn = 1210;
const LL MOD = 1e9 + 7;

int k, n;
struct Node{
    int x, y;
}node[maxn];

double dis[maxn][maxn], dp[maxn][maxn], mst;
bool used[maxn][maxn];
VI G[maxn];

double Dis(int x1, int y1, int x2, int y2)
{
    return sqrt(1.0 * (x1 - x2) * (x1 - x2) + 1.0 * (y1 - y2) * (y1 - y2));
}

void prim()
{
    CLR(used, 0);
    double near[maxn], mi;
    bool vis[maxn] = {0};
    REP(i, n)
        near[i] = dis[0][i];
    vis[0] = 1, mst = 0;
    int pos, fa[maxn] = {0};
    REP(i, n - 1)
    {
        mi = INF;
        REP(j, n)
            if (!vis[j] && near[j] < mi)
            {
                mi = near[j];
                pos = j;
            }
        mst += mi, vis[pos] = 1;
        used[fa[pos]][pos] = used[pos][fa[pos]] = 1;
        G[fa[pos]].PB(pos), G[pos].PB(fa[pos]);
        REP(j, n)
            if (!vis[j] && dis[pos][j] < near[j])
                near[j] = dis[pos][j], fa[j] = pos;
    }
}

double dfs(int u, int fa, int rt)
{
    double ans = INF;
    REP(i, SZ(G[u]))
    {
        int v  = G[u][i];
        if (v != fa)
        {
            double t = dfs(v, u, rt);
            ans = min(ans, t);
            dp[u][v] = dp[v][u] = min(dp[u][v], t);
        }
    }
    if (fa != rt)   ans = min(ans, dis[rt][u]);
    return ans;
}

void solve()
{
    REP(i, n)
    {
        FF(j, i, n)
            dis[i][j] = dis[j][i] = Dis(node[i].x, node[i].y, node[j].x, node[j].y);
        REP(j, n)
            dp[i][j] = INF;
        G[i].clear();
    }
    prim();
    REP(i, n)
        dfs(i, -1, i);
    double ans = mst;
    FF(i, 1, n)
    {
        REP(j, SZ(G[i]))
        {
            int v = G[i][j];
            if (v == 0) continue;
            ans = max(mst - dis[i][v] + dp[i][v], ans);
        }
    }
    printf("%.2lf\n", ans * k);
}

int main()
{
    int T;
    RI(T);
    while (T--)
    {
        RII(n, k);
        REP(i, n)
            RII(node[i].x, node[i].y);
        solve();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值