• 牛客 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. }

  • 相关阅读:
    基于Python flask 的某招聘网站爬虫,招聘岗位可视化系统
    Java Double parseDouble(String s)方法具有什么功能呢?
    【博学谷学习记录】超强总结,用心分享丨人工智能 Python基础 个人学习总结之列表排序
    大数据(9f)Flink双流JOIN
    【MATLAB教程案例38】语音信号的去噪方法matlab仿真学习——LMS自适应滤波,谱减法去噪滤波及维纳滤波等
    【加密算法】PKI技术
    【9. 子矩阵和】
    C++:string类的常用接口说明及其模拟实现
    Js运算符
    golang闭包
  • 原文地址:https://blog.csdn.net/qq_62242287/article/details/126683544