nssl1167-桐人的约会【最短路】
生活随笔
收集整理的這篇文章主要介紹了
nssl1167-桐人的约会【最短路】
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
正題
題目大意
去掉一條邊使得最短路最長。
解題思路
這條邊一定在最短路上而最短路最多只有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); }總結(jié)
以上是生活随笔為你收集整理的nssl1167-桐人的约会【最短路】的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 语音广告怎么做的
- 下一篇: P4562-[JXOI2018]游戏【数