01分数规划+prim POJ2728 Desert King
2018-06-17 22:12:10来源:未知 阅读 ()
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 26009 | Accepted: 7219 |
Description
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
Output
Sample Input
4 0 0 0 0 1 1 1 1 2 1 0 3 0
Sample Output
1.000
Source
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<cmath> 6 using namespace std; 7 const double acc=1e-7; 8 const double inf=1e15; 9 int n; 10 struct data{ 11 double x,y,z; 12 }node[1010]; 13 double l,r,mid; 14 double dis[1010][1010],h[1010][1010],w[1010]; 15 bool check[1010]; 16 double ds(double x1,double y1,double x2,double y2){ 17 return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); 18 } 19 double prim(double c){//coefficient-系数 20 double ret=0.0; 21 for(int i=1;i<=n;i++) w[i]=inf;//double 赋初值 22 memset(check,0,sizeof(check)); 23 w[1]=0.0; 24 for(int i=1;i<=n;i++){ 25 double mn=inf; 26 int k; 27 for(int j = 1;j<=n;j++) 28 if(!check[j]&&w[j]<mn) mn=w[j],k=j; 29 check[k]=1; 30 ret+=mn; 31 for(int j=1;j<=n;j++) 32 if(!check[j]&&w[j]>h[k][j]-c*dis[k][j]) 33 w[j]=h[k][j]-c*dis[k][j]; 34 } 35 return ret; 36 } 37 int main(){ 38 while(scanf("%d",&n)){ 39 if(!n) return 0; 40 for(int i=1;i<=n;i++){ 41 scanf("%lf%lf%lf",&node[i].x,&node[i].y,&node[i].z); 42 for(int j=1;j<=i;j++){ 43 dis[i][j]=dis[j][i]=ds(node[i].x,node[i].y,node[j].x,node[j].y); 44 h[i][j]=h[j][i]=abs(node[i].z-node[j].z); 45 } 46 } 47 l=0.0; 48 r=10000.0; 49 while(r-l>acc){ 50 mid=(l+r)*1.0/2; 51 if(prim(mid)>0) l=mid; 52 else r=mid; 53 } 54 printf("%.3f\n",mid); 55 } 56 }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
下一篇:3002 石子归并 3
- [题记-动态规划] 编辑距离 - leetcode 2020-04-06
- 动态规划:最大子串和 2020-01-30
- 关于 DP 的一些内容 2019-12-25
- 【c++primer练习】 typedef与指针、常量和类型别名 2019-11-18
- C++动态规划实现查找最长公共子序列 2019-10-31
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash