• 牛客 NC24755 [USACO 2010 Dec S]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 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

    思路:因为是双向边,因此p1到p2与p2到p1的距离是相等的,于是我们只需要比较p到p1和p到p2那个短再加上p1到p2就是答案;因为目的地数量较少所以可以直接通过两次求最短路找到答案,分别以p1和p2为起点

    AC代码:

    1. #include
    2. #include
    3. #include
    4. #include
    5. using namespace std;
    6. #define PII pair
    7. const int N=100010,M=400010;
    8. int n,m,p1,p2,p;
    9. int h[N],e[M],ne[M],w[M],idx;
    10. int dist[3][N];
    11. bool st[N];
    12. queue<int>q ;
    13. void add(int a,int b,int c)
    14. {
    15. e[idx]=b,w[idx]=c,ne[idx]=h[a],h[a]=idx++;
    16. }
    17. void dijkstra(int dist[],int s)
    18. {
    19. memset(dist,0x3f,4*N);
    20. memset(st,0,sizeof st);
    21. dist[s]=0;
    22. priority_queue,greater> q;
    23. q.push({0,s});
    24. while(!q.empty())
    25. {
    26. PII t=q.top();
    27. q.pop();
    28. int ver=t.second,dis=t.first;
    29. if(st[ver])continue;
    30. st[ver]=true;
    31. for(int i=h[ver];i!=-1;i=ne[i])
    32. {
    33. int j=e[i];
    34. if(dist[j]>dis+w[i])
    35. {
    36. dist[j]=dis+w[i];
    37. q.push({dist[j],j});
    38. }
    39. }
    40. }
    41. }
    42. int main()
    43. {
    44. memset(h,-1,sizeof h);
    45. scanf("%d%d%d%d%d",&m,&n,&p,&p1,&p2);
    46. while(m--)
    47. {
    48. int a,b,c;
    49. scanf("%d%d%d",&a,&b,&c);
    50. add(a,b,c),add(b,a,c);
    51. }
    52. dijkstra(dist[0],p1);
    53. dijkstra(dist[1],p2);
    54. cout<<min(dist[0][p],dist[1][p])+dist[0][p2];
    55. }

  • 相关阅读:
    HCIA -- 动态路由协议之RIP
    微服务项目:尚融宝(10)(后端接口:统一异常处理)
    Solitidy - fallback 回退函数 - 2种触发执行方式
    一致性哈希算法【图解理论 + 代码实现】
    01-linux基础-操作系统的学习
    C# .NET Core API 注入Swagger
    sqllab第十关通关笔记
    JAVA计算机毕业设计校园网络维修系统Mybatis+源码+数据库+lw文档+系统+调试部署
    电脑监控软件:保护企业核心信息资产,防止数据泄露
    专利的分类
  • 原文地址:https://blog.csdn.net/qq_62242287/article/details/126671579