HDU 4756 Install Air Conditioning(次小生成树)

题目大意:给你n个点然后让你求出去掉一条边之后所形成的最小生成树。

比较基础的次小生成树吧。。。先prime一遍求出最小生成树,在dfs求出次小生成树。

Install Air Conditioning

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)

Total Submission(s): 1038    Accepted Submission(s): 240

Problem Description

NJUST carries on the tradition of HaJunGong. NJUST, who keeps up the ”people-oriented, harmonious development” of the educational philosophy and develops the ”unity, dedication, truth-seeking, innovation” school motto, has now become an engineering-based,
multidisciplinary university.

As we all know, Nanjing is one of the four hottest cities in China. Students in NJUST find it hard to fall asleep during hot summer every year. They will never, however, suffer from that hot this year, which makes them really excited. NJUST’s 60th birthday
is approaching, in the meantime, 50 million is spent to install air conditioning among students dormitories. Due to NJUST’s long history, the old circuits are not capable to carry heavy load, so it is necessary to set new high-load wires. To reduce cost, every
wire between two dormitory is considered a segment. Now, known about all the location of dormitories and a power plant, and the cost of high-load wire per meter, Tom200 wants to know in advance, under the premise of all dormitories being able to supply electricity,
the minimum cost be spent on high-load wires. And this is the minimum strategy. But Tom200 is informed that there are so many wires between two specific dormitories that we cannot set a new high-load wire between these two, otherwise it may have potential
risks. The problem is that Tom200 doesn’t know exactly which two dormitories until the setting process is started. So according to the minimum strategy described above, how much cost at most you‘ll spend?

Input

The first line of the input contains a single integer T(T ≤ 100), the number of test cases.

For each case, the first line contains two integers n(3 ≤ n ≤ 1000), k(1 ≤ k ≤ 100). n represents n-1 dormitories and one power plant, k represents the cost of high-load wire per meter. n lines followed contains two integers x, y(0 ≤ x, y ≤ 10000000), representing
the location of dormitory or power plant. Assume no two locations are the same, and no three locations are on a straight line. The first one is always the location of the power plant.

Output

For each case, output the cost, correct to two decimal places.

Sample Input

2

4 2
0 0
1 1
2 0
3 1

4 3
0 0
1 1
1 0
0 1

Sample Output

9.66
9.00
#include <set>
#include <map>
#include <queue>
#include <math.h>
#include <vector>
#include <string>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>

#define LL __int64
using namespace std;

const int INF = 0x3f3f3f3f;
const int maxn = 1010;
const int N = 1010;
const int M = 300010;
struct node
{
    int x, y;
} p[maxn];

struct node1
{
    int to, next;
} f[M];

int pre[N], head[N], flag[N];
int vis[N][N];
double low[N];
double dis[N][N], dp[N][N];
int n, m, num;
double sum;

void init()
{
    sum = 0;
    num = 0;
    memset(flag, 0, sizeof(flag));
    memset(vis, 0, sizeof(vis));
    memset(head, -1, sizeof(head));
    memset(dp, 0, sizeof(dp));
}

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

void add(int s, int t)
{
    f[num].to = t;
    f[num].next = head[s];
    head[s] = num ++;
}

void prime()
{
    for(int i = 1; i <= n; i++)
    {
        low[i] = dis[1][i];
        pre[i] = 1;
    }
    flag[1] = 1;
    for(int i = 1; i < n; i++)
    {
        double Min = INF;
        int v;
        for(int j = 1; j <= n; j++)
        {
            if(!flag[j] && Min > low[j])
            {
                v = j;
                Min = low[j];
            }
        }
        sum += Min;
        vis[pre[v]][v] = vis[v][pre[v]] = 1;
        add(v, pre[v]);
        add(pre[v], v);
        flag[v] = 1;
        for(int j = 1; j <= n; j++)
        {
            if(!flag[j] && low[j] > dis[v][j])
            {
                low[j] = dis[v][j];
                pre[j] = v;
            }
        }
    }
}

double dfs(int cur, int u, int fa)  //用cur更新cur点所在的子树和另外子树的最短距离
{
    double ans = INF;
    for(int i = head[u]; ~i; i = f[i].next) //沿着生成树的边遍历
    {
        if(f[i].to == fa)
            continue;
        double tmp = dfs(cur, f[i].to, u);  //用cur更新的以当前边为割边的两个子树最短距离
        ans = min(tmp, ans);        //以(fa,u)为割边的2个子树的最短距离
        dp[u][f[i].to] = dp[f[i].to][u] = min(tmp, dp[u][f[i].to]);
    }
    if(cur != fa)   //生成树边不更新
        ans = min(ans, dis[cur][u]);
    return ans;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        init();
        scanf("%d %d",&n, &m);
        for(int i = 1; i <= n; i++) scanf("%d %d",&p[i].x, &p[i].y);
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= i; j++)
            {
                dp[i][j] = dp[j][i] = INF;
                if(i == j)
                {
                    dis[i][j] = 0.0;
                    continue;
                }
                dis[i][j] = dis[j][i] = Dis(p[i].x, p[i].y, p[j].x, p[j].y);
            }
        }
        prime();
        double ans = sum;
        for(int i = 0; i < n; i++) dfs(i, i, -1);
        for(int i = 2; i <= n; i++)
        {
            for(int j = 2; j < i; j++)
            {
                if(!vis[i][j]) continue;
                ans = max(ans, sum-dis[i][j]+dp[i][j]);
            }
        }
        printf("%.2lf\n",ans*m);
    }
    return 0;
}
时间: 2024-10-05 12:38:48

HDU 4756 Install Air Conditioning(次小生成树)的相关文章

hdu 4126 Genghis Khan the Conqueror hdu 4756 Install Air Conditioning 最小生成树

这两题思路一样.先说下题意. 第一道就是一张图,q个操作,每次将一个边x,y增大到z,求出此时的最小生成树的值w,输出这q个w的平均值. 第二道是一张完全图,但是有一条未知边不能选,求最小生成树最大可能是多少. 对于第一道题,先求出最小生成树,对于每个操作x,y,z,假设x,y不是树边,那么w不变,如果是树边,那么假设这条边连接了u,v两个点集,那么只要添上一条两个点集间所有边的最小的那条即可.但是复杂度为n3,所以为了降低复杂度,要预处理出这条最小边,用dp[ i ][ j ]表示i,j两集合

HDU 4756 Install Air Conditioning

Install Air Conditioning Time Limit: 2000ms Memory Limit: 65535KB This problem will be judged on HDU. Original ID: 475664-bit integer IO format: %I64d      Java class name: Main   NJUST carries on the tradition of HaJunGong. NJUST, who keeps up the ”

HDU 4756 Install Air Conditioning (MST+树形DP)

题意:n-1个宿舍,1个供电站,n个位置每两个位置都有边相连,其中有一条边不能连,求n个位置连通的最小花费的最大值. 析:因为要连通,还要权值最小,所以就是MST了,然后就是改变一条边,然后去找出改变哪条能使得总花费最大,dp[i][j] 表示那条边左边的 i 和右边的 j, 最短距离,然后枚举MST里面的每条边,就能知道哪是最大了,注意 供电站和宿舍之间的边不能考虑的. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000&qu

【树形DP】 HDU 4756 Install Air Conditioning

通道 题意:给n个点,现在要使这n个点连通,并且要求代价最小.现在有2个点之间不能直接连通(除了第一个点),求最小代价 思路:先求mst,然后枚举边,对于生成树上的边替换,用树形dp O(N^2)求出每条生成树边的最小替代边.然后替换后的最大值 代码: #include <cmath> #include <queue> #include <cstdio> #include <cstring> #include <algorithm> using

Install Air Conditioning HDU - 4756(最小生成树+树形dp)

Install Air Conditioning HDU - 4756 题意是要让n-1间宿舍和发电站相连 也就是连通嘛 最小生成树板子一套 但是还有个限制条件 就是其中有两个宿舍是不能连着的 要求所有情况中最大的那个 这是稠密图 用kruskal的时间会大大增加 所以先跑一遍prim 跑完之后对最小生成树里面的边去搜索(树形dp)我觉得dp就是搜索(虽然我菜到切不了dp题.) so dfs的过程我也叫做树形dp咯 dp[i][j]表示i和j不相连后 这两个部分距离最小的边 代码如下 #incl

hdu 1679 The Unique MST 次小生成树 简单题

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21737   Accepted: 7692 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

HDU 4081 Qin Shi Huang&#39;s National Road System(最小生成树/次小生成树)

题目链接:传送门 题意: 有n坐城市,知道每坐城市的坐标和人口.现在要在所有城市之间修路,保证每个城市都能相连,并且保证A/B 最大,所有路径的花费和最小,A是某条路i两端城市人口的和,B表示除路i以外所有路的花费的和(路径i的花费为0). 分析: 先求一棵最小生成树,然后枚举每一条最小生成树上的边,删掉后变成两个生成树,然后找两个集合中点权最大的两 个连接起来.这两个点中必然有权值最大的那个点,所以直接从权值最大的点开始dfs. 为了使A/B的值最大,则A尽可能大,B尽可能小.所以B中的边一定

hdu 4081 Qin Shi Huang&#39;s National Road System 次小生成树 算法

Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4180    Accepted Submission(s): 1450 Problem Description During the Warring States Period of ancient China(4

HDU 4081—— Qin Shi Huang&#39;s National Road System——————【次小生成树、prim】

Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5608    Accepted Submission(s): 1972 Problem Description During the Warring States Period of ancient China(47