Qin Shi Huang's National Road System(次小生成树)
来源:互联网 发布:协同过滤推荐算法 java 编辑:程序博客网 时间:2024/06/05 18:30
Qin Shi Huang's National Road System
题目链接:Click Here~
题目重述:
题目说秦始皇要修路,把N个城市用N-1条边连通。且他希望花费最小,但是这时候有一个多管闲事的道士出来说他有魔法可以帮助秦始皇变成一条路,但是只能变出一条。但是,两个人对修路的法案存在歧义,道士希望修路可以给更多的百姓带来福利,而秦始皇希望修路要尽量使花费小。最后,秦始皇拿出了一个公式A/B,A表示两个城市的人数,B表示出了用魔法变出来的路外,最短的总距离。现在要你求出A/B的最大值。
思路分析:
从题目可以看出是一个此小生成树问题,但是也有人有DP AC了。具体思路就看网上别人的解释吧。
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <cmath>using namespace std;const int N = 1e3 + 5;int n,m;double path[N][N],G[N][N];bool used[N][N],hash[N];struct Point{ double x,y,cost;}p[N];double Dist(Point a,Point b){ return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}double Prim(){ int pre[N]; double sum = 0,mincost[N]; memset(hash,0,sizeof(hash)); memset(used,0,sizeof(used)); memset(path,0,sizeof(path)); hash[1] = 1; for(int i = 1;i <= n;++i){ mincost[i] = G[1][i]; pre[i] = 1; } for(int i = 1;i < n;++i) { int u = -1; for(int j = 1;j <= n;++j)if(!hash[j]){ if(u == -1||mincost[j] < mincost[u]) u = j; } hash[u] = 1; used[u][pre[u]] = used[pre[u]][u] = 1; //判断两点之间是否在生成树中 sum += G[pre[u]][u]; for(int j = 1;j <= n;++j) { if(hash[j]&&u != j){ path[j][u] = path[u][j] = max(mincost[u],path[j][pre[u]]); //生成树中的最大权值// path[j][u] = path[u][j] = mincost[u]; } if(!hash[j]){ if(mincost[j] > G[u][j]){ mincost[j] = G[u][j]; pre[j] = u; } } } } return sum;}int main(){ int T; scanf("%d",&T); while(T--) { scanf("%d",&n); for(int i = 1;i <= n;++i){ scanf("%lf%lf%lf",&p[i].x,&p[i].y,&p[i].cost); } memset(G,0,sizeof(G)); for(int i = 1;i <= n;++i) for(int j = i+1;j <= n;++j){ G[i][j] = G[j][i] = Dist(p[i],p[j]); } double mincost = Prim(); double ratio = -1; for(int i = 1;i <= n;++i) for(int j = 1;j <= n;++j) printf("path[%d][%d] = %lf\n",i,j,path[i][j]); for(int i = 1;i <= n;++i) for(int j = i+1;j <= n;++j){ if(used[i][j]){ ratio = max(ratio,(p[i].cost+p[j].cost)/(mincost-G[i][j])); }else { //删除生成树中的最大的一条边,生成一个类此小生成树(因为,不是严格的此小生成树) ratio = max(ratio,(p[i].cost+p[j].cost)/(mincost-path[i][j])); } } printf("%.2lf\n",ratio); } return 0;}
1 0
- uva 1494 - Qin Shi Huang's National Road System(次小生成树类似问题)
- HDU 4081 Qin Shi Huang's National Road System (枚举次小生成树)
- Qin Shi Huang's National Road System(次小生成树)
- UVALive 5713 Qin Shi Huang's National Road System(次小生成树)
- 杭电4081 Qin Shi Huang's National Road System(次小生成树)
- HDU-#4081 Qin Shi Huang's National Road System(次小生成树变形)
- hdu 4081 Qin Shi Huang's National Road System(次小生成树)
- HDU4081 Qin Shi Huang's National Road System(次小生成树)
- hdu4081 Qin Shi Huang's National Road System(次小生成树模板)
- uva 1494 Qin Shi Huang's National Road System (次小生成树)
- hdoj 4081 Qin Shi Huang's National Road System(次小生成树变形)
- UVA 1494 - Qin Shi Huang's National Road System(次小生成树)
- HDU4081 Qin Shi Huang's National Road System(prim+次小生成树)
- HDU-4081-Qin Shi Huang's National Road System(次小生成树)
- hdu-4081 Qin Shi Huang's National Road System(次小生成树)
- poj 1944 Qin Shi Huang's National Road System(次小生成树)
- HDU-4081-Qin Shi Huang's National Road System (次小生成树)
- HDU4081-Qin Shi Huang's National Road System(次小生成树)
- UVa:11100 The Trip, 2007
- asp.net CMS无法打开虚拟目录
- HTTP跳转
- JAVA的字符串拼接和性能
- 2014寒假训练计划
- Qin Shi Huang's National Road System(次小生成树)
- poj-1042 && nyoj-30(Gone fishing)
- awk 计算两个数的百分比
- form中onsubmit的使用
- poj1850
- 利用httplib.HTTPSConnection 发送post请求的方式
- Windows Azure功能更新:网站服务支持过渡环境,支持异步任务,监控改进等
- Spark on Yarn:性能调优
- freebsd介绍