Harry Potter and the Final Battle
题意
1.求删除一条边后的最短路中的最大值
题解
1.建任意一棵最小路径树,边权等于长度
2.树上1->n的链标记
3.枚举链上一条边被删除
4.dj求最小值
5.所有最小值中取max
6.复杂度:$nmlogm$
具体代码
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
typedef pair<int,int>P;
const int M=1005;
int n,m;
int asdf,head[M];
bool mark[M*100];
struct edge {
int to,cost,nxt,id;
} G[M*100];
void add_edge(int a,int b,int c,int d) {
G[++asdf].to=b;
G[asdf].nxt=head[a];
G[asdf].cost=c;
G[asdf].id=d;
head[a]=asdf;
}
int dis[M],fa[M],cost[M],mi[M];
void dj(int S) {
priority_queue<P,vector<P>,greater<P> >Q;
for(int i=1; i<=n; i++) {
dis[i]=1e9;
}
Q.push(P(dis[S]=0,S));
while(!Q.empty()) {
P p=Q.top();
Q.pop();
int x=p.second;
if(p.first>dis[x])continue;
for(int i=head[x]; i; i=G[i].nxt) {
int y=G[i].to;
if(dis[y]>dis[x]+G[i].cost) {
dis[y]=dis[x]+G[i].cost;
fa[y]=x;
cost[y]=G[i].id;
Q.push(P(dis[y],y));
}
}
}
}
void Dj(int S) {
priority_queue<P,vector<P>,greater<P> >Q;
for(int i=1; i<=n; i++) {
dis[i]=1e9;
}
Q.push(P(dis[S]=0,S));
while(!Q.empty()) {
P p=Q.top();
Q.pop();
int x=p.second;
if(p.first>dis[x])continue;
for(int i=head[x]; i; i=G[i].nxt) {
if(mark[G[i].id])continue;
int y=G[i].to;
if(dis[y]>dis[x]+G[i].cost) {
dis[y]=dis[x]+G[i].cost;
Q.push(P(dis[y],y));
}
}
}
}
int main() {
int cas;
int a,b,c;
scanf("%d",&cas);
while(cas--) {
scanf("%d %d",&n,&m);
asdf=0;
for(int i=1; i<=n; i++)head[i]=0,mi[i]=1e9,mark[i]=0;
for(int i=1; i<=m; i++) {
scanf("%d %d %d",&a,&b,&c);
add_edge(a,b,c,i);
add_edge(b,a,c,i);
}
dj(1);
int now=n,ans=0;
while(now!=1) {
mark[cost[now]]=1;
Dj(1);
ans=max(ans,dis[n]);
mark[cost[now]]=0;
now=fa[now];
}
printf("%d\n",ans==1e9?-1:ans);
}
return 0;
}