POJ 1849 Two【树的直径+树的遍历】
来源:互联网 发布:三防漆涂覆机编程 编辑:程序博客网 时间:2024/05/17 07:22
Two
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 1644 Accepted: 870
Description
The city consists of intersections and streets that connect them.
Heavy snow covered the city so the mayor Milan gave to the winter-service a list of streets that have to be cleaned of snow. These streets are chosen such that the number of streets is as small as possible but still every two intersections to be connected i.e. between every two intersections there will be exactly one path. The winter service consists of two snow plovers and two drivers, Mirko and Slavko, and their starting position is on one of the intersections.
The snow plover burns one liter of fuel per meter (even if it is driving through a street that has already been cleared of snow) and it has to clean all streets from the list in such order so the total fuel spent is minimal. When all the streets are cleared of snow, the snow plovers are parked on the last intersection they visited. Mirko and Slavko don’t have to finish their plowing on the same intersection.
Write a program that calculates the total amount of fuel that the snow plovers will spend.
Heavy snow covered the city so the mayor Milan gave to the winter-service a list of streets that have to be cleaned of snow. These streets are chosen such that the number of streets is as small as possible but still every two intersections to be connected i.e. between every two intersections there will be exactly one path. The winter service consists of two snow plovers and two drivers, Mirko and Slavko, and their starting position is on one of the intersections.
The snow plover burns one liter of fuel per meter (even if it is driving through a street that has already been cleared of snow) and it has to clean all streets from the list in such order so the total fuel spent is minimal. When all the streets are cleared of snow, the snow plovers are parked on the last intersection they visited. Mirko and Slavko don’t have to finish their plowing on the same intersection.
Write a program that calculates the total amount of fuel that the snow plovers will spend.
Input
The first line of the input contains two integers: N and S, 1 <= N <= 100000, 1 <= S <= N. N is the total number of intersections; S is ordinal number of the snow plovers starting intersection. Intersections are marked with numbers 1...N.
Each of the next N-1 lines contains three integers: A, B and C, meaning that intersections A and B are directly connected by a street and that street's length is C meters, 1 <= C <= 1000.
Each of the next N-1 lines contains three integers: A, B and C, meaning that intersections A and B are directly connected by a street and that street's length is C meters, 1 <= C <= 1000.
Output
Write to the output the minimal amount of fuel needed to clean all streets.
Sample Input
5 21 2 12 3 23 4 24 5 1
Sample Output
6
Source
Croatia OI 2002 national – second day, seniors
原题链接:http://poj.org/problem?id=1849
题意:两辆清雪车从同一点出发,要清理所有路上的积雪,问耗油量(路程和)最少是多少。不需要回到起点。
要使路程最近,若起点在树的直径上,则两辆车往不同的方向走,直径上的边只用走一遍,其他的要走两遍。
若起点不在直径上,则两人一起走到直径上,再往不同的方向走。
综上:路径和为直径+2*非直径.
即:2*sum-d.(sum为路径和,d为直径)
参考博客:http://blog.csdn.net/u013480600/article/details/40710425
里面还分析了其它情况.
AC代码:
/** * 行有余力,则来刷题! * 博客链接:http://blog.csdn.net/hurmishine * 个人博客网站:http://wuyunfeng.cn/*/#include <iostream>#include <cstdio>#include <cstring>#include <vector>using namespace std;const int maxn=100000+5;vector<pair<int,int> >G[maxn];int n,m;int d;int p;bool vis[maxn];void DFS(int u,int len){ vis[u]=true; if(len>d) { d=len; p=u; } for(int i=0; i<G[u].size(); i++) { int v=G[u][i].first; int w=G[u][i].second; if(!vis[v]) { DFS(v,len+w); } }}int main(){ //freopen("C:\\Documents and Settings\\Administrator\\桌面\\data.txt","r",stdin); while(cin>>n>>m) { for(int i=0; i<=n; i++) { G[i].clear(); } int u,v,w; int sum=0; for(int i=1; i<n; i++) { scanf("%d%d%d",&u,&v,&w); G[u].push_back(make_pair(v,w)); G[v].push_back(make_pair(u,w)); sum+=w; } memset(vis,false,sizeof(vis)); d=0; DFS(m,0); //d=0; memset(vis,false,sizeof(vis)); DFS(p,0); cout<<2*sum-d<<endl; } return 0;}
阅读全文
0 0
- POJ 1849 Two【树的直径+树的遍历】
- POJ 1849 Two(树的直径)
- POJ 1849 Two///树的直径
- POJ 1849 Two 树的直径
- POJ 1849 Two(求树的直径)
- poj-1839-Two 树形dp/树的直径
- POJ 1849 Two(树的直径--树形DP)(好题)
- POJ 1849 Two (树形dp 树的直径 两种方法)
- poj 1849 树的直径+思维
- poj 1985 树的直径
- 树的直径 poj 2631
- POJ-----1985树的直径
- POJ-----2631树的直径
- POJ 1849 Two(遍历树)
- [POJ-2631] 求树的直径
- POJ 1985 Cow Marathon 树的直径
- POJ 1985 Cow Marathon 树的直径
- 【树的直径】 POJ 1985 Cow Marathon
- Linux中查看各文件夹大小命令du -h --max-depth=1
- 简单获取最近6个月的方法
- win10服务主机占用cpu使用率该怎么办?win10服务主机cpu高的处理方法
- 剑指offer 46---实现1+2+3...+n,要求不能使用乘除法、循环、条件判断、选择相关的关键字
- 谷歌官方MVP Demo解读
- POJ 1849 Two【树的直径+树的遍历】
- spring factory-bean用法
- PythonNewHere
- Vector用数组下标访问的条件
- 坦克类游戏的制作之路
- oracle按某两个字段的两个值排序
- 1、驱动应该怎么学
- JavaScript 判断两个数组是否相等 或者 判断一个数组中是否有重复的元素
- hibernate_级联操作、关联关系映射