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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
| #include <cstdio>
#include <algorithm>
#include <cctype>
using namespace std;
namespace fast_io {
//...
}using namespace fast_io;
const int MAXN = 151000;
struct Link_Cat_Tree{
int val[MAXN],maxn[MAXN];
int c[MAXN][2],f[MAXN];
bool rev[MAXN];
int getmax(int u,int x,int y){
if(val[u] >= val[maxn[x]] && val[u] >= val[maxn[y]])
return u;
else if(val[maxn[x]] >= val[maxn[y]])
return maxn[x];
else
return maxn[y];
}
bool noroot(int x){
return (c[f[x]][1] == x) || (c[f[x]][0] == x);
}
void push_up(int x){
if(!x) return;
maxn[x] = getmax(x,c[x][0],c[x][1]);
}
void reverse(int x){
if(!x) return;
swap(c[x][0],c[x][1]);
rev[x]^=1;
}
void push_down(int x){
if(!x) return;
if(rev[x]){
reverse(c[x][0]),reverse(c[x][1]);
rev[x] = 0;
}
}
void push_all(int x){
if(!x) return;
if(noroot(x)) push_all(f[x]);
push_down(x);
}
void rotate(int x){
int y = f[x],z = f[y],t = (c[y][1] == x),w = c[x][1-t];
if(noroot(y)) c[z][c[z][1]==y] = x;
c[x][1-t] = y;c[y][t] = w;
if(w) f[w] = y;
f[y] = x;f[x] = z;
push_up(y),push_up(x);
}
void splay(int x){
push_all(x);
while(noroot(x)){
int y = f[x],z = f[y];
if(noroot(y))
(c[z][1] == y) ^ (c[y][1] == x) ? rotate(x):rotate(y);
rotate(x);
}
}
void access(int x){
for(int y = 0;x;x=f[y=x]){
splay(x);
c[x][1] = y,push_up(x);
}
}
void makeroot(int x){
access(x);splay(x);reverse(x);
}
int findroot(int x){
access(x);splay(x);
push_down(x);
while(c[x][0])
x = c[x][0],push_down(x);
return x;
}
void link(int x,int y){
makeroot(x);
if(findroot(y) != x)
f[x] = y;
}
void cat(int x,int y){
makeroot(x);
if(findroot(y) == x && f[x] == y && !c[x][1])
f[x] = c[y][0] = 0,push_up(y);
}
void split(int x,int y){
makeroot(x),access(y),splay(y);
}
int querymax(int x,int y){
return split(x,y),maxn[y];
}
}T;
//点1->n 边n+1->n+m
int n,m;
struct Edge{
int from,to,a,b;
}edge[MAXN];
bool cmp(Edge x,Edge y){
return x.a < y.a;
}
void init(){
read(n),read(m);
int a,b,f,t;
for(int i = 1;i<=m;i++){
read(f),read(t),read(a),read(b);
edge[i] = (Edge){f,t,a,b};
}
}
void solve(){
int ans = 0x3f3f3f3f;
sort(edge + 1,edge+m+1,cmp);
for(int i = 1;i<=m;i++)
T.val[n+i] = edge[i].b;
for(int i = 1;i<=n+m;i++)
T.maxn[i] = i;
int x = 0,y = 0,a = 0,b = 0;
for(int i = 1;i<=m;i++){
x = edge[i].from,y = edge[i].to;
a = edge[i].a,b = edge[i].b;
if(x == y) continue;
if(T.findroot(x)!=T.findroot(y))
T.link(n+i,x),T.link(n+i,y);
else{
int t = T.querymax(x,y) - n;
if(edge[t].b < b) continue;
T.cat(n+t,edge[t].from),T.cat(n+t,edge[t].to);
T.link(n+i,x),T.link(n+i,y);
}
if(T.findroot(1) == T.findroot(n))
ans = min(ans,a+edge[T.querymax(1,n)-n].b);
}
if(ans > 1000000000)
print(-1);
else
print(ans);
print('\n');
}
int main(){
init();
solve();
flush();
return 0;
}
|