zoukankan      html  css  js  c++  java
  • FZU

    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
    ",cnt++,ans);
            
        }
        return 0;
    }
  • 相关阅读:
    del
    sublime右键菜单,anaconda设置
    全概率公式、贝叶斯公式推导过程
    python数据结构之链表(一)
    关于panda中dataframe的与&运算*(stackoverflow高票答案)
    转载:python 日期,季度,年份
    商业模式画布
    互联网思维
    互联网思维
    战略品牌管理_1
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/10841090.html
Copyright © 2011-2022 走看看