hdu 4756 Install Air Conditioning(次小生成树变形+树dp)

本文探讨了南京大学在炎炎夏日为学生宿舍安装空调的策略与成本优化问题,通过构建最小生成树并进行变形操作,以求在特定条件下达到最低成本。详细介绍了算法思路、输入输出规范以及实现代码,旨在解决复杂网络环境下资源分配的优化问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Install Air Conditioning

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 294    Accepted Submission(s): 52


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
题意:有1个发电厂和n-1个宿舍,现在要使所有宿舍都有电,需要建造一些线路,并且要求建造线路的总花费尽量小。但是,有两个宿舍(具体哪两个宿舍是不知道的)之间不能建造线路,求最坏情况下的花费。
思路:次小生成树变形。某两个宿舍之间不能建造线路,则先求最小生成树,在最小生成树上删去某一条边,这时就会分成两个连通块,然后要连一条除删去的边外的两连通块之间的最短边使之变成一棵新的树。而这条最短边可以通过树dp求出。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <map>
#include <cstdlib>
using namespace std;

const int maxn=1005;
const double INF=1000000000;
struct node
{
    double x,y;
}p[maxn];
struct node1
{
    int u,v,next;
}edge[maxn*maxn];
double G[maxn][maxn],dis[maxn],dp[maxn][maxn];
double mst;
bool vis[maxn];
int pre[maxn],head[maxn];
int n,k,num;
void init()
{
    memset(head,-1,sizeof(head));
    memset(vis,false,sizeof(vis));
    num=mst=0;
}
void add(int u,int v)
{
    edge[num].u=u;
    edge[num].v=v;
    edge[num].next=head[u];
    head[u]=num++;
}
double get_dis(node a,node b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
void prim()
{
    double m;
    int x;
    init();
    for(int i=0;i<n;i++)
    {
        pre[i]=0;
        dis[i]=G[0][i];
    }
    vis[0]=true;
    for(int i=1;i<n;i++)
    {
        m=INF;
        for(int j=0;j<n;j++)
        if(!vis[j]&&dis[j]<m) m=dis[x=j];
        if(m==INF) break;
        vis[x]=true;
        mst+=m;
        add(pre[x],x);
        add(x,pre[x]);
        for(int j=0;j<n;j++)
        if(!vis[j]&&dis[j]>G[x][j])
        {
            dis[j]=G[x][j];
            pre[j]=x;
        }
    }
}
double dfs(int rt,int u,int pre)
{
    double ret=INF;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(v==pre) continue;
        double tmp=dfs(rt,v,u);
        ret=min(ret,tmp);
        dp[u][v]=dp[v][u]=min(dp[u][v],tmp);
    }
    if(pre!=rt)
    ret=min(ret,G[rt][u]);
    return ret;
}
double dfs_ans(int u,int pre)
{
    double ret=0;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(v==pre) continue;
        ret=max(ret,mst-G[u][v]+dp[u][v]);
        ret=max(ret,dfs_ans(v,u));
    }
    return ret;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&k);
        for(int i=0;i<n;i++)
        scanf("%lf%lf",&p[i].x,&p[i].y);
        for(int i=0;i<n;i++)
        {
            G[i][i]=0;
            dp[i][i]=INF;
            for(int j=i+1;j<n;j++)
            {
                G[i][j]=G[j][i]=get_dis(p[i],p[j]);
                dp[i][j]=dp[j][i]=INF;
            }
        }
        prim();
        for(int i=0;i<n;i++)
        dfs(i,i,-1);
        double ans=mst;
        for(int i=head[0];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v;
            ans=max(ans,dfs_ans(v,0));
        }
        printf("%.2lf\n",ans*k);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值