fzu 2038图的遍历,递归求解

Problem 2038 Another Postman Problem

Accept: 219    Submit: 738
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

160 1 11 2 12 3 11 4 11 5 1

Sample Output

Case 1: 56

Source

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

思路来自::http://www.tuicool.com/articles/VvmuUv2

题目大意:给出n, 然后给出n - 1条边,保证图联通,计算每个点到其他所有点的路径总和的和。

解题思路:一开始想到Floyd算法求出所有点点之间的最短路,但是o(n^3)的复杂度太高了;后来想到说用枚举起点后直接用BFS去计算距离,可是o(n^2)还是超时。后来灵光一闪,发现可以枚举每条边走过的次数,以为图肯定为无环图,所以剪断当前边后,可以将图上的点分为两个集合,然后走过的次数就是两个集合个数的乘积。然后递归遍历每个点的每条边。



#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 2000000000
#define EPS 1e-6
typedef long long LL;
const double PI = acos(-1.0);
//#pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x)
{
    return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
const int N=100005;
typedef long long ll;
struct node
{
    int next;
    long long val;
    long long size;
    node()
    {
        next=0,val=size=0;
    }
    node(int next,long long val)
    {
        this->next=next;
        this->val=val;
        this->size=0;
    }
};
int vis[N];
long long n;
std::vector<node> v[N];
void init()
{
    scanf("%I64d",&n);
    for(int i=0;i<n;i++)
        v[i].clear();
    memset(vis,0,sizeof(vis));
    int a,b;
    long long c;
    for(int i=1;i<n;i++)
    {
        scanf("%d%d%I64d",&a,&b,&c);
        v[a].push_back(node(b,c));
        v[b].push_back(node(a,c));
    }
}
long long dfs(int s)
{
    long long cnt=0;
    vis[s]=1;
    for(int i=0;i<v[s].size();i++)
    {
        int u=v[s][i].next;
        if(vis[u])
            continue;
        long long t=dfs(u);
        v[s][i].size=(n-t)*t;
        cnt+=t;
    }
    return cnt+1;
}
long long solve()
{
    dfs(0);
    long long ans=0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<v[i].size();j++)
        {
            ans+=v[i][j].val*v[i][j].size;
        }
    }
    return ans*2;
}
int cnt=1;
int main()
{
#ifdef DeBUGs
    freopen("//home//amb//桌面//1.in", "r", stdin);
#endif
    int T;
    scanf("%d",&T);
    while(T--)
    {
        init();
        printf("Case %d: %I64d\n",cnt++,solve());
    }

    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值