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 ...
随机推荐
- Android VideoView未解决,动态读取权限、BottomNavigationView的用法
昨天想写的,但是因为Video的毛病,是真找不出为啥了.百度也没用,学长也不清楚. 百度了那么久,大概得出结论,电脑的视频是不能用它来播放的... ..经过两天的奋斗,我居然搞定了,我的视频终于出来了 ...
- IDEA新增类的快捷键
1.选中新建类所在包,鼠标右击.然后依次点击New——>Java Class就可以弹出输入类名的弹出框了. 2.选中新建类所在包,按下alt+insert然后选择弹出框中的Jav ...
- MySQL8.0.20安装配置+用Navicat连接详细教程(win10,Navicat15)
MySQL 是最流行的关系型数据库管理系统,在 WEB 应用方面 MySQL 是最好的 RDBMS(Relational Database Management System:关系数据库管理系统)应用 ...
- python深挖65万人的明星贴吧,探究上万个帖子的秘密
前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 最近一直在关注百度明星吧,发现很多有趣的帖子,于是我就想用python把这 ...
- 微信小程序开发着工具获取和更新newticket
newticket是微信开发者工具和微信后台交互的凭证.大多数工具的操作都是需要newticket. 如何获取newticket? 打开开发者工具,依次点击菜单设置->通用设置->代理,使 ...
- 关于在Visual Studio 2019预览版中的用户体验和界面的变化
原文地址:https://blogs.msdn.microsoft.com/visualstudio/2018/11/12/a-preview-of-ux-and-ui-changes-in-visu ...
- C#LeetCode刷题之#707-设计链表(Design Linked List)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4118 访问. 设计链表的实现.您可以选择使用单链表或双链表.单链 ...
- Vue 自定义VueRouter-简版
主要是思路,自己定义组件的时候可以借鉴 Vue-router的 类图 name options: ==> 记录构造函数中传入的对象,在 new VueRouter的时候传了一个对象( route ...
- 关于java中jdk的环境变量配置
关于java中jdk的环境变量配置 烦死人,在网上找了很长时间.最终找到了一个方法!现在将其总结帮助后来人. 方法/步骤 1 下载好jdk,并按照提示一步步安装,最后记下jdk所在的安装位置,这里 ...
- 深度优先搜索(dfs)与出题感想
在3月23号的广度优先搜索(bfs)博客里,我有提到写一篇深搜博客,今天来把这个坑填上. 第一部分:深度优先搜索(dfs) 以上来自百度百科. 简单来说,深度优先搜索算法就是——穷举法,即枚举所有情况 ...