fzu 2038 Another Postman Problem(dfs)
2013-12-12 14:06
274 查看
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 everyedge 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.
![](http://acm.fzu.edu.cn/image/Problem/1150005.jpg)
![](http://acm.fzu.edu.cn/image/prodesc.gif)
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 andb, 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.
![](http://acm.fzu.edu.cn/image/prodesc.gif)
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.
![](http://acm.fzu.edu.cn/image/prodesc.gif)
Sample Input
160 1 11 2 12 3 11 4 11 5 1
![](http://acm.fzu.edu.cn/image/prodesc.gif)
Sample Output
Case 1: 56
![](http://acm.fzu.edu.cn/image/prodesc.gif)
Source
2011年全国大学生程序设计邀请赛(福州)题意:给定一个n-1边,n个点的图,点都是连通的,求所有点到其他点的距离之和。
思路:问题转换为,每条边要走几次,距离为所有边权值*次数之和,对于每条边走几次,就看如果去除这条边,那么分成的两个图中点数之积(想想为什么)。这样一来只要进行一次dfs即可。
代码:
#include <stdio.h> #include <string.h> const int N = 200005; typedef long long ll; int t, n, first , next , u , v , E, vis ; ll w , time ; void add(int a, int b, int value) { u[E] = a; v[E] = b; w[E] = value; time[E] = 0; next[E] = first[u[E]]; first[u[E]] = E; E ++; } void init() { int a, b, value; memset(vis, 0, sizeof(vis)); E = 0; memset(first, -1, sizeof(first)); scanf("%d", &n); for (int i = 0; i < n - 1; i ++) { scanf("%d%d%d", &a, &b, &value); add(a, b, value); add(b, a, value); } } ll dfs(int u) { ll t = 0; vis[u] = 1; for (int e = first[u]; e != -1; e = next[e]) { if (vis[v[e]]) continue; ll cnt = dfs(v[e]); t += cnt; time[e] = time[e^1] = cnt * (n - cnt); } return t + 1; } void solve() { ll ans = 0; dfs(0); for (int i = 0; i < E; i ++) { ans += time[i] * w[i]; } printf("%lld\n", ans); } int main() { int cas = 0; scanf("%d", &t); while (t--) { init(); printf("Case %d: ", ++cas); solve(); } return 0; }
相关文章推荐
- FZU - 2038 Another Postman Problem
- fzu 2038 Another Postman Problem(递归求解)
- FZU 2038 Another Postman Problem【思维】
- fzu 2038 Another Postman Problem 递归
- fzu2038 Another Postman Problem
- Another Postman Problem (邻接表+DFS)
- Another Easy Problem FZU - 1753唯一分解定理
- FZU 1753:Another Easy Problem _素因子分解
- fzu 1753 Another Easy Problem(n!的素因子分解)
- FZU 1753 Another Easy Problem 解题报告(素因子统计)
- fzu 1753 Another Easy Problem
- 【解题报告】fzu 1753 Another Easy Problem - 求150个组合数的最大公约数
- fzu1753 Another Easy Problem
- FZU 1753-Another Easy Problem(求多个组合数的最大公约数)
- FZU1753 Another Easy Problem【组合数】
- FZU Problem 1920 Left Mouse Button[dfs ||bfs,扫雷游戏]
- FZU Problem 2112 Tickets (dfs 欧拉图啊)
- FZU_1753 Another Easy Problem
- fzu-1753 Another Easy Problem-高速求N!多少个月p
- fzu-1753 Another Easy Problem-快速求N!中有多少个p