POJ 0-1规划 最优比率生成树 2728 Desert King
2014-03-26 18:47
381 查看
Desert King
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.
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
Sample Output
Source
Beijing 2005
题意:就是找最优比率生成树。。。
思路:本人第一道0-1规划,0-1规划解决的问题基本是k=f(x)/g(y) ,其中x和y是向量,值是0或者1,要使得k最小。我们构造出另一个函数 h(x,y) = min{f(x)-k*g(y)} ,要使k最小,当且仅当h(x,y) = 0,而且这个函数式单调递减的。 我就不证明了。。。其实我忘了怎么证明了。那么这道题要求的是费用跟长度之间的比值的最大值,刚好满足这个形式,我们解决这个问题可以用二分,也可以用什么什么迭代法
。。。我忘了名字,忽略吧。
那个迭代法速度回快一些,但是精度可能没那么好,二分慢一些,精度好一些。看情况选择吧。还有要注意的是这道题目是一个稠密图(这里就是完全图了),不要用Kruskal(O(mlogm)) 用朴素的Prim就好了O(n^2) ,也不要加堆了,稠密图可能会变为O(n*n*log(n)),那就更加慢了。
代码:
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<cstring>
#include<string.h>
using namespace std;
#define eps 1e-8
const int maxn = 1000 + 5;
const int maxm = maxn*maxn;
int n, m;
const double inf = 1e300;
double cx,dx;
double cost[maxn][maxn];
double len[maxn][maxn];
vector<int> G[maxn];
double w[maxn][maxn];
int pre[maxn];
bool vis[maxn];
double d[maxn];
bool first;
double Prim(double L)
{
for (int u = 0; u < n;++u)
for (int v = u + 1; v < n; ++v)
w[u][v] = w[v][u] = cost[u][v] - L*len[u][v];
memset(pre, -1, sizeof(pre));
for (int i = 0; i < n; ++i)
d[i] = inf;
double ans = d[0] = 0;
memset(vis, 0, sizeof(vis));
dx = cx = 0;
for (int i = 0; i < n; ++i)
{
int k = -1;
double mi = inf;
for (int j = 0; j < n; ++j) if (!vis[j] && d[j] < mi)
mi = d[j], k = j;
if (k == -1) break;
vis[k] = true;
ans += mi;
if(pre[k]!=-1) dx += cost[pre[k]][k];
if(pre[k]!=-1) cx += len[pre[k]][k];
for (int j = 0; j<n; ++j)
{
if (!vis[j] && d[j]>w[k][j]) d[j] = w[k][j], pre[j] = k;
}
}
return ans;
}
struct Village
{
double x, y, z;
Village(double x=0.0, double y=0.0, double z=0.0)
:x(x), y(y), z(z) { }
bool operator<(const Village&v) const
{
return z < v.z;
}
}v[maxn];
inline double sqr(double x) { return x*x; }
inline double CalDist(const Village&v1, const Village&v2)
{
return sqrt(sqr(v1.x - v2.x) + sqr(v1.y - v2.y));
}
void input()
{
for (int i = 0; i < n; ++i) scanf("%lf%lf%lf", &v[i].x, &v[i].y, &v[i].z);
sort(v, v + n);
for (int i = 0; i < n;++i)
for (int j = i + 1; j < n; ++j)
{
cost[i][j] = cost[j][i] = v[j].z - v[i].z;
len[i][j] = len[j][i] = CalDist(v[j], v[i]);
}
}
void solve()
{
double L = 1e8;
while (true)
{
double x = Prim(L);
if (x<eps&&x>-eps) break;
L = dx / cx;
}
printf("%.3f\n", L);
}
int main()
{
while (scanf("%d", &n) == 1)
{
if (n == 0) return 0;
input();
solve();
}
}
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 19501 | Accepted: 5446 |
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
Source
Beijing 2005
题意:就是找最优比率生成树。。。
思路:本人第一道0-1规划,0-1规划解决的问题基本是k=f(x)/g(y) ,其中x和y是向量,值是0或者1,要使得k最小。我们构造出另一个函数 h(x,y) = min{f(x)-k*g(y)} ,要使k最小,当且仅当h(x,y) = 0,而且这个函数式单调递减的。 我就不证明了。。。其实我忘了怎么证明了。那么这道题要求的是费用跟长度之间的比值的最大值,刚好满足这个形式,我们解决这个问题可以用二分,也可以用什么什么迭代法
。。。我忘了名字,忽略吧。
那个迭代法速度回快一些,但是精度可能没那么好,二分慢一些,精度好一些。看情况选择吧。还有要注意的是这道题目是一个稠密图(这里就是完全图了),不要用Kruskal(O(mlogm)) 用朴素的Prim就好了O(n^2) ,也不要加堆了,稠密图可能会变为O(n*n*log(n)),那就更加慢了。
代码:
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<cstring>
#include<string.h>
using namespace std;
#define eps 1e-8
const int maxn = 1000 + 5;
const int maxm = maxn*maxn;
int n, m;
const double inf = 1e300;
double cx,dx;
double cost[maxn][maxn];
double len[maxn][maxn];
vector<int> G[maxn];
double w[maxn][maxn];
int pre[maxn];
bool vis[maxn];
double d[maxn];
bool first;
double Prim(double L)
{
for (int u = 0; u < n;++u)
for (int v = u + 1; v < n; ++v)
w[u][v] = w[v][u] = cost[u][v] - L*len[u][v];
memset(pre, -1, sizeof(pre));
for (int i = 0; i < n; ++i)
d[i] = inf;
double ans = d[0] = 0;
memset(vis, 0, sizeof(vis));
dx = cx = 0;
for (int i = 0; i < n; ++i)
{
int k = -1;
double mi = inf;
for (int j = 0; j < n; ++j) if (!vis[j] && d[j] < mi)
mi = d[j], k = j;
if (k == -1) break;
vis[k] = true;
ans += mi;
if(pre[k]!=-1) dx += cost[pre[k]][k];
if(pre[k]!=-1) cx += len[pre[k]][k];
for (int j = 0; j<n; ++j)
{
if (!vis[j] && d[j]>w[k][j]) d[j] = w[k][j], pre[j] = k;
}
}
return ans;
}
struct Village
{
double x, y, z;
Village(double x=0.0, double y=0.0, double z=0.0)
:x(x), y(y), z(z) { }
bool operator<(const Village&v) const
{
return z < v.z;
}
}v[maxn];
inline double sqr(double x) { return x*x; }
inline double CalDist(const Village&v1, const Village&v2)
{
return sqrt(sqr(v1.x - v2.x) + sqr(v1.y - v2.y));
}
void input()
{
for (int i = 0; i < n; ++i) scanf("%lf%lf%lf", &v[i].x, &v[i].y, &v[i].z);
sort(v, v + n);
for (int i = 0; i < n;++i)
for (int j = i + 1; j < n; ++j)
{
cost[i][j] = cost[j][i] = v[j].z - v[i].z;
len[i][j] = len[j][i] = CalDist(v[j], v[i]);
}
}
void solve()
{
double L = 1e8;
while (true)
{
double x = Prim(L);
if (x<eps&&x>-eps) break;
L = dx / cx;
}
printf("%.3f\n", L);
}
int main()
{
while (scanf("%d", &n) == 1)
{
if (n == 0) return 0;
input();
solve();
}
}
相关文章推荐
- POJ 2728 Desert King (最优比率生成树---01分数规划)
- POJ 2728 Desert King ★(01分数规划介绍 && 应用の最优比率生成树)
- POJ 2728 Desert King ★(01分数规划介绍 && 应用の最优比率生成树)
- POJ 2728 Desert King(最优比率生成树 01分数规划)
- POJ 2728 Desert King 01分数规划,最优比率生成树
- 【POJ】2728 Desert King 最优比率生成树——01分数规划【经典】
- Desert King (poj 2728 最优比率生成树 0-1分数规划)
- poj 2728 Desert King(最优比率生成树,01分数规划)
- POJ-2728 Desert King 01参数规划-最优比率生成树
- poj_2728 Desert King(最优比率生成树+01分数规划+二分+prim)
- POJ 2728 Desert King (最优比率生成树 01分数规划问题)
- 【分数规划,最优比率生成树】POJ 2728 Desert King
- poj 2728 Desert King (最优比率生成树/01分数规划)
- POJ 2728 Desert King 01分数规划 最优比率生成树
- POJ 2728-Desert King(01分数规划_最优比率生成树)
- poj 2728 Desert King(01分数规划--最优比率生成树)
- poj 2728 Desert King(最优比率生成树)
- POJ 2728 Desert King 最优比率生成树
- [POJ 2728][最优比率生成树] Desert King
- POJ2728 Desert King 最优比率生成树