HDU 4756 次小生成树裸题

来源:互联网 发布:php编译安装pdo mysql 编辑:程序博客网 时间:2024/04/27 02:26

Install Air Conditioning

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


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
24 20 01 12 03 14 30 01 11 00 1
 

Sample Output
9.669.00 南京网赛时的题目,看出来是次小生成树,不过数据规模是1000,显然只能n^2的做法,自己不会,网上也没搜到,只好没能做。 次小生成树的解法: 首先对原图求一次最小生成树,把最小生成树上的边标记出来,然后树形dp枚举去掉最小生成树上的每一条边后的最小生成树,然后扫一遍就是答案。代码:
#include <cstdio>#include <cstring>#include <cstdlib>#include <ctime>#include <climits>#include <cmath>#include <cassert>#include <iostream>#include <string>#include <vector>#include <set>#include <map>#include <list>#include <queue>#include <stack>#include <deque>#include <algorithm>using namespace std;typedef long long ll;const double eps=1e-8;const int maxn=1010;struct point{       double x,y;       void input()       {              scanf("%lf%lf",&x,&y);       }}pp[maxn];double dist(point a,point b){       double sss=a.x-b.x;       double ttt=a.y-b.y;       return sqrt(sss*sss+ttt*ttt);}const double inf=1000000000;double dis[maxn][maxn],dp[maxn][maxn],lowc[maxn];int head[maxn],tol,vis[maxn],mp[maxn][maxn],pre[maxn];struct node{       int next,to;}edge[maxn*maxn];void add(int u,int v){       edge[tol].to=v;       edge[tol].next=head[u];       head[u]=tol++;}double prim(int n){       memset(vis,0,sizeof(vis));       memset(mp,0,sizeof(mp));       memset(pre,0,sizeof(pre));       double ans=0;       vis[0]=1;pre[0]=-1;lowc[0]=0;       for(int i=1;i<n;i++)lowc[i]=dis[0][i];       for(int i=1;i<n;i++)       {              double minc=inf;              int p=-1;              for(int j=0;j<n;j++)              if(!vis[j]&&minc>lowc[j])              {                     minc=lowc[j];                     p=j;              }              ans+=minc;              vis[p]=1;              mp[pre[p]][p]=mp[p][pre[p]]=1;              add(pre[p],p);add(p,pre[p]);              for(int j=0;j<n;j++)              if(!vis[j]&&lowc[j]>dis[p][j])              {                     lowc[j]=dis[p][j];                     pre[j]=p;              }       }       return ans;}double dfs(int cur,int u,int fa){       double res=inf;       for(int i=head[u];i!=-1;i=edge[i].next)       {              int v=edge[i].to;              if(v==fa)continue;              double tmp=dfs(cur,v,u);              dp[u][v]=dp[v][u]=min(dp[u][v],tmp);              res=min(res,tmp);       }       if(fa!=cur)res=min(res,dis[cur][u]);       return res;}int main(){       int T,i,j,k,m,n;       scanf("%d",&T);       while(T--)       {              scanf("%d%d",&n,&k);              memset(head,-1,sizeof(head));tol=0;              for(i=0;i<n;i++)pp[i].input();              for(i=0;i<n;i++)              {                     dis[i][i]=0;                     for(j=i+1;j<n;j++)                            dis[i][j]=dis[j][i]=dist(pp[i],pp[j]);              }              double ANS=prim(n);              //cout<<ANS<<endl;              for(i=0;i<n;i++)                     for(j=0;j<n;j++)                            dp[i][j]=inf;                     for(i=0;i<n;i++)dfs(i,i,-1);                     //cout<<tol<<endl;              double ans=ANS;              for(i=1;i<n;i++)                     for(j=i+1;j<n;j++)                     if(mp[i][j])                     ans=max(ans,ANS-dis[i][j]+dp[i][j]);                     printf("%.2lf\n",k*ans);       }       return 0;}


原创粉丝点击