fzu 2038 Another Postman 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

思路:

n很大,常规方法会TLE。

可以看出,每条路经过次等于一边的定点数乘另一边的顶点数。

用递归来求两边顶点数

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;

typedef long long LL;
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e5 + 10;
int t, n;

struct Node {
    int next, val;
    LL cnt;
    Node(int a, int b) {
        next = a;
        val = b;
        cnt = 0;
    }
};

vector<Node> G[MAXN];
bool vis[MAXN];

int DFS(int v) {
    vis[v] = true;
    int r = 0;
    for (int i = 0; i < G[v].size(); ++i) {
        int& u = G[v][i].next;
        if (vis[u]) continue;
        int temp = DFS(u);
        G[v][i].cnt = (LL)(n - temp) * temp;
        r += temp;
    }
    return ++r;
}

LL Solve() {
    memset(vis, 0, sizeof(vis));
    DFS(0);
    LL ans = 0;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < G[i].size(); ++j) {
            ans += LL(G[i][j].cnt * G[i][j].val);
        }
    }
    return ans << 1;
}

int main() {
#ifdef NIGHT_13
    freopen("in.txt", "r", stdin);
#endif
    scanf("%d", &t);
    int cas = 0;
    while (t--) {
        scanf("%d", &n);

        for (int i = 0; i < n; ++i) {
            G[i].clear();
        }

        for (int i = 1, a, b, c; i < n; ++i) {
            scanf("%d%d%d", &a, &b, &c);
            G[a].push_back(Node(b, c));
            G[b].push_back(Node(a, c));
        }
        printf("Case %d: %I64d\n", ++cas, Solve());
    }
    return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值