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. 

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. 

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;}