FZU - 2038 -E - Another Postman Problem (思维+递归+回溯)

Posted staceyacm

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了FZU - 2038 -E - Another Postman Problem (思维+递归+回溯)相关的知识,希望对你有一定的参考价值。

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

如何去计算某一条边的贡献值,做法是2×子树的节点数×(n-节点数)×权值,其余就是递归回溯的过程,注意每次清空vector
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath>

const int maxn=1e5+5;
typedef long long ll;
using namespace std;
struct node
{
    int v,w;
};

vector<node>vec[maxn];
int vis[maxn];
ll ans;
int n;
ll dfs(int x)
{
    vis[x]=1;
    ll cnt=1;
    ll s;
    for(int t=0;t<vec[x].size();t++)
    {
        node next;
        next=vec[x][t];
        if(vis[next.v]==0)
        {
        s=dfs(next.v);
        ans+=next.w*2*(n-s)*s;
        cnt+=s;
        }
    }
    return cnt;
}

int main()
{
    int T;
    cin>>T;
    int cnt=1;
    while(T--)
    {
        memset(vis,0,sizeof(vis));
        scanf("%d",&n);
        for(int t=0;t<n;t++)
        {
            vec[t].clear();
        }
        int u;
        ans=0;
        node s;
        int vv;
        for(int t=0;t<n-1;t++)
        {
           scanf("%d%d%d",&u,&s.v,&s.w);
           vec[u].push_back(s);
           vv=s.v;
           s.v=u;
           vec[vv].push_back(s);
        }
        dfs(0);
        printf("Case %d: %I64d\n",cnt++,ans);
        
    }
    return 0;
}

 

以上是关于FZU - 2038 -E - Another Postman Problem (思维+递归+回溯)的主要内容,如果未能解决你的问题,请参考以下文章

FZU 2221 RunningMan(跑男)

[codeforces]Round #538 (Div. 2) F. Please, another Queries on Array?

Lucas组合数定理组合-FZU 2020

FZU 2150 Fire Game(点火游戏)

bzoj2038-小z的袜子莫队算法

Just another Robbery(概率&01背包)