hdu 4756 Install Air Conditioning(次小生成树变形+树dp)
来源:互联网 发布:淘宝客服实名认证 编辑:程序博客网 时间:2024/05/01 01:54
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.
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题意:有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;}
- hdu 4756 Install Air Conditioning(次小生成树变形+树dp)
- HDU 4756 Install Air Conditioning(次小生成树)
- poj 4756 Install Air Conditioning 生成树变形
- HDU 4756:Install Air Conditioning (最小生成树最佳替换边,最小生成树+树形dp)
- hdu4576 Install Air Conditioning(最小生成树+树型dp)
- HDU - 4756 Install Air Conditioning(树形DP + 最小生成树)
- hdu 4756 Install Air Conditioning
- hdu 4756 Install Air Conditioning
- MST(prim)+树形dp-hdu-4756-Install Air Conditioning
- hdu 4756 Install Air Conditioning (MST+树形dp)
- HDU 4756 Install Air Conditioning(MST + 树形DP)
- HDU 4756 Install Air Conditioning(树形dp+MST)
- Install Air Conditioning HDU
- hdu 4756 Install Air Conditioning(2013南京网络赛)
- hdu 4756 Install Air Conditioning(MST+全边次小树,4级)
- HDU 4756 Install Air Conditioning 块与块,非树边最短路
- hdu4756 Install Air Conditioning(MST + 树形DP)
- HDU 4081 次小生成树变形记
- POJ 2389大数乘法
- get与post
- delphi向SQL Server2005中存取图片
- Xcode 5的新特性
- ubuntu 搜索文件方法(find命令)
- hdu 4756 Install Air Conditioning(次小生成树变形+树dp)
- request中各个地址的区别
- 电子工程师名片——USB设备枚举过程
- PHP获取当前时间、时间戳的各种格式写法汇总[日期时间]
- Android的设置界面及Preference使用
- java 基础问题的一些处理,记录一下,持续更新呗
- 数学手册. 完美 书签 文字版.pdf
- linux下批量替换文件内容的三种办法
- Spring ContextLoaderListener源码分析 .