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 155 156 157 158 159 160 161 162 163
| #include<iostream> #include<cstdio> #include<algorithm> using namespace std; long long read() { long long x=0,f=1; char c=getchar(); while(!isdigit(c)){if(c=='-') f=-1;c=getchar();} while(isdigit(c)){x=x*10+c-'0';c=getchar();} return x*f; } const int M=100000+100; const int N=50000+100; const int T=N+M; struct road { int s,t,a,b; }e[M]; int n,m; bool cmp(road x,road y) { return x.a<y.a; } struct LCT { int son[T][2],fa[T],lazy[T],MAX[T],num[T],mstack[T],top; inline void Update(int x) { MAX[x]=0; if(num[MAX[son[x][0]]]>=num[x] and num[MAX[son[x][0]]]>=num[MAX[son[x][1]]]) MAX[x]=MAX[son[x][0]]; if(num[MAX[son[x][1]]]>=num[x] and num[MAX[son[x][1]]]>=num[MAX[son[x][0]]]) MAX[x]=MAX[son[x][1]]; if(num[x]>=num[MAX[son[x][0]]] and num[x]>=num[MAX[son[x][1]]]) MAX[x]=x; } inline void Mirror(int x) { lazy[x]=!lazy[x],swap(son[x][0],son[x][1]); } inline void PushDown(int x) { if(lazy[x]==0) return; lazy[x]=0; Mirror(son[x][0]),Mirror(son[x][1]); } inline bool IsRoot(int x) { return x!=son[fa[x]][0] and x!=son[fa[x]][1]; } inline void Rotate(int x,int type) { int y=fa[x],z=fa[y]; if(IsRoot(y)==false) son[z][y==son[z][1]]=x; fa[x]=z; son[y][!type]=son[x][type],fa[son[x][type]]=y; son[x][type]=y,fa[y]=x; Update(y),Update(x); } inline void Splay(int x) { mstack[top=1]=x; for(int i=x;i!=0;i=fa[i]) mstack[++top]=fa[i]; for(int i=top;i>=1;i--) PushDown(mstack[i]); while(IsRoot(x)==false) { if(x==son[fa[x]][fa[x]==son[fa[fa[x]]][1]] and IsRoot(fa[x])==false) Rotate(fa[x],x==son[fa[x]][0]), Rotate(x,x==son[fa[x]][0]); else Rotate(x,x==son[fa[x]][0]); } } void Access(int x) { for(int t=0;x!=0;t=x,x=fa[x]) Splay(x),son[x][1]=t,fa[t]=x,Update(x); } inline void MakeRoot(int x) { Access(x),Splay(x); Mirror(x); } inline int FindRoot(int x) { Access(x),Splay(x); while(son[x][0]!=0) PushDown(x),x=son[x][0]; Splay(x); return x; } inline void Link(int x,int y) { if(FindRoot(x)==FindRoot(y)) return; MakeRoot(x); fa[x]=y; } inline void Split(int x,int y) { MakeRoot(x); Access(y),Splay(y); } inline void Cut(int x,int y) { Split(x,y); if(x==son[y][0] and fa[x]==y) { son[y][0]=fa[x]=0; Update(y); } } inline int Query(int x,int y) { MakeRoot(x); Access(y),Splay(y); return MAX[y]; } inline void AddLine(int x) { if(e[x].s==e[x].t) return; num[n+x]=e[x].b,MAX[n+x]=n+x; if(FindRoot(e[x].s)!=FindRoot(e[x].t)) { Link(n+x,e[x].s),Link(n+x,e[x].t); return ; } int t=Query(e[x].s,e[x].t); if(num[n+x]<num[t]) { Cut(e[t-n].s,t); Cut(e[t-n].t,t); Link(e[x].s,n+x); Link(e[x].t,n+x); } } inline int Query2() { if(FindRoot(n)!=FindRoot(1)) return 0x3f3f3f3f; return num[Query(1,n)]; } }lct; int main() { n=read(),m=read(); for(int i=1;i<=m;i++) e[i].s=read(),e[i].t=read(),e[i].a=read(),e[i].b=read();
sort(e+1,e+1+m,cmp); int ans=0x3f3f3f3f; for(int i=1;i<=m;i++) { lct.AddLine(i); ans=min(ans,e[i].a+lct.Query2()); }
if(ans==0x3f3f3f3f) printf("-1"); else printf("%d",ans); return 0; }
|