若医院建在1 处,则距离和=4+12+2*20+2*40=136;若医院建在3 处,则距离和=4*2+13+20+40=81……
标签:name ext input ret style min 描述 data user
5
13 2 3
4 0 0
12 4 5
20 0 0
40 0 0
81
思路:Floyd跑出最短路,再枚举医院的位置。
#include<bits/stdc++.h> using namespace std; int n, dp[105][105],p[105],ans; #define REP(i, a, b) for(int i = (a); i <= (b); ++ i) #define REP(j, a, b) for(int j = (a); j <= (b); ++ j) #define PER(i, a, b) for(int i = (a); i >= (b); -- i) template <class T> inline void rd(T &ret) { char c; ret = 0; while ((c = getchar()) < ‘0‘ || c > ‘9‘); while (c >= ‘0‘ && c <= ‘9‘) { ret = ret * 10 + (c - ‘0‘), c = getchar(); } } int main() { rd(n); ans = 0x3f3f3f3f; REP(i, 1, n) { REP(j, 1, n) { dp[i][j] = 0x3f3f3f3f; } } REP(i, 1, n) { int y, z; rd(p[i]),rd(y), rd(z); if (y)dp[i][y] = dp[y][i]=1; if (z)dp[i][z] = dp[z][i] = 1; dp[i][i] = 0; } for (int k = 1; k <= n; k++) { REP(i, 1, n) { REP(j, 1, n) { dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]); } } } REP(i, 1, n) { int cur = 0; REP(j, 1, n) { cur += dp[i][j] * p[j]; } ans = min(ans, cur); } cout << ans << endl; }
标签:name ext input ret style min 描述 data user
原文地址:https://www.cnblogs.com/czy-power/p/10359994.html