两种方法,一种SPFA动态加边,一种lct维护最小生成树...
但是思路大致一样,考虑枚举其中一种值,比如a,然后用b的值做最小生成树,然后找到1到n之间的这条链的最大值即为答案
c++代码如下:
1.SPFA动态加边:
#include
#define rep(i,x,y) for(register int i = x ;i <= y; ++ i)
#define repd(i,x,y) for(register int i = x ; i >= y; -- i)
using namespace std;
typedef long long ll;
templateinline void read(T&x)
{
x = 0;char c;int sign = 1;
do { c = getchar(); if(c == '-') sign = -1; }while(!isdigit(c));
do { x = x * 10 + c - '0'; c = getchar(); }while(isdigit(c));
x *= sign;
}
const int N = 1e5+500,M = 2e5+500;
int n,m,ans,d[N];
int head[N],to[M],nxt[M],w[M],tot;
struct Str { int u,v,a,b; }e[M];
bool vis[N];
const bool cmp(Str a,Str b) { return a.a < b.a || a.a == b.a && a.b < b.b; }
inline void add(int x,int y,int val)
{
w[tot] = val;
to[tot] = y;
nxt[tot] = head[x];
head[x] = tot ++;
}
int main()
{
memset(head,-1,sizeof head); ans = 1e9+7;
memset(d,0x3f,sizeof d); d[1] = 0;
read(n); read(m);
rep(i,1,m)
{
read(e[i].u); read(e[i].v);
read(e[i].a); read(e[i].b);
}
sort(e + 1,e + m + 1,cmp);
queueq;
d[1] = 0;
rep(i,1,m)
{
add(e[i].u,e[i].v,e[i].b);
add(e[i].v,e[i].u,e[i].b);
q.push(1);q.push(e[i].u);q.push(e[i].v);
vis[e[i].u] = vis[e[i].v] = vis[1] = 1;
while(!q.empty())
{
int x = q.front();q.pop();vis[x] = 0;
for(register int i = head[x];~i;i = nxt[i])
if(d[to[i]] > max(d[x],w[i]))
{
d[to[i]] = max(d[x],w[i]);
if(!vis[to[i]])
q.push(to[i]),vis[to[i]] = 1;
}
}
ans = min(ans,e[i].a + d[n]);
}
cout << (ans == 1e9+7?-1:ans) << endl;
return 0;
}
2:lct 维护最小生成树:
#include
#define rep(i,x,y) for(register int i = x ;i <= y; ++ i)
#define repd(i,x,y) for(register int i = x ; i >= y; -- i)
using namespace std;
typedef long long ll;
templateinline void read(T&x)
{
x = 0;char c;int sign = 1;
do { c = getchar(); if(c == '-') sign = -1; }while(!isdigit(c));
do { x = x * 10 + c - '0'; c = getchar(); }while(isdigit(c));
x *= sign;
}
const int N = 1e5+500,M = 2e5+500;
int n,m,ans,p[N];
struct Str { int u,v,a,b; }e[M];
struct Link_Cut_Tree
{
int ch[N+M][2],f[N+M],num[N+M],val[N+M],rev[N+M];
#define lc ch[x][0]
#define rc ch[x][1]
inline bool get(int x) { return x == ch[f[x]][1]; }
inline bool isroot(int x) { return !(ch[f[x]][1] == x) && !(ch[f[x]][0] == x) ; }
inline void update(int x)
{
val[x] = x;
if(num[val[x]] < num[val[lc]])
val[x] = val[lc];
if(num[val[x]] < num[val[rc]])
val[x] = val[rc];
}
inline void rotate(int x)
{
int fa = f[x],t = get(x);
f[x] = f[fa];
if(!isroot(fa)) ch[f[x]][get(fa)] = x;
f[fa] = x;
ch[fa][t] = ch[x][t^1];
if(ch[x][t^1]) f[ch[x][t^1]] = fa;
ch[x][t^1] = fa;
update(fa); update(x);
}
inline void put_down(int x)
{
if(!isroot(x) ) put_down(f[x]);
if(rev[x])
{
rev[x] ^= 1;rev[lc] ^= 1;rev[rc] ^= 1;
swap(lc,rc);
}
}
inline void splay(int x)
{
put_down(x);
while(!isroot(x))
{
if(!isroot(f[x])) rotate(get(f[x]) == get(x) ? f[x] : x);
rotate(x);
}
}
inline void access(int x)
{
int t = 0;
while(x)
{
splay(x);
ch[x][1] = t;
update(x);
t = x;
x = f[x];
}
}
inline void mkroot(int x) { access(x); splay(x); rev[x] ^= 1; }
inline void Link(int x,int y) { mkroot(x); f[x] = y; }
inline void cut(int x,int y)
{
mkroot(x); access(y); splay(y);
f[x] = 0; ch[y][0] = 0;
update(x); update(y);
}
int query(int x,int y)
{
mkroot(x);
access(y);
splay(y);
return val[y];
}
}lct;
int get_fa(int x) { return p[x] == x ? x : p[x] = get_fa(p[x]); }
const bool cmp(Str a,Str b) { return a.a < b.a; }
int main()
{
ans = 1e9+7;
read(n); read(m);
rep(i,1,m)
{
read(e[i].u); read(e[i].v);
read(e[i].a); read(e[i].b);
}
sort(e + 1,e + 1 + m,cmp);
rep(i,1,n) p[i] = i;
rep(i,1,m) lct.num[i + n] = e[i].b;
rep(i,1,m)
{
int x = get_fa(e[i].u),y = get_fa(e[i].v);
if(x != y)
{
p[x] = y;
lct.Link(e[i].u,i + n);
lct.Link(e[i].v,i + n);
}else
{
int cnt = lct.query(e[i].u,e[i].v);
if(e[i].b < lct.num[cnt])
{
lct.cut(e[cnt - n].u,cnt); lct.cut(e[cnt - n].v,cnt);
lct.Link(e[i].u,i + n); lct.Link(e[i].v,i + n);
}
}
if(get_fa(1) == get_fa(n)) ans = min(ans,e[i].a + lct.num[lct.query(1,n)]);
}
cout << (ans == 1e9+7 ? -1 : ans) << endl;
return 0;
}