Meaning
n A tree of city nodes, the electricity loss of node I to Node J is i*i*r* (number of edges in the path of I to J), and now a power station is built at a node to minimize the total loss of this node to all other nodes.
Ideas
A typical tree DP
You can first use DFS to find out the number of subtree nodes per point num[u], and each point to its subtree all nodes of the total distance f[u][0];
Then with a DFS, the total distance of each node to the node to remove its subtree is f[u][1].
F[V][1] = (F[u][0]-f[v][0]-num[v]) + f[u][1] + n-num[v];
Code
/**========================================== * is a solution to ACM/ICPC problem * * @source: poj-4045 power Station * @type: Tree DP * @author: Shuangde * @blog: blog.csdn.net/shuangde800 * @email: zengshuangde@gmail.com *=========== ================================*/#include <iostream> #include <cstdio> #include <algorithm> #
include<vector> #include <queue> #include <cmath> #include <cstring> using namespace std;
typedef long long Int64;
const int INF = 0X3F3F3F3F;
Const double PI = ACOs (-1.0);
const int MAXN = 50010;
Int64 f[maxn][2];
Int64 NUM[MAXN], ans;
BOOL VIS[MAXN];
int n, I, R;
vector<int>adj[maxn];
Int64 dfs (int u) {Vis[u] = true;//U is the subtree of root node, direction down, u to total distance of all child nodes f[u][0] = 0;
U is the root node of the subtree, the direction down, total number of nodes num[u] = 1; for (int i=0; i<adj[u].size (); ++i) {int v = adj[u][i]; if (vis[v)) continue; Dfs (v); f[u][0] = f[v][0]; Num[u] + = Num[v
];
} f[u][0] + = num[u]-1;
return f[u][0]; } void DP (int u) {Vis[u]= true; for (int i=0; i<adj[u].size (); ++i) {int v = adj[u][i]; if (vis[v)) continue;//calculated with V as root node, direction up, total distance//http://www.biance
ng.cn f[v][1] = (F[u][0]-f[v][0]-num[v]) + f[u][1] + n-num[v];
DP (v);
ans = min (ans, f[u][0]+f[u][1]);
int main () {int ncase; scanf ("%d", &ncase); while (ncase--) {scanf ("%d%d%d", &n, &i, &r);
for (int i=0; i<=n; ++i) adj[i].clear ();
for (int i=0, u, v; i<n-1; ++i) {scanf ("%d%d", &u, &v); Adj[u].push_back (v); Adj[v].push_back (u);}
memset (Vis, 0, sizeof (VIS));
DFS (1);
Ans = (Int64) 1<<62;
memset (Vis, 0, sizeof (VIS));
F[1][1] = 0;
DP (1);
cout << ans*i*i*r<< Endl;
bool A = true;
for (int i=1; i<=n; ++i) {if (f[i][0]+f[i][1] = ans) {if (a) {first=false; printf ("%d", I);} else printf ("%d", I);}
printf ("\ n \ nthe");
return 0; }