• 牛客 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.
  • 相关阅读:
    【问题解决】Ubuntu 安装 SeisSol 依赖 easi 报错解决: undefined reference to `H5free_memory‘
    Windows环境下VSCode C无法跳转自动补全
    网页课程设计大作业——华山旅游网
    动画云渲染要多少钱?云渲染怎么使用?
    2022CSP-J 题解[持续更新ing]
    国内成品油价格测算
    DocCMS keyword SQL注入漏洞复现 [附POC]
    新鲜速递:Spring Boot3多模块项目跨包自动注入的方法,快速编写自己的starter项目
    CV&NLP基础10之卷积神经网络CNN进阶
    4.2 实现注册与登录模块
  • 原文地址:https://blog.csdn.net/qq_62242287/article/details/126683445