1. 程式人生 > 實用技巧 >【洛谷 3003】[USACO10DEC]Apple Delivery S

【洛谷 3003】[USACO10DEC]Apple Delivery S

題目描述

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.

輸入格式

* 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

題意翻譯

貝西有兩個又香又脆的紅蘋果要送給她的兩個朋友。當然她可以走的CC(1<=C<=2000001<=C<=200000)條“牛路”都被包含在一種常用的圖中,包含了PP(1<=P<=1000001<=P<=100000)個牧場,分別被標為1..P1..P。沒有“牛路”會從一個牧場又走回它自己。“牛路”是雙向的,每條牛路都會被標上一個距離。最重要的是,每個牧場都可以通向另一個牧場。每條牛路都連線著兩個不同的牧場P1\_iP1_i和P2\_iP2_i(1<=P1\_i,p2\_i<=P1<=P1_i,p2_i<=P),距離為D\_iD_i。所有“牛路”的距離之和不大於20000000002000000000。

現在,貝西要從牧場PBPB開始給PA\_1PA_1和PA\_2PA_2牧場各送一個蘋果(PA\_1PA_1和PA\_2PA_2順序可以調換),那麼最短的距離是多少呢?當然,PBPB、PA\_1PA_1和PA\_2PA_2各不相同。

輸入輸出樣例

輸入 #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,因此進行stf優化即可。我也是才學的(其實我覺得背下來就ojbk?)
注意dis[v]<dis[q.front()] q.front()不能寫u,我就是這裡想了好久哦哈哈哈
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int oo=0x3f3f3f3f;
const int N=400002;

int m,n,S,T1,T2,head[N];
int cnt,vis[N],dis[N],x,y,z;

struct node{
    int next;
    int to;
    int w;
}e[N];

void add(int x,int y,int z){
    e[++cnt].to=y;
    e[cnt].w=z;
    e[cnt].next=head[x];
    head[x]=cnt;
}

int spfa(int s,int t){
    //queue<int>q;
    std::deque<int>q;
    memset(dis,0x3f,sizeof(dis));
    memset(vis,0,sizeof(vis));
    vis[s]=1; dis[s]=0; q.push_front(s);
    while(q.size()){
        int u=q.front(); q.pop_front(); vis[u]=0;
        for(int i=head[u];i;i=e[i].next){
            int v=e[i].to;
            if(dis[v]>dis[u]+e[i].w){
                dis[v]=dis[u]+e[i].w;
                if(!vis[v]){
                    vis[v]=1; //q.push_front(v); 
                    if(q.size() && dis[v]<dis[q.front()])
                       q.push_front(v);
                    else q.push_back(v);
                }
            }
        }
    } 
    return dis[t];
}
int ans=-1;
int main(){
    freopen("3033.in","r",stdin);
    freopen("3033.out","w",stdout);
    scanf("%d %d %d %d %d",&m,&n,&S,&T1,&T2);
    for(int i=1;i<=m;i++){
        scanf("%d %d %d",&x,&y,&z);
        add(x,y,z); add(y,x,z);
    }
    cout<<std::min(spfa(S,T1),spfa(S,T2))+spfa(T1,T2);
    return 0;
}