FZU 2038 Another Postman Problem【思维】
来源:互联网 发布:淘宝京剧服装 编辑:程序博客网 时间:2024/06/05 19:28
Accept: 331 Submit: 1103
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
Sample Output
Source
2011年全国大学生程序设计邀请赛(福州)思路:对于每一条边,其一共走过的次数是这样来计算的:2*终点子树中节点的个数tmp*(n-tmp)*权值。
代码实现,图的遍历:dfs-求子树节点的个数。
AC代码:
#include<stdio.h>#include<string.h>#include<vector>using namespace std;#define ll __int64struct zuobiao{ int v,w;}now;ll ans;int vis[100010],n;vector<zuobiao >mp[100010];ll dfs(int u){ vis[u]=1; ll cont=1; ll tmp; for(int i=0;i<mp[u].size();i++) { int v=mp[u][i].v; if(vis[v]==0) { tmp=dfs(v); ans+=2*mp[u][i].w*tmp*(n-tmp); cont+=tmp; } } return cont;}int main(){ int t; int kase=0; scanf("%d",&t); while(t--) { memset(vis,0,sizeof(vis)); scanf("%d",&n); for(int i=0;i<n;i++) { mp[i].clear(); } for(int i=0;i<n-1;i++) { int u,v,w; scanf("%d%d%d",&u,&v,&w); now.v=v; now.w=w; mp[u].push_back(now); now.v=u; now.w=w; mp[v].push_back(now); } ans=0; dfs(0); printf("Case %d: %I64d\n",++kase,ans); }}
- FZU 2038 Another Postman Problem【思维】
- FZU - 2038 Another Postman Problem
- fzu 2038 Another Postman Problem(递归求解)
- fzu 2038 Another Postman Problem(dfs)
- fzu 2038 Another Postman Problem 递归
- fzu2038 Another Postman Problem
- Another Easy Problem FZU
- fzu 1753 Another Easy Problem
- FZU2038 Another Postman Problem 树状DP
- Another Postman Problem (邻接表+DFS)
- FZU2011年全国大学生程序设计邀请赛(福州) 2038Another Postman Problem
- Fzu 2058 Another a+b【离散化+思维】
- FZU 1753:Another Easy Problem _素因子分解
- FZU 1753-Another Easy Problem(求多个组合数的最大公约数)
- Another a+b FZU
- 【解题报告】fzu 1753 Another Easy Problem - 求150个组合数的最大公约数
- fzu 1753 Another Easy Problem(n!的素因子分解)
- FZU 1753 Another Easy Problem 解题报告(素因子统计)
- 【代码】SQL分页语句
- CSS定位机制之标准文档流
- python基础教程 re模块 学习笔记
- C++重复模板实例的处理
- 《JS权威指南》学习笔记(六):函数
- FZU 2038 Another Postman Problem【思维】
- 20.从上往下打印二叉树
- javascript高级程序设计读书笔记——Array总结
- 快速排序复习
- Android项目构成之AndroidManifest.xml文件简介
- cmd命令大全
- Spring AOP 之 实现切点、切面
- 克隆或复制VMware虚拟机时的网络设置
- 10474 - Where is the Marble?