POJ 4045 Power Station__poj

Source: Internet
Author: User

Description

  The massive tsunami that struck the coastal city has  washed away many of  and inhabitants the Re. After the tsunami,  the power supply facilities  of Coastal  the   are City completely destroyed. People are in panic in the dark night. doubts  remain over whether the communities would be able to rebuild the city. To calm people  down, the heads of the "City are planning to rebuild  the" to "Start with" recovery  O f The power supply facilities.  The coastal city consists of n communities which are numbered from 1 to N. to  Save the electric cables  n-1  cables has been to connect these  communities  used, So this each pair of communities  is able to transfer electronic energy .    the heads of the decide to set a power station in one of the communities.  There  is  the Rmal Energy loss along the cables. Each cable has a resisTance of R ohm.  the total  thermal energy loss is the sum of  i^2* ri . here  ri is The total residence along  the path between the  ith community and the power station, And I is a constant. they  are troubling their head in the issue of where to set the "station to" the total  energy Loss minimized.

Input

There are multiple test cases.  The contains one integer indicating the number of test cases. For the contains three positive integers n, I and R, indicating the number of communities , the above mentioned constant and the residence of each cable.  (3≤n≤50000, 1≤i≤10, 1≤r≤50) The next n-1 lines, describe a cable connection by two integers x, Y, which indicates, and between Community X and C Ommunity Y, there is a cable.

Output

For each test case, please output two lines.  The minimum total thermal energy loss.  The second line are all of the optional communities in ascending order. You are are requested to leave a blank line after the each test case.

Sample Input

2 5 1 1 3 2 1 2 5 2 4 3 6 1 2 1 2 2 3 3 4 2 6 3-5

Sample Output

5 2 14 2 3

Tree DP, find the distance from all points in the tree to the other points and

#include <map> #include <stack> #include <queue> #include <cmath> #include <cstdio> # include<vector> #include <string> #include <cstdlib> #include <cstring> #include <algorithm
> Using namespace std;
typedef long Long LL;
const int maxn=50005;
Vector<int> TREE[MAXN];
LL X,Y,T,N,CNT[MAXN],SUM[MAXN],F[MAXN];

LL I,r;
	void Dfs (int x,int fa) {cnt[x]=1; sum[x]=0;
		for (int i=0;i<tree[x].size (); i++) if (TREE[X][I]!=FA) {DFS (tree[x][i],x);
		Cnt[x]+=cnt[tree[x][i]];
	Sum[x]+=cnt[tree[x][i]]+sum[tree[x][i]]; } void Dfs (int x,int fa) {for (int i=0;i<tree[x].size (); i++) if (TREE[X][I]!=FA) {F[tree[x][i]]=f[x]-cnt[tree [x] [I]]
		+n-cnt[tree[x][i]];
	Dfs (TREE[X][I],X);
	int main () {scanf ("%lld", &t);	
		while (t--) {scanf ("%lld%lld%lld", &n,&i,&r);
			for (int i=1;i<=n;i++) {tree[i].clear ();
		f[i]=cnt[i]=sum[i]=0;
	for (int i=1;i<n;i++) {scanf ("%lld%lld", &x,&y);		Tree[x].push_back (y);
		Tree[y].push_back (x);
		DFS (1,1);
		F[1]=SUM[1];
		Dfs (1,1);
		LL ans=0x7fffffff;
		for (int i=1;i<=n;i++) ans=min (Ans,f[i]);
		printf ("%lld\n", ans*i*i*r);
		int flag=0;
				for (int i=1;i<=n;i++) if (F[i]==ans) {if (flag) printf ("");
				else flag=1;
			printf ("%d", I);
	printf ("\ n \ nthe");
return 0; }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.