ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2022-08-18 21:34:31  阅读:178  来源: 互联网

标签: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
来源: https://www.cnblogs.com/er007/p/16600173.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有