fzu 2038图的遍历,递归求解
2014-03-18 21:56
225 查看
Problem 2038 Another Postman Problem
Accept: 219 Submit: 738
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.
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.
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.
160 1 11 2 12 3 11 4 11 5 1
Case 1: 56
2011年全国大学生程序设计邀请赛(福州)
思路来自::http://www.tuicool.com/articles/VvmuUv2
题目大意:给出n, 然后给出n - 1条边,保证图联通,计算每个点到其他所有点的路径总和的和。
解题思路:一开始想到Floyd算法求出所有点点之间的最短路,但是o(n^3)的复杂度太高了;后来想到说用枚举起点后直接用BFS去计算距离,可是o(n^2)还是超时。后来灵光一闪,发现可以枚举每条边走过的次数,以为图肯定为无环图,所以剪断当前边后,可以将图上的点分为两个集合,然后走过的次数就是两个集合个数的乘积。然后递归遍历每个点的每条边。
Accept: 219 Submit: 738
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 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.
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.
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
160 1 11 2 12 3 11 4 11 5 1
Sample Output
Case 1: 56
Source
2011年全国大学生程序设计邀请赛(福州)思路来自::http://www.tuicool.com/articles/VvmuUv2
题目大意:给出n, 然后给出n - 1条边,保证图联通,计算每个点到其他所有点的路径总和的和。
解题思路:一开始想到Floyd算法求出所有点点之间的最短路,但是o(n^3)的复杂度太高了;后来想到说用枚举起点后直接用BFS去计算距离,可是o(n^2)还是超时。后来灵光一闪,发现可以枚举每条边走过的次数,以为图肯定为无环图,所以剪断当前边后,可以将图上的点分为两个集合,然后走过的次数就是两个集合个数的乘积。然后递归遍历每个点的每条边。
#define DeBUG #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <vector> #include <stack> #include <queue> #include <string> #include <set> #include <sstream> #include <map> #include <bitset> using namespace std ; #define zero {0} #define INF 2000000000 #define EPS 1e-6 typedef long long LL; const double PI = acos(-1.0); //#pragma comment(linker, "/STACK:102400000,102400000") inline int sgn(double x) { return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1); } const int N=100005; typedef long long ll; struct node { int next; long long val; long long size; node() { next=0,val=size=0; } node(int next,long long val) { this->next=next; this->val=val; this->size=0; } }; int vis ; long long n; std::vector<node> v ; void init() { scanf("%I64d",&n); for(int i=0;i<n;i++) v[i].clear(); memset(vis,0,sizeof(vis)); int a,b; long long c; for(int i=1;i<n;i++) { scanf("%d%d%I64d",&a,&b,&c); v[a].push_back(node(b,c)); v[b].push_back(node(a,c)); } } long long dfs(int s) { long long cnt=0; vis[s]=1; for(int i=0;i<v[s].size();i++) { int u=v[s][i].next; if(vis[u]) continue; long long t=dfs(u); v[s][i].size=(n-t)*t; cnt+=t; } return cnt+1; } long long solve() { dfs(0); long long ans=0; for(int i=0;i<n;i++) { for(int j=0;j<v[i].size();j++) { ans+=v[i][j].val*v[i][j].size; } } return ans*2; } int cnt=1; int main() { #ifdef DeBUGs freopen("//home//amb//桌面//1.in", "r", stdin); #endif int T; scanf("%d",&T); while(T--) { init(); printf("Case %d: %I64d\n",cnt++,solve()); } return 0; }
相关文章推荐
- 使用 Mono Develop 调试 U3D 脚本
- struts2多文件上传(修改名称、压缩图片、删除图片)
- 学习OpenCV范例(十三)——图像金字塔
- ContentProvider,SQLiteOpenHelper,SQLiteDatabase三者之间的区别和联系
- MVC 在控制器中获取某个视图动态的HTML代码
- 编程回忆之Android回忆(AnimationDrawable的使用)
- matlab 调试
- 说话内容做得好无非有几点,信息量大、逻辑严密、表达趣味。还有,学会闭嘴。文自/宗宁
- USACO Section 1.5 Superprime Rib
- 动态规划小结
- Eclipse安装ADT失败解决办法
- iframe去边框,无边框,使用大全
- HighCharts之2D折线图
- python学习笔记
- USACO Section 1.5 Prime Palindromes
- HighCharts之2D折线图
- Struts2—整合Spring
- struts 标签1
- 浙大PAT 3-06. 表达式转换 (解题思路)
- 算术基本定理,又称为正整数的唯一分解定理