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+;
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]=;
ll cnt=;
ll s;
for(int t=;t<vec[x].size();t++)
{
node next;
next=vec[x][t];
if(vis[next.v]==)
{
s=dfs(next.v);
ans+=next.w**(n-s)*s;
cnt+=s;
}
}
return cnt;
} int main()
{
int T;
cin>>T;
int cnt=;
while(T--)
{
memset(vis,,sizeof(vis));
scanf("%d",&n);
for(int t=;t<n;t++)
{
vec[t].clear();
}
int u;
ans=;
node s;
int vv;
for(int t=;t<n-;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();
printf("Case %d: %I64d\n",cnt++,ans); }
return ;
}

FZU - 2038 -E - Another Postman Problem (思维+递归+回溯)的更多相关文章

  1. hdoj--1016--Prime Ring Problem(递归回溯)

    Prime Ring Problem                                                                             Time ...

  2. HITOJ 2739 The Chinese Postman Problem(欧拉回路+最小费用流)

    The Chinese Postman Problem My Tags   (Edit)   Source : bin3   Time limit : 1 sec   Memory limit : 6 ...

  3. 递归回溯 UVa140 Bandwidth宽带

    本题题意:寻找一个排列,在此排序中,带宽的长度最小(带宽是指:任意一点v与其距离最远的且与v有边相连的顶点与v的距离的最大值),若有多个,按照字典序输出最小的哪一个. 解题思路: 方法一:由于题目说结 ...

  4. LeetCode || 递归 / 回溯

    呜呜呜 递归好不想写qwq 求“所有情况”这种就递归 17. Letter Combinations of a Phone Number 题意:在九宫格上按数字,输出所有可能的字母组合 Input: ...

  5. 40. 组合总和 II + 递归 + 回溯 + 记录路径

    40. 组合总和 II LeetCode_40 题目描述 题解分析 此题和 39. 组合总和 + 递归 + 回溯 + 存储路径很像,只不过题目修改了一下. 题解的关键是首先将候选数组进行排序,然后记录 ...

  6. HIT 2739 - The Chinese Postman Problem - [带权有向图上的中国邮路问题][最小费用最大流]

    题目链接:http://acm.hit.edu.cn/hoj/problem/view?id=2739 Time limit : 1 sec Memory limit : 64 M A Chinese ...

  7. FZU 2224 An exciting GCD problem(GCD种类预处理+树状数组维护)同hdu5869

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=2224 同hdu5869 //#pragma comment(linker, "/STACK:1024 ...

  8. FZU 2240 Daxia & Suneast's problem

    博弈,$SG$函数,规律,线段树. 这个问题套路很明显,先找求出$SG$函数值是多少,然后异或起来,如果是$0$就后手赢,否则先手赢.修改操作和区间查询的话可以用线段树维护一下区间异或和. 数据那么大 ...

  9. CF990C Bracket Sequences Concatenation Problem 思维 第五道 括号经典处理题目

     Bracket Sequences Concatenation Problem time limit per test 2 seconds memory limit per test 256 meg ...

随机推荐

  1. 使用HttpClient 发送 GET、POST(FormData、Raw)、PUT、Delete请求及文件上传

    httpclient4.3.6 package org.caeit.cloud.dev.util; import java.io.File; import java.io.IOException; i ...

  2. JS 前端框架笔记

    Swiper轮播图插件使用     官网:https://www.swiper.com.cn/     先引用插件的css.js文件     然后找到需要使用的轮播图文件中去把HTML css和JS代 ...

  3. DIFF算法浅析(三)在react中的实现

    在虚拟dom中diff的实现. 分别从4个方面: DIFF抽象概念(概述.时间复杂性分析) 在Vue2中的实现(版本2.6.11.必要性.执行方式) 在React中的实现(版本16.13.1,必要性. ...

  4. Redis服务之集群节点管理

    上一篇博客主要聊了下redis cluster的部署配置,以及使用redis.trib.rb工具所需ruby环境的搭建.使用redis.trib.rb工具创建.查看集群相关信息等,回顾请参考https ...

  5. 栈及其简单应用(二)(python代码)

    一.括号判定 前一篇文章我们介绍了栈的简单应用中,关于括号的判定,但那只是一种括号的判定,下面我们来介绍多种括号混合使用时,如何判断括号左右一一对应. 比如“{}{(}(][”这种情况,需要对一种括号 ...

  6. C#LeetCode刷题之#590-N叉树的后序遍历(N-ary Tree Postorder Traversal)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4092 访问. 给定一个 N 叉树,返回其节点值的后序遍历. 例如 ...

  7. Android 内部存储读写介绍

    内部存储读写 内容介绍 Android系统允许应用程序创建仅能够自身访问的私有文件,文件保存在设备的内部存储器上,在Linux系统下的/data/data//files目录中 Android系统不仅支 ...

  8. YAML简要入门

    这是一篇简单的YAML入门教程,目的是让你知晓什么YAML,以及YAML的基础语法.方便接下来学习如何使用Golang解析YAML.如果想获得更多YAML的知识,请查看http://yaml.org ...

  9. 为什么?为什么?Java处理排序后的数组比没有排序的快?想过没有?

    先看再点赞,给自己一点思考的时间,微信搜索[沉默王二]关注这个有颜值却假装靠才华苟且的程序员.本文 GitHub github.com/itwanger 已收录,里面还有我精心为你准备的一线大厂面试题 ...

  10. 《从缺陷中学习CC++》总结

    从缺陷中学习CC++总结 从本质上来说,这就是一个CC++的错题集.全书中包括63个问题引发的错误,即书名中的缺陷.共分为10章,每一张还有一个比较经典的小结,个人感觉这本书只需要仔细看一下每一章后面 ...