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;
    }
  • 相关阅读:
    20140630 科技脉搏-互联网精神之“我不是为了输赢,我就是认真”
    iOS 获取本地视频的缩略图
    iOS App与iTunes文件传输的方法和对iOS App文件结构的说明
    罗振宇自媒体品牌“罗辑思维”估值1亿背后:媒体通往社群之路
    20140622 科技脉搏 -互联网思维之“一群人团结起来占其他人便宜”
    20140616 科技脉搏 -最大颠覆来自创业公司与边缘产业
    关于流媒体(m3u8)的下载与播放
    20140608 科技脉搏 -下半身需求是人类共同需求,有多少人就有多大市场
    IOS遍历未知对象属性、函数
    iOS中使用 Reachability 检测网络
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/10841090.html
Copyright © 2011-2022 走看看