forked from marioyc/Online-Judge-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3662 - Telephone Lines.cpp
executable file
·90 lines (70 loc) · 2.07 KB
/
3662 - Telephone Lines.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
89
90
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
int N,P,K;
scanf("%d %d %d",&N,&P,&K);
int u,v,w,W[10001];
vector< vector< pair<int, int> > > L(N);
W[0]=0;
for(int i=0;i<P;i++){
scanf("%d %d %d",&u,&v,&w);
u--; v--;
L[u].push_back(make_pair(v,w));
L[v].push_back(make_pair(u,w));
W[i+1]=w;
}
sort(W,W+P);
int lo=0,hi=P,mi,l;
int Q[1000];
int s1,s2,Qsize;
bool solved,visited[1000];
pair<int, int> p;
while(1){
mi=(lo+hi)>>1;
l=W[mi];
s1=0; Qsize=0;
fill(visited,visited+N,false);
Q[Qsize++]=0;
visited[0]=true;
solved=false;
for(int x=0;x<=K && !solved;x++){
for(int i=s1;i<Qsize && !solved;i++){
if(Q[i]==N-1) solved=true;
for(int j=L[Q[i]].size()-1;j>=0;j--){
p=L[Q[i]][j];
u=p.first;
v=p.second;
if(v<=l && !visited[u]){
Q[Qsize++]=u;
visited[u]=true;
}
}
}
if(solved) break;
s2=s1;
s1=Qsize;
for(int i=s2;i<s1;i++){
for(int j=L[Q[i]].size()-1;j>=0;j--){
p=L[Q[i]][j];
u=p.first;
v=p.second;
if(v>l && !visited[u]){
Q[Qsize++]=u;
visited[u]=true;
}
}
}
}
if(lo==hi){
if(!solved) printf("-1\n");
else printf("%d\n",W[lo]);
break;
}else{
if(!solved) lo=mi+1;
else hi=mi;
}
}
return 0;
}