日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【POJ - 2728】Desert King (最有比率生成树,分数规划)

發布時間:2023/12/10 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【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 0

Sample 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 (最有比率生成树,分数规划)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。