正题
题目大意
去掉一条边使得最短路最长。
解题思路
这条边一定在最短路上而最短路最多只有n−1n-1n−1条边,所以直接枚举最短路上的边。复杂度O(nmK)O(nmK)O(nmK)
codecodecode
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
const int N=1100;
struct edge{int to,w,next;
}a[N*N];
int n,m,ans,tot;
int f[N],pre[N],ls[N];
queue<int> q;
bool v[N];
void adde(int x,int y,int w)
{a[++tot].to=y;a[tot].next=ls[x];ls[x]=tot;a[tot].w=w;
}
int spfa(bool mark)
{memset(f,0x3f,sizeof(f));q.push(1);v[1]=1;f[1]=0;while(!q.empty()){int x=q.front();for(int i=ls[x];i;i=a[i].next){int y=a[i].to;if(f[x]+a[i].w<f[y]){f[y]=f[x]+a[i].w;if(mark)pre[y]=i;if(!v[y]){v[y]=1;q.push(y);}}}v[x]=0;q.pop();}return f[n];
}
int main()
{scanf("%d%d",&n,&m);tot=1;for(int i=1;i<=m;i++){int x,y,w;scanf("%d%d%d",&x,&y,&w);adde(x,y,w);adde(y,x,w);}spfa(1);int x=n;while(x){int w=a[pre[x]].w;a[pre[x]].w=a[pre[x]^1].w=2147483647/3;ans=max(ans,spfa(0));a[pre[x]].w=a[pre[x]^1].w=w;x=a[pre[x]^1].to;}printf("%d",ans);
}