hdu 4756 Install Air Conditioning (MST+树形dp)

59 篇文章 0 订阅

Install Air Conditioning

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


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
 

Source
 

ps:和这题基本上是一样的:hdu 4126

题意:
抽象出来后,就是让你先求最小生成树,其中一条宿舍到宿舍的边有危险,要将这条边换掉,问最大的可能花费为多少。

思路:求MST,然后树形dp求去掉一条边后的最佳替换边,详见hdu 4126。

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
//#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 1005
#define MAXN 100005
#define mod 1000000007
#define INF 0x3f3f3f3f
using namespace std;

typedef long long ll;
int n,m,cnt,ni;
double sum,ans;
bool vis[maxn];
int pre[maxn],pp[maxn];
double dist[maxn],city[maxn][maxn],dp[maxn][maxn];
struct Node
{
    int x,y;
}p[maxn],mst[maxn];
struct node
{
    int v,next;
}edge[maxn*2];

void init()
{
    int i,j;
	memset(vis,0,sizeof(vis));
	memset(pp,0,sizeof(pp));
	for(i=1;i<=n;i++)
    {
        dist[i]=INF;
        for(j=i+1;j<=n;j++)
        {
            dp[i][j]=dp[j][i]=INF;
        }
    }
}
double caldist(int k1,int k2)
{
    double x,y;
    x=p[k1].x-p[k2].x;
    y=p[k1].y-p[k2].y;
    return sqrt(x*x+y*y);
}
void presolve()
{
    int i,j;
    for(i=1;i<=n;i++)
    {
        for(j=i+1;j<=n;j++)
        {
            city[i][j]=city[j][i]=caldist(i,j);
        }
    }
}
void addedge(int u,int v)
{
    cnt++;
    edge[cnt].v=v;
    edge[cnt].next=pp[u];
    pp[u]=cnt;
}
void prim()
{
	int i,j,k,now=1;
	double mi;
	sum=0;
	vis[1]=1;
	dist[1]=0;
	for(i=1;i<n;i++)
	{
		for(j=1;j<=n;j++)
		{
			if(!vis[j]&&city[now][j]<dist[j])
                pre[j]=now,dist[j]=city[now][j];
		}
		mi=INF;
		for(j=1;j<=n;j++)
		{
			if(!vis[j]&&dist[j]<mi)
			{
				mi=dist[j];
				k=j;
			}
		}
		addedge(k,pre[k]);
		addedge(pre[k],k);
		mst[i].x=k;
		mst[i].y=pre[k];
		sum+=dist[k];
		now=k;
		vis[k]=1;
	}
}
double dfs(int u,int pre)
{
    int i,j,v;
    double t,best=INF;
    for(i=pp[u];i;i=edge[i].next)
    {
        v=edge[i].v;
        if(v!=pre)
        {
            t=dfs(v,u);
            best=min(best,t);
            dp[u][v]=min(dp[u][v],t);
        }
    }
    if(pre!=ni) best=min(best,city[ni][u]);
    return best;
}
void solve()
{
    int i,j;
    for(i=1;i<=n;i++)
    {
        ni=i;
        dfs(i,i);
    }
}
int main()
{
    int i,j,t,u,v;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        init();
        for(i=1;i<=n;i++)
        {
            scanf("%d%d",&p[i].x,&p[i].y);
        }
        cnt=0;
        presolve();
        prim();
        solve();
        ans=sum;
        for(i=1;i<n;i++)
        {
            u=mst[i].x;
            v=mst[i].y;
            if(u==1||v==1) continue ;
            ans=max(ans,sum-city[u][v]+dp[u][v]);
        }
        printf("%.2f\n",ans*m);
    }
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值