博客
关于我
P1364 医院设置(Floyed)
阅读量:399 次
发布时间:2019-03-05

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

医院设置

题目描述

设有一棵二叉树,如图:
其中,圈中的数字表示结点中居民的人口。圈边上数字表示结点编号,现在要求在某个结点上建立一个医院,使所有居民所走的路程之和为最小,同时约定,相邻接点之间的距离为1。如上图中,
若医院建在1 处,则距离和=4+12+220+240=136;若医院建在3 处,则距离和=4*2+13+20+40=81……
输入格式
第一行一个整数n,表示树的结点数。(n≤100)
接下来的n行每行描述了一个结点的状况,包含三个整数,整数之间用空格(一个或多个)分隔,其中:第一个数为居民人口数;第二个数为左链接,为0表示无链接;第三个数为右链接。
输出格式
一个整数,表示最小距离和。
输入输出样例
输入
5
13 2 3
4 0 0
12 4 5
20 0 0
40 0 0
输出
81
分析
这是一道模板题,我用的是Floyed算法
可参考
中的Floyed
AC代码

#include
#include
using namespace std;int n,x,y,m,mm,w[105],a[105][105];int main(){ cin>>n; memset(a,1,sizeof(a)); for(int i=1;i<=n;i++) { cin>>w[i]>>x>>y; a[i][x]=1;//无向图 a[x][i]=1; a[i][y]=1; a[y][i]=1; a[i][i]=0;//自己不能走自己 } for(int k=1;k<=n;k++)//Floyed标准5行 for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(i!=j&&j!=k&&i!=k) a[i][j]=min(a[i][j],a[i][k]+a[k][j]); m=2147483647; for(int i=1;i<=n;i++) { mm=0; for(int j=1;j<=n;j++)//找和的最小值 mm+=w[j]*a[i][j]; m=min(mm,m); } cout<

谢谢

转载地址:http://pqpzz.baihongyu.com/

你可能感兴趣的文章
Nginx+Lua 开发高性能Web应用实战
查看>>
nginx+mysql+redis+mongdb+rabbitmq 自动化部署脚本
查看>>
nginx+php的搭建
查看>>
Nginx+Redis+Ehcache:大型高并发与高可用的三层缓存架构总结
查看>>
nginx+tomcat+memcached
查看>>
nginx+tomcat单个域名及多个域名配置
查看>>
Nginx+Tomcat实现动静分离
查看>>
nginx+Tomcat性能监控
查看>>
nginx+uwsgi+django
查看>>
nginx+vsftp搭建图片服务器
查看>>
Nginx-http-flv-module流媒体服务器搭建+模拟推流+flv.js在前端html和Vue中播放HTTP-FLV视频流
查看>>
nginx-vts + prometheus 监控nginx
查看>>
Nginx/Apache反向代理
查看>>
Nginx: 413 – Request Entity Too Large Error and Solution
查看>>
nginx: [emerg] getpwnam(“www”) failed 错误处理方法
查看>>
nginx: [emerg] the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:
查看>>
nginx: [error] open() “/usr/local/nginx/logs/nginx.pid“ failed (2: No such file or directory)
查看>>
nginx:Error ./configure: error: the HTTP rewrite module requires the PCRE library
查看>>
Nginx:objs/Makefile:432: recipe for target ‘objs/src/core/ngx_murmurhash.o‘解决方法
查看>>
nginxWebUI runCmd RCE漏洞复现
查看>>