洛穀P3003 [USACO10DEC]蘋果交貨Apple Delivery

來源:互聯網
上載者:User

標籤:ast   開始   log   sel   fine   pos   div   txt   oci   

P3003 [USACO10DEC]蘋果交貨Apple Delivery題目描述

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 
/*    基本上是個最短路模板題吧。。    就是求出t1和t2間的最短路a1    再求min(dis[s][t1],dis[s][t2])    兩者相加即可    本來以為這題沒這麼簡單,隨便一寫結果A了    我用的堆最佳化Dij*/#include<iostream>#include<cstdio>#include<cstring>#include<queue>using namespace std;#define maxn 100010int n,m,s,t1,t2,dis[maxn],num,head[maxn];bool vis[maxn];struct node{    int id,d;    bool operator < (const node b)const{        return d>b.d;    }};struct Node{    int to,pre,v;}e[200010*2];void Insert(int from,int to,int v){    e[++num].to=to;    e[num].v=v;    e[num].pre=head[from];    head[from]=num;}void Dij(int st,int tt1,int tt2){    int flag=0;    priority_queue<node>q;    memset(dis,127/3,sizeof(dis));    memset(vis,0,sizeof(vis));    dis[st]=0;node now;    now.id=st;now.d=0;    q.push(now);    while(!q.empty()){        now=q.top();q.pop();        int u=now.id;        if(vis[u])continue;        vis[u]=1;        if(u==tt1)flag++;        if(u==tt2)flag++;        if(flag==2)return;        for(int i=head[u];i;i=e[i].pre){            int to=e[i].to;            if(dis[u]+e[i].v<dis[to]){                dis[to]=dis[u]+e[i].v;                node nxt;nxt.d=dis[to];nxt.id=to;                q.push(nxt);            }        }    }}int main(){    //freopen("Cola.txt","r",stdin);    scanf("%d%d%d%d%d",&m,&n,&s,&t1,&t2);    int x,y,z;    for(int i=1;i<=m;i++){        scanf("%d%d%d",&x,&y,&z);        Insert(x,y,z);        Insert(y,x,z);    }    int a1,a2=0x7fffffff;    Dij(s,t1,t2);    a1=min(dis[t1],dis[t2]);    Dij(t1,t2,t2);    a2=min(dis[t2],a2);    Dij(t2,t1,t1);    a2=min(dis[t1],a2);    cout<<a1+a2;}

 

洛穀P3003 [USACO10DEC]蘋果交貨Apple Delivery

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.