• 牛客 NC25077 [USACO 2007 Ope B]Bronze Cow Party


    题目描述

    One cow from each of N farms (1 <= N <= 1000) conveniently numbered 1..N is attending the big cow party to be held at farm #X (1 <= X <= N). Each of M (1 <= M <= 100,000) bidirectional roads connects one farm to another. It is always possible to travel from one farm to another on the roads. Traversing road i requires Ti (1 <= Ti <= 100) units of time. One or more pairs of farms might be directly connected by more than one road.
    After all the cows gathered at farm #X, they realized that every single cow left her party favors back at the farm. They decided to suspend the party and send all the cows back to get the party favors. The cows all travel optimal routes to their home farm and back to the party. What is the minimum number of time units the party must be suspended?One cow from each of N farms (1 <= N <= 1000) conveniently numbered 1..N is attending the big cow party to be held at farm #X (1 <= X <= N). Each of M (1 <= M <= 100,000) bidirectional roads connects one farm to another. It is always possible to travel from one farm to another on the roads. Traversing road i requires Ti (1 <= Ti <= 100) units of time. One or more pairs of farms might be directly connected by more than one road.

    输入描述:

    Line 1: Three space-separated integers, respectively: N, M, and X
    Lines 2..M+1: Line i+1 describes road i with three space-separated integers, respectively: Ai, Bi, and Ti. The described road connects Ai and Bi and requires Ti time units to traverse.

    输出描述:

    Line 1: One integer: the minimum amount of time the party must be suspended.

    示例1

    输入

    复制

    4 8 2
    1 2 7
    1 3 8
    1 4 4
    2 1 3
    2 3 1
    3 1 2
    3 4 6
    4 2 2
    

    输出

    复制

    6

    说明

    Four cows; eight roads; party at farm 2.
    Direct paths connects farm 2 to each of the other farms (to farm 1: 7 and 3; to farm 3: 1; to farm 4: 2). The longest path is length 3, so the round-trip time is 6.

    思路:一道非常简单的最短路问题,要求所有奶牛往返的最短时间,只需要求所有牛场到终点之间最短路的最大值,然后将最大值的两倍输出即可,于是我们可以以终点为起点跑一遍spfa,然后遍历终点到每个牛场的距离取最大值即可;

    AC代码:

    1. #include
    2. #include
    3. #include
    4. #include
    5. using namespace std;
    6. const int N=1010,M=200010;
    7. int n,m,x;
    8. int h[N],w[M],ne[M],e[M],idx;
    9. int dist[N];
    10. bool st[N];
    11. void add(int a,int b,int c)
    12. {
    13. w[idx]=c,e[idx]=b,ne[idx]=h[a],h[a]=idx++;
    14. }
    15. void spfa()
    16. {
    17. memset(dist,0x3f,sizeof dist);
    18. dist[x]=0;
    19. queue<int>q;
    20. q.push(x);
    21. while(q.size())
    22. {
    23. int t=q.front();
    24. q.pop();
    25. st[t]=0;
    26. for(int i=h[t];~i;i=ne[i])
    27. {
    28. int j=e[i];
    29. if(dist[j]>dist[t]+w[i])
    30. {
    31. dist[j]=dist[t]+w[i];
    32. if(!st[j])
    33. {
    34. st[j]=1;
    35. q.push(j);
    36. }
    37. }
    38. }
    39. }
    40. }
    41. int main()
    42. {
    43. memset(h,-1,sizeof h);
    44. scanf("%d%d%d",&n,&m,&x);
    45. while(m--)
    46. {
    47. int a,b,c;
    48. scanf("%d%d%d",&a,&b,&c);
    49. add(a,b,c),add(b,a,c);
    50. }
    51. spfa();
    52. int res=0;
    53. for(int i=1;i<=n;i++)
    54. res=max(res,dist[i]);
    55. cout<2<
    56. }

    题目描述

    One cow from each of N farms (1 <= N <= 1000) conveniently numbered 1..N is attending the big cow party to be held at farm #X (1 <= X <= N). Each of M (1 <= M <= 100,000) bidirectional roads connects one farm to another. It is always possible to travel from one farm to another on the roads. Traversing road i requires Ti (1 <= Ti <= 100) units of time. One or more pairs of farms might be directly connected by more than one road.
    After all the cows gathered at farm #X, they realized that every single cow left her party favors back at the farm. They decided to suspend the party and send all the cows back to get the party favors. The cows all travel optimal routes to their home farm and back to the party. What is the minimum number of time units the party must be suspended?One cow from each of N farms (1 <= N <= 1000) conveniently numbered 1..N is attending the big cow party to be held at farm #X (1 <= X <= N). Each of M (1 <= M <= 100,000) bidirectional roads connects one farm to another. It is always possible to travel from one farm to another on the roads. Traversing road i requires Ti (1 <= Ti <= 100) units of time. One or more pairs of farms might be directly connected by more than one road.

    输入描述:

    Line 1: Three space-separated integers, respectively: N, M, and X
    Lines 2..M+1: Line i+1 describes road i with three space-separated integers, respectively: Ai, Bi, and Ti. The described road connects Ai and Bi and requires Ti time units to traverse.

    输出描述:

    Line 1: One integer: the minimum amount of time the party must be suspended.

    示例1

    输入

    复制

    4 8 2
    1 2 7
    1 3 8
    1 4 4
    2 1 3
    2 3 1
    3 1 2
    3 4 6
    4 2 2
    

    输出

    复制

    6

    说明

    Four cows; eight roads; party at farm 2.
    Direct paths connects farm 2 to each of the other farms (to farm 1: 7 and 3; to farm 3: 1; to farm 4: 2). The longest path is length 3, so the round-trip time is 6.
  • 相关阅读:
    软件测试-朋友圈的点赞功能怎么测?
    halcon脚本-边缘及骨架的识别【附源码】
    vue js 禁用控件一分钟,并显示倒计时
    对图的广度优先遍历BFS和深度优先遍历DFS的应用——基于LeetCode133 克隆图
    ElasticSearch部署实战架构、容量规划优化
    Java后端开发&微服务面试题,有爱分享,祝顺利秋招
    教程九 在Go中使用Energy创建跨平台GUI应用 - Go绑定变量JS调用
    比亚迪、吉利、蔚来等将出席2023第四届中国新能源汽车热管理峰会
    【juc】countdownlatch实现游戏进度
    Rust GUI库 egui 的简单应用
  • 原文地址:https://blog.csdn.net/qq_62242287/article/details/126683445