FZU 2038 Another Postman Problem【思维】

来源:互联网 发布:淘宝京剧服装 编辑:程序博客网 时间:2024/06/05 19:28

 Problem 2038 Another Postman Problem

Accept: 331    Submit: 1103
Time Limit: 2000 mSec    Memory Limit : 32768 KB

 Problem Description

Chinese Postman Problem is a very famous hard problem in graph theory. The problem is to find a shortest closed path or circuit that visits every edge of a (connected) undirected graph. When the graph has an Eulerian Circuit (a closed walk that covers every edge once), that circuit is an optimal solution.

This problem is another version of Postman Problem. Assume there are n towns and n-1 roads, and there is a unique path between every pair of towns. There are n-1 postmen in every town, and each postman in one town regularly sends mails to one of the other n-1 towns respectively. Now, given the length of each road, you are asked to calculate the total length that all the postmen need to travel in order to send out the mails.

For example, there are six towns in the following picture. The 30 postmen should totally travel 56. The postmen in town 0 should travel 1, 2, 2, 2, 3 respectively, the postmen in town 1 should travel 1, 1, 1, 1, 2 respectively, the postmen in town 2 should travel 1, 1, 2, 2, 2 respectively, the postmen in town 3 should travel 1, 2, 3, 3, 3 respectively, the postmen in town 4 should travel 1, 2, 2, 2, 3 respectively, and the postmen in town 5 should travel 1, 2, 2, 2, 3 respectively. So the total distance is 56.

 Input

The first line of the input contains an integer T(T≤20), indicating the number of test cases. Each case begins with one integer n(n≤100,000), the number of towns. In one case, each of the following n-1 lines describes the length of path between pair a and b, with the format a, b, c(1≤c≤1000), indicating that town a and town b are directly connected by a road of length c. Note that all the n towns are numbered from 0 to n-1.

 Output

For each test case, print a line containing the test case number (beginning with 1) and the total sum of the length that all postmen should travel.

 Sample Input

1
6
0 1 1
1 2 1
2 3 1
1 4 1
1 5 1

 Sample Output

Case 1: 56

 Source

2011年全国大学生程序设计邀请赛(福州)

题目大意:一共n个城市,n-1条边,每个城市都有一个邮递员,问,每个城市的每个邮递员到其他所有城市一共需要的权值和,题目对于样例的解释已经足够清楚,这里就不多啰嗦了。


思路:对于每一条边,其一共走过的次数是这样来计算的:2*终点子树中节点的个数tmp*(n-tmp)*权值。

代码实现,图的遍历:dfs-求子树节点的个数。

AC代码:

#include<stdio.h>#include<string.h>#include<vector>using namespace std;#define ll __int64struct zuobiao{    int v,w;}now;ll ans;int vis[100010],n;vector<zuobiao >mp[100010];ll dfs(int u){    vis[u]=1;    ll cont=1;    ll tmp;    for(int i=0;i<mp[u].size();i++)    {        int v=mp[u][i].v;        if(vis[v]==0)        {            tmp=dfs(v);            ans+=2*mp[u][i].w*tmp*(n-tmp);            cont+=tmp;        }    }    return cont;}int main(){    int t;    int kase=0;    scanf("%d",&t);    while(t--)    {        memset(vis,0,sizeof(vis));        scanf("%d",&n);        for(int i=0;i<n;i++)        {            mp[i].clear();        }        for(int i=0;i<n-1;i++)        {            int u,v,w;            scanf("%d%d%d",&u,&v,&w);            now.v=v;            now.w=w;            mp[u].push_back(now);            now.v=u;            now.w=w;            mp[v].push_back(now);        }        ans=0;        dfs(0);        printf("Case %d: %I64d\n",++kase,ans);    }}













0 0
原创粉丝点击