FZU - 2038 -E - Another Postman Problem (思维+递归+回溯)
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 (思维+递归+回溯)的更多相关文章
- hdoj--1016--Prime Ring Problem(递归回溯)
Prime Ring Problem Time ...
- HITOJ 2739 The Chinese Postman Problem(欧拉回路+最小费用流)
The Chinese Postman Problem My Tags (Edit) Source : bin3 Time limit : 1 sec Memory limit : 6 ...
- 递归回溯 UVa140 Bandwidth宽带
本题题意:寻找一个排列,在此排序中,带宽的长度最小(带宽是指:任意一点v与其距离最远的且与v有边相连的顶点与v的距离的最大值),若有多个,按照字典序输出最小的哪一个. 解题思路: 方法一:由于题目说结 ...
- LeetCode || 递归 / 回溯
呜呜呜 递归好不想写qwq 求“所有情况”这种就递归 17. Letter Combinations of a Phone Number 题意:在九宫格上按数字,输出所有可能的字母组合 Input: ...
- 40. 组合总和 II + 递归 + 回溯 + 记录路径
40. 组合总和 II LeetCode_40 题目描述 题解分析 此题和 39. 组合总和 + 递归 + 回溯 + 存储路径很像,只不过题目修改了一下. 题解的关键是首先将候选数组进行排序,然后记录 ...
- 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 ...
- FZU 2224 An exciting GCD problem(GCD种类预处理+树状数组维护)同hdu5869
题目链接:http://acm.fzu.edu.cn/problem.php?pid=2224 同hdu5869 //#pragma comment(linker, "/STACK:1024 ...
- FZU 2240 Daxia & Suneast's problem
博弈,$SG$函数,规律,线段树. 这个问题套路很明显,先找求出$SG$函数值是多少,然后异或起来,如果是$0$就后手赢,否则先手赢.修改操作和区间查询的话可以用线段树维护一下区间异或和. 数据那么大 ...
- CF990C Bracket Sequences Concatenation Problem 思维 第五道 括号经典处理题目
Bracket Sequences Concatenation Problem time limit per test 2 seconds memory limit per test 256 meg ...
随机推荐
- UIPickView的简单使用
好记性不如烂笔头,勤做笔记. 摘要: 1.UIPickVIew 几个重要的属性 (1)datePickerMode UIDatePickerModeTime, // Displays hour, mi ...
- 02-java实现单链表
02-手撸链表 本篇是恋上数据结构第一季个人总结 借鉴https://juejin.im/post/6844904001478066183#heading-0 本人git https://github ...
- [深度学习] Pytorch学习(二)—— torch.nn 实践:训练分类器(含多GPU训练CPU加载预测的使用方法)
Learn From: Pytroch 官方Tutorials Pytorch 官方文档 环境:python3.6 CUDA10 pytorch1.3 vscode+jupyter扩展 #%% #%% ...
- C#LeetCode刷题之#88-合并两个有序数组(Merge Sorted Array)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3686 访问. 给定两个有序整数数组 nums1 和 nums2, ...
- X86汇编——计算斐波那契数列程序(详细注释和流程图说明)
X86汇编实现斐波那契数列 程序说明: 输入斐波那契数列的项数, 然后依次输出斐波那契数列, 输入的项数小于256且为数字, 计算的项数不能超过2^16次方, 输入失败是 不会回显数字 因为存结果是A ...
- CTFhub-WEB前置-http协议闯关
前情提要: 在渗透学习过程中,web的基础知识很重要,在这里通过long long ago之前学习的http基础,并结合网上的CTFhub--WEB前置之http协议闯关,对web基础知识进行加固并查 ...
- 初始化vtable
在InstanceKlass::link_class_impl()方法中完成方法连接后会继续初始化vtable与itable,之前已经介绍过vtable与itable,并且在类解析过程中已经完成了大小 ...
- 《从缺陷中学习CC++》总结
从缺陷中学习CC++总结 从本质上来说,这就是一个CC++的错题集.全书中包括63个问题引发的错误,即书名中的缺陷.共分为10章,每一张还有一个比较经典的小结,个人感觉这本书只需要仔细看一下每一章后面 ...
- 已废弃_CSDN慕零的黑夜-头条-第一期(必问)[导读:]1.CSDN必问赏金流向何方 2.CSDN必问偷偷做的手脚 3.CSDN必问靠谱吗 4.关于钱于回答的平衡问题:一美元拍卖骗局qq3461896724
[本文有已知的链接差错,懒得改了] 本期是关于CSDN 必问 (biwen.csdn.net)的内容,欢迎评论文末,文中插入有 小姐姐 img(附py代码,1.49G) + coding资料 哟~~~ ...
- 4 IDEA环境应用
第4章 IDEA环境应用 spark shell仅在测试和验证我们的程序时使用的较多,在生产环境中,通常会在IDE中编制程序,然后打成jar包,然后提交到集群,最常用的是创建一个Maven项目,利用M ...