[NOI2003]逃学的小孩

题目描述

Chris家的电话铃响起了,里面传出了Chris的老师焦急的声音:“喂,是Chris的家长吗?你们的孩子又没来上课,不想参加考试了吗?”一听说要考试,Chris的父母就心急如焚,他们决定在尽量短的时间内找到Chris。他们告诉Chris的老师:“根据以往的经验,Chris现在必然躲在朋友Shermie或Yashiro家里偷玩《拳皇》游戏。现在,我们就从家出发去找Chris,一但找到,我们立刻给您打电话。”说完砰的一声把电话挂了。

Chris居住的城市由N个居住点和若干条连接居住点的双向街道组成,经过街道x需花费Tx分钟。可以保证,任两个居住点间有且仅有一条通路。Chris家在点C,Shermie和Yashiro分别住在点A和点B。Chris的老师和Chris的父母都有城市地图,但Chris的父母知道点A、B、C的具体位置而Chris的老师不知。

为了尽快找到Chris,Chris的父母会遵守以下两条规则:

  1. 如果A距离C比B距离C近,那么Chris的父母先去Shermie家寻找Chris,如果找不到,Chris的父母再去Yashiro家;反之亦然。
  2. Chris的父母总沿着两点间唯一的通路行走。

显然,Chris的老师知道Chris的父母在寻找Chris的过程中会遵守以上两条规则,但由于他并不知道A,B,C的具体位置,所以现在他希望你告诉他,最坏情况下Chris的父母要耗费多长时间才能找到Chris?

输入输出格式

输入格式:

输入文件第一行是两个整数N(3 ≤ N ≤ 200000)和M,分别表示居住点总数和街道总数。

以下M行,每行给出一条街道的信息。第i+1行包含整数Ui、Vi、Ti(1≤Ui, Vi ≤ N,1 ≤ Ti ≤ 1000000000),表示街道i连接居住点Ui和Vi,并且经过街道i需花费Ti分钟。街道信息不会重复给出。

输出格式:

输出文件仅包含整数T,即最坏情况下Chris的父母需要花费T分钟才能找到Chris。

此题很容易看出是要先求树的直径,然后的操作我一开始的思路有些错误。

一开始我的想法是在直径上找一个点作为起点,在dfs的过程中记录下直径的路径和权值road,

把road数组前缀和化,于是答案就是lenth+max{min(lenth-road[i],road[i])};

但是在WA掉以后,我发现起点不在直径上也许会更好,于是我们用SPFA求出,每个点到直径两端的距离

那么答案就是lenth+max{min(dis[0][i],dis[1][i])};

实现如下:

#include <algorithm>
#include <iostream>
#include <cmath>
#include <cstring>
#include <map>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
using namespace std;
typedef long long ll;
inline ll read()
{
    register ll p(1),a(0);register char ch=getchar();
    while((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
    if(ch=='-') p=-1,ch=getchar();
    while(ch>='0'&&ch<='9') a=a*10+ch-48,ch=getchar();
    return a*p;
}
const int N=200010;
int n,m,u,v,head[N],cnt=0,zq,zz,len=0,book[N];
ll ans=0,w,maxn=0,road[N],dis[2][N];
struct EDGE{int nxt,to;ll val;}e[N<<1];
void add(int u,int v,ll w){e[++cnt]=(EDGE){head[u],v,w};head[u]=cnt;}
void dfs(int u,int fa,ll siz)
{
    if(siz>maxn){maxn=siz;zq=u;}
    for(int i=head[u],v=e[i].to;i;i=e[i].nxt,v=e[i].to) if(fa!=v) dfs(v,u,siz+e[i].val);
}
queue<int> Q;
void SPFA(int u,int pre)
{
    memset(dis[pre],0x3f,sizeof(dis[pre]));
    memset(book,0,sizeof(book));
    Q.push(u);dis[pre][u]=0;book[u]=1;
    while(!Q.empty())
    {
        u=Q.front();Q.pop();book[u]=0;
        for(int i=head[u],v=e[i].to;i;i=e[i].nxt,v=e[i].to)
            if(dis[pre][v]>dis[pre][u]+e[i].val)
            {
                dis[pre][v]=dis[pre][u]+e[i].val;
                if(!book[v]) Q.push(v);
            }
    }
}
int main()
{
    // freopen("input","r",stdin);
    // freopen("output","w",stdout);
    n=read(),m=read();
    for(int i=1;i<=m;i++)
    {
        u=read(),v=read(),w=read();
        add(u,v,w);add(v,u,w);
    }
    dfs(1,-1,0);zz=zq,maxn=0;
    dfs(zq,-1,0);
    SPFA(zz,0);SPFA(zq,1);
    for(int i=1;i<=n;i++) ans=max(ans,min(dis[0][i],dis[1][i]));
    printf("%lld
",ans+maxn);
    return 0;
}
原文地址:https://www.cnblogs.com/cold-cold/p/10035649.html