【POJ - 2728】Desert King (最有比率生成树,分数规划)
題干:
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.
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 0Sample Output
1.000題目大意:
有n個村莊,村莊在不同坐標和海拔,現在要對所有村莊供水,只要兩個村莊之間有一條路即可,建造水管距離為坐標之間的歐幾里德距離,費用為海拔之差,現在要求方案使得費用與距離的比值最小,很顯然,這個題目是要求一棵最優比率生成樹。
一句話題意:
解題報告:
除了二分,還有一種算法叫Dinkelbach算法
每次將r'代入z函數中計算以后,我們將得到一組x
讓r''=(∑(ci*xi))/(∑(di*xi))
當r''=r'時,r''就是我們需要的解
否則將r'=r'',繼續迭代
這種方法比二分法要快一點
AC代碼:(迭代法)
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair using namespace std; const int MAX = 2e5 + 5; int n; const double eps = 1e-5; struct Node {double x,y,z; } node[MAX]; struct NN {int u,v;double x,y; } ee[20 * MAX]; struct Edge {int u,v;double x,y,w; } e[20 * MAX]; int f[MAX]; int tot; bool cmp(Edge a,Edge b) {return a.w < b.w; } int getf(int v) {return f[v] == v ? v : f[v] = getf(f[v]); } void merge(int u,int v) {int t1 = getf(u);int t2 = getf(v);f[t2] = t1; } void init() {for(int i = 1; i<=n; i++) f[i] = i; } double kls() {sort(e+1,e+tot+1,cmp);init();int cnt = 0;double xx = 0,yy = 0;for(int i = 1; i<=tot; i++) {if(getf(e[i].u) != getf(e[i].v)) {xx += e[i].x;yy += e[i].y;cnt ++;merge(e[i].u,e[i].v);if(cnt == n - 1) break;}}return xx/yy; } double ok(double mid) {for(int i = 1; i<=tot; i++) e[i].u = ee[i].u,e[i].v = ee[i].v,e[i].x = ee[i].x,e[i].y = ee[i].y,e[i].w = ee[i].x - mid*ee[i].y;double res = kls();return res; } int main() {while(~scanf("%d",&n)) {if(n == 0) break;tot = 0;//init() for(int i = 1; i<=n; i++) f[i] = i;for(int i = 1; i<=n; i++) {scanf("%lf%lf%lf",&node[i].x,&node[i].y,&node[i].z);}for(int i = 1; i<=n; i++) {for(int j = 1; j<i; j++) {ee[++tot].y = sqrt((node[i].x-node[j].x)*(node[i].x-node[j].x) + (node[i].y-node[j].y)*(node[i].y-node[j].y));ee[tot].x = fabs(node[i].z - node[j].z);ee[tot].u = i;ee[tot].v = j;} }double l = 0,r = 1e9; // double mid = (l+r)/2,ans=mid; // while(l+eps < r) { // mid = (l+r)/2; // if(ok(mid)) r = mid; // else l = mid; // }double mid = (l+r)/2,ans = -1;while(fabs(ans-mid) >= eps) {ans = mid;mid = ok(mid);}printf("%.3f\n",ans - eps);}return 0 ;}TLE代碼:(二分)
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair using namespace std; const int MAX = 2e5 + 5; int n; const double eps = 1e-5; struct Node {double x,y,z; } node[MAX]; struct NN {int u,v;double x,y; } ee[20 * MAX]; struct Edge {int u,v;double w; } e[20 * MAX]; int f[MAX]; int tot; bool cmp(Edge a,Edge b) {return a.w < b.w; } int getf(int v) {return f[v] == v ? v : f[v] = getf(f[v]); } void merge(int u,int v) {int t1 = getf(u);int t2 = getf(v);f[t2] = t1; } void init() {for(int i = 1; i<=n; i++) f[i] = i; } double kls() {sort(e+1,e+tot+1,cmp);init();int cnt = 0;double res = 0;for(int i = 1; i<=tot; i++) {if(getf(e[i].u) != getf(e[i].v)) {res += e[i].w;cnt ++;merge(e[i].u,e[i].v);if(cnt == n - 1) break;}}return res; } bool ok(double mid) {for(int i = 1; i<=tot; i++) e[i].u = ee[i].u,e[i].v = ee[i].v,e[i].w = ee[i].x - mid*ee[i].y;double res = kls();if(res <=0) return 1;else return 0 ; } int main() {while(~scanf("%d",&n)) {if(n == 0) break;tot = 0;init();for(int i = 1; i<=n; i++) {scanf("%lf%lf%lf",&node[i].x,&node[i].y,&node[i].z);}for(int i = 1; i<=n; i++) {for(int j = 1; j<i; j++) {ee[++tot].y = sqrt((node[i].x-node[j].x)*(node[i].x-node[j].x) + (node[i].y-node[j].y)*(node[i].y-node[j].y));ee[tot].x = fabs(node[i].z - node[j].z);ee[tot].u = i;ee[tot].v = j;} }double l = 0,r = 1e9;double mid = (l+r)/2;while(l+eps < r) {mid = (l+r)/2;if(ok(mid)) r = mid;else l = mid;}printf("%.3f\n",mid - eps);}return 0 ;}AC代碼:
?
總結
以上是生活随笔為你收集整理的【POJ - 2728】Desert King (最有比率生成树,分数规划)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 申请信用卡工作证明范本 这样写更容易通过
- 下一篇: 靠档计息的为什么要取消?靠档计息叫停该怎