poj-2728Desert King(最优比率生成树)
题目连接:
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 23729 | Accepted: 6631 |
Description
David the Great has just become the king of a desert country. To win the respect of his people, he decided to build channels all over his country to bring water to every village. Villages which are connected to his capital village will be watered. As the dominate ruler and the symbol of wisdom in the country, he needs to build the channels in a most elegant way.
After days of study, he finally figured his plan out. He wanted the average cost of each mile of the channels to be minimized. In other words, the ratio of the overall cost of the channels to the total length must be minimized. He just needs to build the necessary channels to bring water to all the villages, which means there will be only one way to connect each village to the capital.
His engineers surveyed the country and recorded the position and altitude of each village. All the channels must go straight between two villages and be built horizontally. Since every two villages are at different altitudes, they concluded that each channel between two villages needed a vertical water lifter, which can lift water up or let water flow down. The length of the channel is the horizontal distance between the two villages. The cost of the channel is the height of the lifter. You should notice that each village is at a different altitude, and different channels can't share a lifter. Channels can intersect safely and no three villages are on the same line.
As King David's prime scientist and programmer, you are asked to find out the best solution to build the channels.
After days of study, he finally figured his plan out. He wanted the average cost of each mile of the channels to be minimized. In other words, the ratio of the overall cost of the channels to the total length must be minimized. He just needs to build the necessary channels to bring water to all the villages, which means there will be only one way to connect each village to the capital.
His engineers surveyed the country and recorded the position and altitude of each village. All the channels must go straight between two villages and be built horizontally. Since every two villages are at different altitudes, they concluded that each channel between two villages needed a vertical water lifter, which can lift water up or let water flow down. The length of the channel is the horizontal distance between the two villages. The cost of the channel is the height of the lifter. You should notice that each village is at a different altitude, and different channels can't share a lifter. Channels can intersect safely and no three villages are on the same line.
As King David's prime scientist and programmer, you are asked to find out the best solution to build the channels.
Input
There are several test cases. Each test case starts with a line containing a number N (2 <= N <= 1000), which is the number of villages. Each of the following N lines contains three integers, x, y and z (0 <= x, y < 10000, 0 <= z < 10000000). (x, y) is the position of the village and z is the altitude. The first village is the capital. A test case with N = 0 ends the input, and should not be processed.
Output
For each test case, output one line containing a decimal number, which is the minimum ratio of overall cost of the channels to the total length. This number should be rounded three digits after the decimal point.
Sample Input
4 0 0 0 0 1 1 1 1 2 1 0 3 0
Sample Output
1.000
Output
题意:
在这么一个图中求一棵生成树,这棵树的单位长度的花费最小是多少?
思路:
最小生成树的表达式可以这样写
∑x[i]*dis[i]-minsum>=0;(x[i]为0或者1,要求为一棵生成树)
这个题目ans<=(∑cost[i]*x[i])/(∑dis[i]*x[i]).变形可得∑x[i]*(cost[i]-dis[i]*ans)-0>=0;cost[i]-dis[i]*ans就相当于最小生成树中的dis[i];
二分ans,check的时候跑一边prime算法看得到的最小生成树的和是否>=0,最后可得答案;
还可以采用迭代的方法;
AC代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <stack> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++) #define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) { char CH; bool F=false; for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar()); for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar()); F && (num=-num); } int stk[70], tp; template<class T> inline void print(T p) { if(!p) { puts("0"); return; } while(p) stk[++ tp] = p%10, p/=10; while(tp) putchar(stk[tp--] + '0'); putchar('\n'); } const LL mod=1e9+7; const double PI=acos(-1.0); const double inf=1e18; const int N=15e4+10; const int maxn=1e3+10; const double eps=1e-5; int n; double cost[maxn][maxn],dis[maxn][maxn],x[maxn],y[maxn],z[maxn]; int vis[maxn]; double get_dis(int a,int b) { return sqrt((x[a]-x[b])*(x[a]-x[b])+(y[a]-y[b])*(y[a]-y[b])); } int check(double x) { mst(vis,0); double sum=0,lowcost[maxn]; vis[1]=1; For(i,1,n)lowcost[i]=cost[1][i]-x*dis[1][i]; For(i,2,n) { double temp=inf; int k=-1; For(j,2,n) { if(!vis[j]&&lowcost[j]<temp) { k=j; temp=lowcost[j]; } } if(k==-1)break; vis[k]=1; sum+=temp; For(j,2,n) { if(!vis[j]&&cost[k][j]-x*dis[k][j]<lowcost[j]) lowcost[j]=cost[k][j]-x*dis[k][j]; } } if(sum>=0)return 1; return 0; } int main() { while(1) { read(n); if(n==0)break; For(i,1,n) { scanf("%lf%lf%lf",&x[i],&y[i],&z[i]); } For(i,1,n) For(j,i+1,n) { dis[i][j]=dis[j][i]=get_dis(i,j); cost[i][j]=cost[j][i]=abs(z[i]-z[j]); } double l=0.0,r=100.0; while(r-l>=eps) { double mid=(l+r)/2; if(check(mid))l=mid; else r=mid; } printf("%.3f\n",r); } return 0; }