博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[POJ 2728]Desert King(0-1分数规划/最优比率生成树)
阅读量:6690 次
发布时间:2019-06-25

本文共 2993 字,大约阅读时间需要 9 分钟。

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.

Solution

最优比率生成树,其实知道0-1分数规划应该就很好做了

稠密图的话用prim会比较好

学过kruskal就没再写过prim的我去重学了一遍╮(╯_╰)╭

#include
#include
#include
#include
#include
#define MAXN 1005#define eps 1e-10using namespace std;int n,x[MAXN],y[MAXN],h[MAXN];double dis[MAXN],map[MAXN][MAXN],f[MAXN][MAXN],cost[MAXN][MAXN];bool vis[MAXN];bool check(double mid){ memset(vis,0,sizeof(vis)); memset(dis,127,sizeof(dis)); dis[1]=0;double res=0; for(int i=1;i<=n;i++) { double mincost=1e20;int k=-1; for(int j=1;j<=n;j++) if(!vis[j]&&mincost>dis[j])mincost=dis[j],k=j; vis[k]=1,res+=mincost; for(int j=1;j<=n;j++) if(!vis[j]&&dis[j]>cost[k][j]-mid*map[k][j])dis[j]=cost[k][j]-mid*map[k][j]; } if(res>0)return false; return true;}int main(){ while(~scanf("%d",&n)&&n) { for(int i=1;i<=n;i++) scanf("%d%d%d",&x[i],&y[i],&h[i]); for(int i=1;i<=n;i++) for(int j=1;j<=i;j++) { map[i][j]=map[j][i]=(double)sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])); cost[i][j]=cost[j][i]=abs(h[i]-h[j]); } double l=0,r=100,mid; while(r-l>eps) { mid=(l+r)/2; if(check(mid))r=mid; else l=mid; } printf("%.3lf\n",mid); } return 0;}

 

转载于:https://www.cnblogs.com/Zars19/p/6949561.html

你可能感兴趣的文章
quartz CronExpression表达式
查看>>
变速变调原理与方法总结
查看>>
西风 West Wind Html帮助构建器
查看>>
杭电2053
查看>>
Arora is a lightweight cross-platform web browser.
查看>>
Android Developers:日历提供者
查看>>
MYSQL 插入二进制数的 2 种方法。
查看>>
Lazarus中system.length说明
查看>>
23. Spring Boot启动加载数据CommandLineRunner【从零开始学Spring Boot】
查看>>
Android开发工程师:Mac 上怎么进行OpenGL环境搭建
查看>>
优秀程序员成功的几个好习惯
查看>>
我们如何使用HAProxy实现单机200万SSL连接
查看>>
Go 语言开源这九年:它是不是你最喜欢的语言?
查看>>
入华四周年,微软Azure不仅干了三件大事,还完成了很多小目标!
查看>>
MWC 2018:华为发布意图驱动的智简网络
查看>>
彰显国货品质 H3C高扩展性服务器推荐
查看>>
跨国族群布里亚特的特殊“年味儿”
查看>>
登台轮赠春联 厦门边检便利通关暖台胞
查看>>
发动机存隐患 现代起亚宣布在美召回16.8万辆车
查看>>
长春7旬老人收藏明信片48年 6千张见证国家变迁
查看>>