poj 4045 Power Station dfs求树上最小距离和

题意:

给一棵树,每条边的权值为R*I^2,求到所有其他节点权值和最小的点和相应最小权值和。

分析:

先将图转化成树,然后在树上dfs。

代码:

//poj 4045
//sep9
#include<iostream>
#include<vector>
using namespace std;
const int maxN=50012;
vector<int> g[maxN];
int vis[maxN],bro[maxN],son[maxN];
__int64 num[maxN],dis[maxN],dp[maxN];

int n,I,R;

void build_tree(int h)
{
	vis[h]=1;
	for(int i=g[h].size()-1;i>=0;--i){
		int x=g[h][i];
		if(vis[x]==0){
			vis[x]=1;
			bro[x]=son[h];
			son[h]=x;
			build_tree(x);
		}
	}
}

void dfs(int h,__int64& num_res,__int64& dis_res)
{
	num_res=1,dis_res=0;
	int s=son[h];
	while(s){
		__int64 x,y;
		dfs(s,x,y);
		num_res+=x;
		dis_res+=(x+y);
		s=bro[s];
	}
	num[h]=num_res;
	dis[h]=dis_res;
}

void ex_dfs(int h,__int64 pre_sum)
{
	dp[h]=dis[h]+pre_sum;
	int s=son[h];
	while(s){
		__int64 a=n-num[s];
		__int64 b=dis[h]-(dis[s]+num[s])+pre_sum;
		ex_dfs(s,a+b);
		s=bro[s];
	}
} 

int main()
{
	int cases;
	scanf("%d",&cases);
	while(cases--){
		scanf("%d%d%d",&n,&I,&R);
		for(int i=1;i<=n;++i) g[i].clear();
		for(int i=1;i<n;++i){
			int a,b;
			scanf("%d%d",&a,&b);
			g[a].push_back(b);
			g[b].push_back(a);
		}
		memset(vis,0,sizeof(vis));
		memset(bro,0,sizeof(bro));
		memset(son,0,sizeof(son));
		build_tree(1);
		memset(num,0,sizeof(num));
		memset(dis,0,sizeof(dis));
		__int64 x,y;
		dfs(1,x,y);
		ex_dfs(1,0);
		__int64 ans=dp[1];
		for(int i=1;i<=n;++i)
			ans=min(ans,dp[i]);
		printf("%I64d\n",ans*R*I*I);
		for(int i=1;i<=n;++i)
			if(dp[i]==ans)
				printf("%d ",i);
		printf("\n\n");
	}
	return 0;
} 
时间: 2024-10-14 22:27:20

poj 4045 Power Station dfs求树上最小距离和的相关文章

poj 4045 Power Station(初涉树形dp)

http://poj.org/problem?id=4045 大致题意:有n个村庄,求将发电站建在哪一个村庄使得花费最少.这是一个无向无环图.简化一下就是求一个节点使它到其他所有节点的距离和最小. 起初一直在向最短路上靠,但因为节点和边数太大,必定TLE.然后无比强大的啸神随便写了两个dfs就过掉了,简直膜拜.赛后搜了搜题解,发现这是道树形dp.sad,真的要好好刷dp了. 大体思路是将这个无向无环图看做一个树,我们就在这个树上进行动态规划.首先先随便拿一个节点看做根节点(假设节点1),计算出它

poj 4022 ASCII Area dfs求二维面积

题意: 给一个有'/','\','.'组成的二维字符数组,求图中'/'和'\'组成的面积有多大. 分析: 每个'/'和'\'的格每个贡献1/2的面积,每个多边形内部的'.'贡献1的面积,关键是求多边形内部的'.'有多少个.一开始往上下左右4个方向搜wa了,原来内部的点可以斜着扩展,比如/./这种情况.但斜着搜要注意避免从多边形内部跑到外部的情况,比如题目中给的sample. 代码: //poj 4022 //sep9 #include <iostream> using namespace st

poj 2406 Power Strings(KMP求循环次数)

题目链接:http://poj.org/problem?id=2406 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, e

POJ 2406 Power Strings (求字符串循环节出现的次数)

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 44217   Accepted: 18449 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "

POJ 1459 Power Network(多源点/汇点最大流问题)

题目链接:http://poj.org/problem?id=1459 题目: Description A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied with an amount s(u) >= 0 of power, may produce an amount 0

poj 1459 Power Network【建立超级源点,超级汇点】

Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 25514   Accepted: 13287 Description A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied

POJ 1459 Power Network (网络流最大流基础 多源点多汇点 Edmonds_Karp算法)

Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 24056   Accepted: 12564 Description A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied

poj 1459 Power Network (dinic)

Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 23059   Accepted: 12072 Description A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied

POJ 1699 Best Sequence (DFS+预处理)

题意:看那张图就一清二楚了吧, N个序列首位相连(相同的序列部分),得到最短的总序列. 题目链接:http://poj.org/problem?id=1699 ~~~~ 思路就是:将N个序列首尾相连能重合的长度求粗来.然后DFS枚举每种首尾相连的情况. #include<cstdio> #include<cstring> #include<algorithm> #define N 22 #define INF 0x7fffffff using namespace std