记录编号 100059 评测结果 AAAAAAAAAA
题目名称 [福州培训2010] 修复公路 最终得分 100
用户昵称 GravatarHouJikan 是否通过 通过
代码语言 C++ 运行时间 0.223 s
提交时间 2014-05-02 21:20:33 内存使用 1.46 MiB
显示代码纯文本
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <algorithm>
  5. #include <cstring>
  6. #include <cstdlib>
  7. #include <set>
  8. #include <list>
  9. #include <queue>
  10. #include <stack>
  11. using namespace std;
  12. struct ed
  13. {
  14. int f,t,w;
  15. }edge[100001];
  16. int root[1001];
  17. int findroot(int x);
  18. bool cmp(ed a,ed b);
  19. int main()
  20. {
  21. freopen("roada.in","r",stdin);
  22. freopen("roada.out","w",stdout);
  23. int n,m;
  24. cin>>n>>m;
  25. for(int a=1;a<=n;a++)
  26. root[a]=a;
  27. for(int a=1;a<=m;a++)
  28. scanf("%d%d%d",&edge[a].f,&edge[a].t,&edge[a].w);
  29. sort(edge+1,edge+1+m,cmp);
  30. bool connect=false;
  31. int sum=0;
  32. int nown=1;
  33. for(int a=1;a<=m;a++)
  34. {
  35. int fr=findroot(edge[a].f);
  36. int tr=findroot(edge[a].t);
  37. if (fr!=tr)
  38. {
  39. root[fr]=tr;
  40. nown++;
  41. sum=max(sum,edge[a].w);
  42. }
  43. if (nown==n)
  44. {
  45. connect=true;
  46. break;
  47. }
  48. }
  49. if (connect)
  50. printf("%d",sum);
  51. else
  52. cout<<"-1";
  53. return 0;
  54. }
  55. //================
  56. int findroot(int x)
  57. {
  58. if (root[x]!=x)
  59. return root[x]=findroot(root[x]);
  60. else
  61. return x;
  62. }
  63. bool cmp(ed a,ed b)
  64. {
  65. return a.w<b.w;
  66. }
  67.