POJ 1849 Two【树的直径+树的遍历】

55 篇文章 1 订阅
12 篇文章 0 订阅

Two
Time Limit: 1000MS Memory Limit: 30000K
Total 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 2
1 2 1
2 3 2
3 4 2
4 5 1

Sample Output

6

Source


原题链接: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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值