码迷,mamicode.com
首页 > 其他好文 > 详细

最大边最小的路径

时间:2019-12-28 09:47:24      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:return   line   pst   输入格式   undefined   ace   res   数据说明   描述   

【问题描述】

  给定 n 个结点,m 条边的带权无向图,请你求 s 到 t所有路径中,权值最大的边的最小的那条路径,输出这个最大边的最小值。

【输入格式】

  第一行两个整数 n 和 m ,图的每个点编号为 1n。
  接下来的 m 行,每行 3 个整数:u v w,表示边无向边 (u,v) 的权值为 w。
  最后一行是 ss 和 tt。

【输出格式】

  输出 s 到 t 路径上的最大边的最小值。

【输入输出样例】

 Input

7 10
1 2 30
1 3 15
1 4 10
2 4 25
2 5 60
3 4 40
3 6 20
4 7 35
5 7 20
6 7 30
1 7

 Output

30

【数据说明】

  对于 100% 的数据 1≤n≤500001≤m≤100000,边权不超过 10^9

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int n,m;
 4 const int maxn=0x3f3f3f;
 5 struct name{
 6     int u,v,w;
 7 }e[maxn];
 8 bool bml(name x,name y)
 9 {
10     return x.w<y.w;
11 }
12 int pa[maxn];
13 void makeset()
14 {
15     for(int i=1;i<=n;i++)
16     {
17         pa[i]=i;
18     }
19 }
20 int Find(int x)
21 {
22     int r=x;
23     while(pa[r]!=r) 
24     r=pa[r];
25     
26     while(x!=r)
27     { 
28     int y=pa[x];
29     pa[x]=r;
30     x=y;
31     }
32     return r;
33 }
34 void Link(int x,int y)
35 {
36     int a=Find(x),b=Find(y);
37     if(a!=b)
38     {
39     pa[a]=b;
40     }
41 }
42 bool Query(int x,int y)
43 {
44     return Find(x)==Find(y);
45 }
46 int main()
47 {
48     scanf("%d%d",&n,&m);
49     for(int i=1;i<=m;i++)
50     {
51         scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);        
52     }
53     sort(e+1,e+1+m,bml);
54     int s,t;
55     cin>>s>>t;
56     
57     makeset();
58     for(int i=1;i<=m;i++)
59     {
60         Link(e[i].u,e[i].v);
61         if(Query(s,t)==1)
62         {
63             cout<<e[i].w<<endl;
64             return 0;
65         }
66     }
67     return 0;
68 }

最大边最小的路径

标签:return   line   pst   输入格式   undefined   ace   res   数据说明   描述   

原文地址:https://www.cnblogs.com/poised/p/12110710.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!