P1364 Hospital Setup-floyd

Topic description

There is a binary tree, as shown in the figure:
insert image description here

where the numbers in the circles represent the population of residents in the node. The number on the edge of the circle represents the node number. Now it is required to build a hospital on a certain node, so that the sum of the distances traveled by all residents is the smallest. At the same time, it is agreed that the distance between adjacent nodes is 11. As shown in the figure above, if the hospital is built in 1 place, then the distance sum = 4 + 12 + 2 × 20 + 2 × 40 = 136 = 4 + 12 + 2 × 20 + 2 × 40 = 136 4+12+2\times20+ 2\times40=136=4+12+2×20+2×40=1364+12+2×20+2×40=136=4+12+2×20+2×40=1 3 6 ; if the hospital is built in 3 places, then the distance sum= 4 × 2 + 13 + 20 + 40 = 81 = 4 × 2 + 13 + 20 + 40 = 81 =4\times2+13+20+40=81 =4×2+13+20+40=81=4×2+13+20+40=81=4×2+13+20+40=81

analyze

Data size and convention

for 100% 100\%1 0 0 % of the data, guaranteed1 ≤ n ≤ 100 , 0 ≤ u , v ≤ n 0 , 1 ≤ w ≤ 1 0 5 1 \leq n \leq 100, 0 \leq u, v \leq n0, 1 \ leq w \leq 10^51n1000u ,vn 0 1w105


I put the data range here deliberately to tell myself to value this process of inferring algorithms based on data.
When the amount of data is less than or equal to 100, the algorithms that can be used include floyd, dp, 高斯消元
after obtaining this question, the multi-source shortest path is examined instead of dfs traversing the tree, and the model in the question is successfully stripped out. This question also lost its mystery

#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int n;
int d[N][N], w[N];

int main() {
    
    
	cin >> n;
	
	memset(d, 0x3f, sizeof d);
	for (int i = 1; i <= n; i++) {
    
    
		int c, a, b;
		cin >> c >> a >> b;
		w[i] = c;
		d[i][i] = 1;
		if (a) d[i][a] = 1, d[a][i] = 1;
		if (b) d[i][b] = 1, d[b][i] = 1;
	}
	
	for(int k = 1; k <= n; k++) 
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
		
	int ans = 1e9;
	for (int i = 1; i <= n; i++) {
    
    
		int temp = 0;
		for (int j = 1; j <= n; j++) {
    
    
			if (j == i) continue;
			temp += d[i][j] * w[j];
		}
		ans = min(ans, temp);
	}
	cout << ans << endl;
	
	return 0;
}

later

I didn't think that my ability was still very lacking. I couldn't solve this kind of problem
. ! !
2022-03-21

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324114418&siteId=291194637