当前位置:网站首页>Informatics Olympiad 1338: [example 3-3] hospital setting | Luogu p1364 hospital setting
Informatics Olympiad 1338: [example 3-3] hospital setting | Luogu p1364 hospital setting
2022-07-05 20:17:00 【Jun Yi_ noip】
【 Topic link 】
ybt 1338:【 example 3-3】 Hospital settings
Luogu P1364 Hospital settings
【 Topic test site 】
1. Binary tree
- Binary tree with pointer to parent node
There are not only pointers to left and right children in the nodes of the tree , There are also pointers to parent nodes
2. chart
The storage structure of the graph :
- Adjacency matrix
- Adjacency list
Graph traversal :
- Depth-first traversal
- Breadth first traversal
【 Their thinking 】
solution 1: enumeration
Enumerate attempts to build hospitals at each node . After determining to build a hospital at a node , Start from this node to do depth or breadth first traversal , Every time we traverse to a new node , Add the sum to the value on this node ( Number of residents ) Multiply by the distance from the node to the node where the hospital is located . After the search is over , The total sum is the sum of the distance the residents have traveled to the hospital at this location .
Calculate the sum of the residents' journey after building the hospital at each node , Compare and output the minimum value .
Storage structure , You can use binary trees with pointers to parent nodes , You can also use the storage structure of the graph : Adjacency matrix or adjacency table .
Because the graph is a tree structure , During the search process, the last searched node can be passed in as a parameter , Therefore, you can not search the visited nodes , So there is no need to set vis Array .
The complexity of the algorithm is O ( n 2 ) O(n^2) O(n2)
solution 2: The center of gravity of the tree
The complexity is O ( n ) O(n) O(n)
( To be improved )
【 Solution code 】
solution 1: enumeration
- How to write it 1: Binary tree with pointer to parent node
#include <bits/stdc++.h>
using namespace std;
#define N 105
#define INF 0x3f3f3f3f
struct Node
{
int val;// Number of residents
int left, right, parent;// Left the child , The right child , Parents
};
Node node[N];
int sum;//sum: Residents' journey and
// From fr Node search to k node , The first k The distance from the node to the hospital is d, Ask the residents for the distance and
void dfs(int k, int d, int fr)
{
sum += node[k].val*d;// The total distance plus the number of residents on the node times the distance to the hospital
int p[3] = {
node[k].parent, node[k].left, node[k].right};// take 3 There is an array of addresses that may be accessed next p in .
for(int i = 0; i < 3; ++i)
{
if(p[i] != 0 && p[i] != fr)// From fr Node search to k node , You can't start from k Node search to fr node
dfs(p[i], d + 1, k);
}
}
int main()
{
int n, val, left, right, ans = INF;
cin >> n;
for(int i = 1; i <= n; ++i)// The first i Node number is stored in node[i]
{
cin >> val >> left >> right;
node[i].val = val;// Set up i node
node[i].left = left;
node[i].right = right;
if(left != 0)// take i The parents of the children around the node are set to i
node[left].parent = i;
if(right != 0)
node[right].parent = i;
}
for(int i = 1; i <= n; ++i)
{
sum = 0;
dfs(i, 0, 0);// At node i Build a hospital , A distance of 0 The previous node does not exist , fill 0 that will do .
ans = min(ans, sum);// Find the minimum value of the sum of residents' distances
}
cout << ans;
return 0;
}
- How to write it 2: Adjacency matrix + Deep search
#include <bits/stdc++.h>
using namespace std;
#define N 105
#define INF 0x3f3f3f3f
bool edge[N][N];
int val[N];//val[i]: The first i Number of residents at the top
int n, sum;//sum: Residents' journey and
// From fr Vertex search to k The vertices , The first k The distance from the vertex to the hospital is d, Ask the residents for the distance and
void dfs(int k, int d, int fr)
{
sum += val[k]*d;// The total distance plus the number of residents on the vertex times the distance to the hospital
for(int i = 1; i <= n; ++i)
{
if(edge[k][i] && i != fr)// From fr Node search to k node , You can't start from k Node search to fr node
dfs(i, d + 1, k);
}
}
int main()
{
int l, r, ans = INF;
cin >> n;
for(int i = 1; i <= n; ++i)// The first i Node number is stored in node[i]
{
cin >> val[i] >> l >> r;//i My left child is l, The right child is r, Equivalent to having edges (i,l),(i,r)
edge[i][l] = edge[l][i] = true;
edge[i][r] = edge[r][i] = true;
}
for(int i = 1; i <= n; ++i)
{
sum = 0;
dfs(i, 0, 0);// At the top i Build a hospital , A distance of 0 The previous vertex does not exist , fill 0 that will do .
ans = min(ans, sum);// Find the minimum value of the sum of residents' distances
}
cout << ans;
return 0;
}
- How to write it 3: Adjacency list + Guang Shu
#include <bits/stdc++.h>
using namespace std;
#define N 105
#define INF 0x3f3f3f3f
struct Node
{
int f, t, d;// from f To search the t,d: distance
Node(){
}
Node(int a, int b, int c):f(a), t(b), d(c){
}
};
vector<int> edge[N];
int n, val[N];//val[i]: The first i Number of residents at the top
int bfs(int st)
{
int sum = 0;//sum: Residents' journey and
queue<Node> que;
que.push(Node(0, st, 0));// from 0 To st, Starting steps 0
while(que.empty() == false)
{
Node u = que.front();
que.pop();
sum += val[u.t]*u.d;
for(int i = 0; i < edge[u.t].size(); ++i)
{
int v = edge[u.t][i];
if(v != u.f)// from u.f To search the u.t, No more from u.t Search back u.f
que.push(Node(u.t, v, u.d + 1));// from u.t To v, The steps are u.d+1
}
}
return sum;
}
int main()
{
int l, r, ans = INF;
cin >> n;
for(int i = 1; i <= n; ++i)// The first i Node number is stored in node[i]
{
cin >> val[i] >> l >> r;//i My left child is l, The right child is r, Equivalent to having edges (i,l),(i,r)
if(l != 0)
{
edge[i].push_back(l);
edge[l].push_back(i);
}
if(r != 0)
{
edge[i].push_back(r);
edge[r].push_back(i);
}
}
for(int i = 1; i <= n; ++i)
ans = min(ans, bfs(i));// Find the minimum value of the sum of residents' distances
cout << ans;
return 0;
}
边栏推荐
- Debezium series: modify the source code to support UNIX_ timestamp() as DEFAULT value
- c語言oj得pe,ACM入門之OJ~
- Based on vs2017 and cmake GUI configuration, zxing and opencv are used in win10 x64 environment, and simple detection of data matrix code is realized
- Four methods of random number generation | random | math | threadlocalrandom | securityrandom
- model方法
- 2022年7月4日-2022年7月10日(ue4视频教程mysql)
- leetcode刷题:二叉树16(路径总和)
- CTF reverse Foundation
- 解决Thinkphp框架应用目录下数据库配置信息修改后依然按默认方式连接
- Wechat applet regular expression extraction link
猜你喜欢

Hong Kong stocks will welcome the "best ten yuan store". Can famous creative products break through through the IPO?

js实现禁止网页缩放(Ctrl+鼠标、+、-缩放有效亲测)

Based on vs2017 and cmake GUI configuration, zxing and opencv are used in win10 x64 environment, and simple detection of data matrix code is realized

Go language | 01 wsl+vscode environment construction pit avoidance Guide

Mysql频繁操作出现锁表问题
![[quick start to digital IC Verification] 8. Typical circuits in digital ICs and their corresponding Verilog description methods](/img/3a/7eaff0bf819c129b4f866388e57b87.png)
[quick start to digital IC Verification] 8. Typical circuits in digital ICs and their corresponding Verilog description methods

- Oui. Net Distributed Transaction and Landing Solution

【愚公系列】2022年7月 Go教学课程 004-Go代码注释

Guidelines for application of Shenzhen green and low carbon industry support plan in 2023

About the priority of Bram IP reset
随机推荐
selenium 元素信息
Debezium series: idea integrates lexical and grammatical analysis ANTLR, and check the DDL, DML and other statements supported by debezium
Securerandom things | true and false random numbers
Enter the parallel world
图嵌入Graph embedding学习笔记
【数字IC验证快速入门】2、通过一个SoC项目实例,了解SoC的架构,初探数字系统设计流程
model方法
leetcode刷题:二叉树12(二叉树的所有路径)
Introduction to dead letter queue (two consumers, one producer)
[quick start of Digital IC Verification] 7. Basic knowledge of digital circuits necessary for verification positions (including common interview questions)
ICTCLAS用的字Lucene4.9捆绑
leetcode刷题:二叉树17(从中序与后序遍历序列构造二叉树)
E. Singhal and Numbers(质因数分解)
【数字IC验证快速入门】8、数字IC中的典型电路及其对应的Verilog描述方法
Station B up builds the world's first pure red stone neural network, pornographic detection based on deep learning action recognition, Chen Tianqi's course progress of machine science compilation MLC,
Parler de threadlocal insecurerandom
Zero cloud new UI design
Ffplay document [easy to understand]
基础篇——配置文件解析
BZOJ 3747 POI2015 Kinoman 段树