JLUCPC
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1346 Accepted Submission(s): 397
Problem Description
Dr. Skywind and Dr. Walkoncloud are planning to hold the annual JLU Collegiate Programming Contest. The contest was always held in the college of software in the past. However, they changed their minds and decided to find the most convenient location from all colleges this year.
Each college in JLU is located in one of the N (1 <= N <= 100,000) different locations (labeled as 1 to N) connected by N-1 roads. Any two colleges are reachable to each other. The Contest can be held at any one of these N colleges. Moreover, Road i connects college A_i and B_i (1 <= A_i <=N; 1 <= B_i <= N) and has length L_i (1 <= L_i <= 1,000). College i has T_i (0 <= T_i <= 1,000) teams participating in the contest.
When choosing the college to hold the Contest, Dr. Skywind wishes to minimize the inconvenience of the chosen location. The inconvenience of choosing college P is the sum of the distance that all teams need to reach college P (i.e., if the distance from college i to college P is 20, then the travel distance is T_i*20). Please help Dr. Skywind and Dr. Walkoncloud to choose the most convenient location for the contest.
Each college in JLU is located in one of the N (1 <= N <= 100,000) different locations (labeled as 1 to N) connected by N-1 roads. Any two colleges are reachable to each other. The Contest can be held at any one of these N colleges. Moreover, Road i connects college A_i and B_i (1 <= A_i <=N; 1 <= B_i <= N) and has length L_i (1 <= L_i <= 1,000). College i has T_i (0 <= T_i <= 1,000) teams participating in the contest.
When choosing the college to hold the Contest, Dr. Skywind wishes to minimize the inconvenience of the chosen location. The inconvenience of choosing college P is the sum of the distance that all teams need to reach college P (i.e., if the distance from college i to college P is 20, then the travel distance is T_i*20). Please help Dr. Skywind and Dr. Walkoncloud to choose the most convenient location for the contest.
Input
There are multiple test cases. For each case, the first line contains a single integer N, indicating the number of colleges. The next N lines describes T_1 to T_n. Then, each of the last N-1 lines will contain 3 integers, namely A_i, B_i and L_i.
Output
For each case, output the minimum inconvenience possible
Sample Input
3
1
1
2
1 2 2
2 3 1
4
100
1
1
1
1 2 1
2 3 1
2 4 1
Sample Output
4
5
Source
题意:
有n个点,每个点有相应的人数,n-1条带权边,从a点的所有人走到b点的花费是a点的人数*边权值,问所有人在哪个点集合能够使花费最小求最小花费。
代码:
//先dfs求出以1为根节点使每个点后代到这个点集合的花费sum以及这个点及其后代共有几个人cnt //然后枚举每个点i作为集合点(根节点)时他的花费就是sum[i]+他的父节点减去从i来的人的花费+ //父节点总人数减去从i来的人数*边权值。并且更新最小花费和sum[i],cnt[i]. #include<iostream> #include<cstdio> #include<cstring> using namespace std; typedef long long ll; const int maxn=100009; int n,val[maxn],head[maxn],cnt[maxn],tol; ll sum[maxn],ans; struct node{ int to,w,next; }nodes[maxn*2]; void Add(int x,int y,int z){ nodes[tol].to=y; nodes[tol].w=z; nodes[tol].next=head[x]; head[x]=tol++; } void dfs(int x,int fa){ cnt[x]=val[x];sum[x]=0; for(int i=head[x];i!=-1;i=nodes[i].next){ int y=nodes[i].to; if(y==fa) continue; dfs(y,x); cnt[x]+=cnt[y]; sum[x]+=(sum[y]+1LL*cnt[y]*nodes[i].w); } } void dfs1(int x,int fa){ for(int i=head[x];i!=-1;i=nodes[i].next){ int y=nodes[i].to; if(y==fa) continue; sum[y]=sum[y]+sum[x]-sum[y]-1LL*cnt[y]*nodes[i].w+1LL*(cnt[x]-cnt[y])*nodes[i].w; cnt[y]+=(cnt[x]-cnt[y]); //cout<<y<<" "<<sum[y]<<endl; //ans=min(ans,sum[y]+sum[x]-sum[y]-1LL*cnt[y]*nodes[i].w+1LL*(cnt[x]-cnt[y])*nodes[i].w); ans=min(ans,sum[y]); dfs1(y,x); } } int main() { while(scanf("%d",&n)==1){ memset(val,0,sizeof(val)); memset(head,-1,sizeof(head)); tol=0; for(int i=1;i<=n;i++) scanf("%d",&val[i]); for(int i=1;i<n;i++){ int x,y,z; scanf("%d%d%d",&x,&y,&z); Add(x,y,z); Add(y,x,z); } dfs(1,0); ans=sum[1]; dfs1(1,0); printf("%I64d ",ans); } return 0; }