bzoj2100&luogu3003[USACO10DEC]苹果交货Apple Delivery

http://www.elijahqi.win/archives/926
题目描述

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:

           3        2       2
       [1]-----[2]------[3]-----[4]
         \     / \              /
         7\   /4  \3           /2
           \ /     \          /
           [5]-----[6]------[7]
                1       2

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.

贝西有两个又香又脆的红苹果要送给她的两个朋友。当然她可以走的C(1<=C<=200000)条“牛路”都被包含在一种常用的图中,包含了P(1<=P<=100000)个牧场,分别被标为1..P。没有“牛路”会从一个牧场又走回它自己。“牛路”是双向的,每条牛路都会被标上一个距离。最重要的是,每个牧场都可以通向另一个牧场。每条牛路都连接着两个不同的牧场P1_i和P2_i(1<=P1_i,p2_i<=P),距离为D_i。所有“牛路”的距离之和不大于2000000000。

现在,贝西要从牧场PB开始给PA_1和PA_2牧场各送一个苹果(PA_1和PA_2顺序可以调换),那么最短的距离是多少呢?当然,PB、PA_1和PA_2各不相同。

输入输出格式

输入格式:

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
输出格式:

Line 1: The shortest distance Bessie must travel to deliver both apples
输入输出样例

输入样例#1:

9 7 5 1 4
5 1 7
6 7 2
4 7 2
5 6 1
5 2 4
4 3 2
1 2 3
3 2 2
2 6 3
输出样例#1:

12
普通spfa被卡
快使用slf spfa

#include<cstdio>
#include<cstring>
#include<deque>
#define N 110000
#define M 220000
inline int read(){
    int x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
    while (ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
struct node{
    int y,z,next;
}data[M<<1];
inline int min(int x,int y){
    return x<y?x:y;
}
int f[N],c,p,s,t1,t2,ans,h[N],num=0;bool flag[N];
void spfa(int xx){
    memset(f,0x7f,sizeof(f));memset(flag,false,sizeof(flag));
    std::deque<int>q;
    q.push_back(xx);f[xx]=0;flag[xx]=true;
    while (!q.empty()){
        int x=q.front();q.pop_front();flag[x]=false;
        for (int i=h[x];i;i=data[i].next){
            int y=data[i].y,z=data[i].z;
            if (f[x]+z<f[y]){
                f[y]=f[x]+z;
                if (flag[y]==false){
                    if (!q.empty()&&f[y]<f[q.front()]) q.push_front(y);else q.push_back(y);
                    flag[y]=true;
                }
            }
        }
    }
}
int main(){
    freopen("3003.in","r",stdin);
    c=read();p=read();s=read();t1=read();t2=read();
    for (int i=1;i<=c;++i){
        int x=read(),y=read(),z=read();
        data[++num].y=y;data[num].z=z;data[num].next=h[x];h[x]=num;
        data[++num].y=x;data[num].z=z;data[num].next=h[y];h[y]=num;
    }
    spfa(t1);ans=f[s]+f[t2];
    spfa(t2);ans=min(ans,f[s]+f[t1]);
    printf("%d",ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值