[BZOJ2100]&&[Usaco2010 Dec]Apple Delivery

描述 Description

Bessie has two crisp red apples to deliver to two of her friends in the herd. Of course, she travels the C (1 <= C <= 200,000) cowpaths which are arranged as the usual graph which connects P (1 <= P <= 100,000) pastures conveniently numbered from 1..P: no cowpath leads from a pasture to itself, cowpaths are bidirectional, each cowpath has an associated distance, and, best of all, it is always possible to get from any pasture to any other pasture. Each cowpath connects two differing pastures P1_i (1 <= P1_i <= P) and P2_i (1 <= P2_i <= P) with a distance between them of D_i. The sum of all the distances D_i does not exceed 2,000,000,000. What is the minimum total distance Bessie must travel to deliver both apples by starting at pasture PB (1 <= PB <= P) and visiting pastures PA1 (1 <= PA1 <= P) and PA2 (1 <= PA2 <= P) in any order. All three of these pastures are distinct, of course. Consider this map of bracketed pasture numbers and cowpaths with distances:

If Bessie starts at pasture [5] and delivers apples to pastures [1] and [4], her best path is: 5 -> 6-> 7 -> 4* -> 3 -> 2 -> 1* with a total distance of 12.

一张 P 个点的无向图,C 条正权路。CLJ 要从 Pb 点(家)出发,既要去 Pa1 点 NOI 赛场拿金牌,也要去 Pa2 点 CMO 赛场拿金牌。(途中不必回家)可以先去 NOI,也可以先去 CMO。当然神犇 CLJ 肯定会使总路程最小,输出最小值。

输入格式 InputFormat

Line 1: Line 1 contains five space-separated integers: C, P, PB, PA1, and PA2 * Lines 2..C+1: Line i+1 describes cowpath i by naming two pastures it connects and the distance between them: P1_i, P2_i, D_i.

输出格式 OutputFormat

Line 1: The shortest distance Bessie must travel to deliver both apples.

样例输入 SampleInput

15 10 1 9 5
1 3 812
1 4 548
2 4 881
1 2 288
8 7 1031
3 2 383
5 10 819
8 6 1196
4 8 599
2 6 724
8 1 485
4 3 990
5 7 12
3 5 125
7 9 352

样例输出 SampleOutput

1160

来源 Source

Silver


BZOJ 2100


代码 Code

只有两条路径:①Pb->Pa1->Pa2; ②Pb->Pa2->Pa1. 于是以 Pa1 和 Pa2 为起点两次 SPFA 取最小值。

#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
const int inf=0x7fffffff/11.27;
struct edge
{
    int to,next,val;
}e[400005];
int head[100005];
int dis1[100005],dis2[100005];
bool used[100005];
deque <int> q;
int i,j,t,n,m,l,r,k,z,y,x,c,p,pb,pa1,pa2,ans,cnt=0;
inline void read(int &x)
{
    x=0;char ch=getchar();
    while (ch<'0' || ch>'9') ch=getchar();
    while (ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
}
void ins(int u,int v,int w)
{
    e[++cnt].to=v;e[cnt].next=head[u];e[cnt].val=w;
    head[u]=cnt;
}
void spfa(int s,int *dis)
{
    int i,j,t,u,v,w;
    memset(used,false,sizeof(used));
    while (!q.empty()) q.pop_front();
    for (i=1;i<=p;i++) dis[i]=inf;
    used[s]=true;dis[s]=0;q.push_back(s);
    while (!q.empty())
    {
        u=q.front();q.pop_front();
        used[u]=false;
        for (i=head[u];i;i=e[i].next)
        {
            v=e[i].to;w=e[i].val;
            if (dis[v]>dis[u]+w)
            {
                dis[v]=dis[u]+w;
                if (!used[v])
                {
                    used[v]=true;
                    if (!q.empty())
                    {
                        if (dis[v]>dis[q.front()]) q.push_back(v);
                        else q.push_front(v);
                    }
                    else q.push_back(v);
                }
            }
        }
    }
}
int main()
{
    read(c);read(p);read(pb);read(pa1);read(pa2);
    for (i=1;i<=c;i++)
    {
        read(x);read(y);read(z);
        ins(x,y,z);
        ins(y,x,z);
    }
    spfa(pa1,dis1);
    spfa(pa2,dis2);
    ans=dis1[pa2]+min(dis1[pb],dis2[pb]);
    printf("%d\n",ans);
    return 0;
}