首页 > 其他分享 >1015 [USACO 2010 Dec S]Apple Delivery 最短路 建图

1015 [USACO 2010 Dec S]Apple Delivery 最短路 建图

时间:2022-08-18 21:22:22浏览次数:84  
标签:dist Apple int USACO Delivery a3 a2 ans ver

 链接:https://ac.nowcoder.com/acm/contest/26077/1015
来源:牛客网

题目描述

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 P1iP1_iP1i​ (1 <= P1iP1_iP1i​ <= P) and P2iP2_iP2i​ (1 <= P2iP2_iP2i​ <= P) with a distance between them of DiD_iDi​. The sum of all the distances DiD_iDi​ 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: P1i,P2i,DiP1_i, P2_i, D_iP1i​,P2i​,Di​

输出描述:

* 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 

输出

复制
12

分析

四种走法:

 

 

 

 

 

 

 

 

//-------------------------代码----------------------------

//#define int ll
const int N = 2e5+10;
int n,m;
V<pii> e[N];
bool vis[N];
int dist[N];
void dij(int st) {
    priority_queue<pii,V<pii>,greater<pii>> q;
    ms(dist,0x3f);ms(vis,0);
    q.push({0,st});
    dist[st] = 0;
    while(q.size()) {
        auto tmp = q.top();q.pop();
        int ver = tmp.y;
        if(vis[ver]) continue;
        vis[ver] = 1;
        for(auto it:e[ver]) {
            int j = it.first;
            if(dist[j] > dist[ver] + it.second) {
                dist[j] = dist[ver] + it.second;
                q.push({dist[j],j});
            }
        }
    }
}
int c,p,a1,a2,a3;
void solve()
{
//    cin>>n>>m;
    cin>>c>>p>>a1>>a2>>a3;
    fo(i,1,c) {
        int x,y,z;cin>>x>>y>>z;
        e[x].pb({y,z});e[y].pb({x,z});
    }
    
    int ans = 0x3f3f3f3f;
    dij(a1);
    ans = min({ans,dist[a2] + 2 * dist[a3],dist[a2] * 2 + dist[a3]});
    dij(a2);
    ans = min(ans,dist[a1]+dist[a3]);
    dij(a3);
    ans = min(ans,dist[a1]+dist[a2]);
    cout<<ans<<endl;
}
void main_init() {}
signed main(){
    AC();clapping();TLE;
    cout<<fixed<<setprecision(12);
    main_init();
//  while(cin>>n,n)
//  while(cin>>n>>m,n,m)
//    int t;cin>>t;while(t -- )
    solve();
//    {solve(); }
    return 0;
}

/*样例区


*/

//------------------------------------------------------------

 

标签:dist,Apple,int,USACO,Delivery,a3,a2,ans,ver
From: https://www.cnblogs.com/er007/p/16600173.html

相关文章