-
Notifications
You must be signed in to change notification settings - Fork 0
/
BOJ13905.cpp
88 lines (86 loc) · 1.49 KB
/
BOJ13905.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
#define MAX 100001
using namespace std;
int parent[MAX]={0};
typedef struct _node{
int start;
int end;
int weight;
}Node;
bool cmp(const Node&a,const Node&b){
if(a.weight>b.weight)
return true;
else
return false;
}
vector<Node> sorted;
int find(int x){
if(parent[x]==x)
return x;
else
return parent[x]=find(parent[x]);
}
bool unionAble(int x,int y){
x=find(x);
y=find(y);
if(x==y)
return false;
else
return true;
}
void unionTogeter(int x,int y){
x=find(x);
y=find(y);
if(x==y)
return;
else if(x>y)
parent[x]=y;
else
parent[y]=x;
return;
}
bool parentSame(int start,int end){
start=find(start);
end=find(end);
if(start==end)
return true;
else
return false;
}
int main(void){
int sum=0,l=0;
for(int i=0;i<MAX;i++)
parent[i]=i;
ios_base :: sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n,m,start,end;
int temp1,temp2,weight;
cin>>n>>m>>start>>end;
for(int i=0;i<m;i++){
Node temp;
cin>>temp1>>temp2>>weight;
temp.start=temp1;
temp.end=temp2;
temp.weight=weight;
sorted.push_back(temp);
}
sort(sorted.begin(),sorted.end(),cmp);
while(l<sorted.size()){
if(parentSame(start,end))
break;
if(unionAble(sorted[l].start,sorted[l].end)){
unionTogeter(sorted[l].start,sorted[l].end);
sum=sorted[l].weight;
}
l++;
}
if(parentSame(start,end))
cout<<sum;
else
cout<<0;
return 0;
}