P3003 [USACO10DEC]蘋果交貨Apple Delivery

來源:互聯網
上載者:User

標籤:lin   direct   oid   bsp   namespace   重要   tar   radius   get   

題目描述

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: 
 1 /*這道題很好做,直接套slf最佳化的模板,跑兩次取最小值,就好了,本人先開始把slf和lll最佳化一起加了進去,但是由於lll被常數卡爆了,卡到指數級去了,然後怎麼最佳化都會T掉第二個點,如果用lll最佳化會T掉4個點(本人親手實驗),用slf可以A掉,而且很快。直接上代碼吧。*/ 2  3 #include<algorithm> 4 #include <iostream> 5 #include  <cstdlib> 6 #include  <cstring> 7 #include  <climits> 8 #include   <cstdio> 9 #include   <string>10 #include    <cmath>11 #include    <stack>12 #include    <queue>13 #include    <deque>14 using namespace std;15 const int gg=450000;16 const int INF=1e9;17 int head[gg];18 struct node19 {20     int next;21     int w;22     int to;23 } a[gg];24 int dis[gg];25 bool vis[gg];26 int c,p,pb,pa1,pa2,cnt;27 int ans;28 int ans2;29 int sum,tot;30 int rans;31 inline void add(int i,int j,int w)32 {33     a[++cnt].to=j;34     a[cnt].next=head[i];35     a[cnt].w=w;36     head[i]=cnt;37 }38 inline void spfa(int s)39 {40     deque<int>q;41     memset(vis,false,sizeof(vis));42     memset(dis,0x7f,sizeof(dis));43     dis[s]=0;44     vis[s]=true;45     q.push_back(s);46     while(!q.empty())47     {48         int u=q.front();49         q.pop_front();50         vis[u]=false;51         for(register int i=head[u]; i; i=a[i].next)52         {53             int v=a[i].to;54             if(dis[v]>dis[u]+a[i].w)55             {56                 dis[v]=dis[u]+a[i].w;57                 if(!vis[v])58                 {59                     vis[v]=true;60                     if(q.empty()||dis[v]>dis[q.front()])61                     {62                         q.push_back(v);63                     }64                     else65                         q.push_front(v);66                 }67             }68         }69     }70 }71 int main()72 {73     scanf("%d%d%d%d%d",&c,&p,&pb,&pa1,&pa2);74     for(register int i=1; i<=c; i++)75     {76         int x,y,z;77         scanf("%d%d%d",&x,&y,&z);78         add(x,y,z);79         add(y,x,z);80     }81     spfa(pa1);82     ans+=dis[pa2]+dis[pb];83     spfa(pa2);84     ans2+=dis[pa1]+dis[pb];85     rans=min(ans,ans2);86     printf("%d\n",rans);87     return 0;88 }

 


12 

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.