TOJ 1638 Out of Hay Prim最大边

柔光的暖阳◎ 2022-05-18 00:50 185阅读 0赞

1638: Out of Hay

描述

The cows have run out of hay, a horrible event that must be remedied immediately. Bessie intends to visit the other farms to survey their hay situation. There are N (2 <= N <= 2,000) farms (numbered 1..N); Bessie starts at Farm 1. She’ll traverse some or all of the M (1 <= M <= 10,000) two-way roads whose length does not exceed 1,000,000,000 that connect the farms. Some farms may be multiply connected with different length roads. All farms are connected one way or another to Farm 1.

Bessie is trying to decide how large a waterskin she will need. She knows that she needs one ounce of water for each unit of length of a road. Since she can get more water at each farm, she’s only concerned about the length of the longest road. Of course, she plans her route between farms such that she minimizes the amount of water she must carry.

Help Bessie know the largest amount of water she will ever have to carry: what is the length of longest road she’ll have to travel between any two farms, presuming she chooses routes that minimize that number? This means, of course, that she might backtrack over a road in order to minimize the length of the longest road she’ll have to traverse.

输入

* Line 1: Two space-separated integers, N and M.

* Lines 2..1+M: Line i+1 contains three space-separated integers, A_i, B_i, and L_i, describing a road from A_i to B_i of length L_i.

输出

* Line 1: A single integer that is the length of the longest road required to be traversed.

样例输入

3 3
1 2 23
2 3 1000
1 3 43

样例输出

43

提示

OUTPUT DETAILS:

In order to reach farm 2, Bessie travels along a road of length 23. To reach farm 3, Bessie travels along a road of length 43. With capacity 43, she can travel along these roads provided that she refills her tank to maximum capacity before she starts down a road.

Prim求最大边,套一下模板,判断一下ans就好啦。

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<math.h>
  4. #include<iostream>
  5. #include<string>
  6. #include<algorithm>
  7. #include<map>
  8. #include<set>
  9. #include<queue>
  10. #include<vector>
  11. using namespace std;
  12. #define inf 0x3f3f3f3f
  13. #define LL long long
  14. int n;
  15. int graph[2005][2005];
  16. int vis[2005];
  17. int prim()
  18. {
  19. memset(vis,0,sizeof vis);
  20. int low[2005];
  21. int ans=-1,pos=0,ap=0;
  22. vis[0]=1;
  23. for(int i=0;i<n;i++)
  24. low[i]=graph[pos][i];
  25. for(int i=1;i<n;i++)
  26. {
  27. int min=inf;
  28. for(int j=0;j<n;j++)
  29. {
  30. if(!vis[j]&&min>low[j])
  31. {
  32. min=low[j];
  33. pos=j;
  34. }
  35. }
  36. if(min==inf)break;
  37. vis[pos]=1;
  38. if(ans<min)
  39. ans=min;
  40. for(int k=1;k<=n;k++)
  41. {
  42. if(!vis[k]&&low[k]>graph[pos][k])
  43. low[k]=graph[pos][k];
  44. }
  45. }
  46. return ans;
  47. }
  48. int main()
  49. {
  50. int m,i,j,t,x,y,z;
  51. scanf("%d%d",&n,&m);
  52. memset(graph,inf,sizeof graph);
  53. for(i=0;i<m;i++)
  54. {
  55. scanf("%d%d%d",&x,&y,&z);
  56. if(graph[x-1][y-1]>z)
  57. graph[x-1][y-1]=graph[y-1][x-1]=z;
  58. }
  59. printf("%d\n",prim());
  60. }

发表评论

表情:
评论列表 (有 0 条评论,185人围观)

还没有评论,来说两句吧...

相关阅读

    相关 TOJ 3711 浪漫自习

    如今的校园谈恋爱已是习以为常,这不,去上自习也要成双成对的。现在假设某班里有N对情侣从同一寝室楼出发,到达同一个教室上自习。途中,她们可能会经过长廊、静溪等一系列的景点观光游览