• 牛客 NC25020 [USACO 2007 Nov S]Cow Hurdles


    题目描述

    Farmer John wants the cows to prepare for the county jumping competition, so Bessie and the gang are practicing jumping over hurdles. They are getting tired, though, so they want to be able to use as little energy as possible to jump over the hurdles.
    Obviously, it is not very difficult for a cow to jump over several very short hurdles, but one tall hurdle can be very stressful. Thus, the cows are only concerned about the height of the tallest hurdle they have to jump over.
    The cows' practice room has N (1 ≤ N ≤ 300) stations, conveniently labeled 1..N. A set of M (1 ≤ M ≤ 25,000) one-way paths connects pairs of stations; the paths are also conveniently labeled 1..M. Path i travels from station Si to station Ei and contains exactly one hurdle of height Hi (1 ≤ Hi ≤ 1,000,000). Cows must jump hurdles in any path they traverse.
    The cows have T (1 ≤ T ≤ 40,000) tasks to complete. Task i comprises two distinct numbers, Ai and Bi (1 ≤ Ai ≤ N; 1 ≤ Bi ≤ N), which connote that a cow has to travel from station Ai to station Bi (by traversing over one or more paths over some route). The cows want to take a path the minimizes the height of the tallest hurdle they jump over when traveling from Ai to Bi . Your job is to write a program that determines the path whose tallest hurdle is smallest and report that height.

    输入描述:

    * Line 1: Three space-separated integers: N, M, and T
    * Lines 2..M+1: Line i+1 contains three space-separated integers: Si , Ei , and Hi
    * Lines M+2..M+T+1: Line i+M+1 contains two space-separated integers that describe task i: Ai and Bi

    输出描述:

    * Lines 1..T: Line i contains the result for task i and tells the smallest possible maximum height necessary to travel between the stations. Output -1 if it is impossible to travel between the two stations.

    示例1

    输入

    复制

    5 6 3
    1 2 12
    3 2 8
    1 3 5
    2 5 3
    3 4 4
    2 4 8
    3 4
    1 2
    5 1

    输出

    复制

    4
    8
    -1

    说明

    Query #1: The best way is to simply travel on the path from station 3 to station 4.
    Query #2: There is a path from station 1 to station 2, but a better way would be to travel from station 1 to station 3 and then to station 2.
    Query #3: There are no paths that start at station 5, so it is clear that there is no way to reach station 1 from station 5.

    思路:要求最短路当中的最大栏高,我们可以在更新最短路时直接用最大栏高更新

    AC代码:

    1. #include
    2. #include
    3. #include
    4. #include
    5. using namespace std;
    6. const int N=310,INF=0x3f3f3f3f;
    7. int n,m,x;
    8. int d[N][N];
    9. void floyd()
    10. {
    11. for(int k=1;k<=n;k++)
    12. for(int i=1;i<=n;i++)
    13. for(int j=1;j<=n;j++)
    14. d[i][j]=min(d[i][j],max(d[i][k],d[k][j]));
    15. }
    16. int main()
    17. {
    18. scanf("%d%d%d",&n,&m,&x);
    19. memset(d,0x3f,sizeof d);
    20. for(int i=1;i<=n;i++)
    21. d[i][i]=0;
    22. while(m--)
    23. {
    24. int a,b,c;
    25. scanf("%d%d%d",&a,&b,&c);
    26. d[a][b]=min(d[a][b],c);
    27. }
    28. floyd();
    29. while(x--)
    30. {
    31. int a,b;
    32. scanf("%d%d",&a,&b);
    33. if(d[a][b]==INF)
    34. puts("-1");
    35. else
    36. printf("%d\n",d[a][b]);
    37. }
    38. }

  • 相关阅读:
    酒店解决方案近呗科技
    继承和多态的深度剖析
    利用uni-app 开发的iOS app 发布到App Store全流程
    最近一段时间的规划
    CocosCreator3.8研究笔记(八)CocosCreator 节点和组件的使用
    【计算机网络】数字签名为什么能够解决信用问题
    股票复盘思路
    JMETER实战从创建测试数据到分布式压测测试全流程
    Node.js 零基础入门 Node.js 零基础入门第二天 2.3 npm与包
    c语言进阶篇:柔性数组
  • 原文地址:https://blog.csdn.net/qq_62242287/article/details/126683544