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
|
#include<iostream> #include<cstring> #include<queue> using namespace std; #define Debug(x) cout<<#x<<':'<<x<<endl #define INF 0x7fffffff typedef long long ll; typedef pair<ll,ll> P; const ll maxn=1e3+11; const ll maxm=1e6+11; struct Edge{ ll fr,to,w,ne; }e[maxm]; ll head[maxn],dis[maxn],vis[maxn],pre[maxn]; ll n,m,start,flag,tmp; priority_queue<P,vector<P>,greater<P>> q; void init(){ memset(head,0,sizeof(head));tmp=2; } void add(ll u,ll v,ll w){ e[tmp]={u,v,w};e[tmp].ne=head[u];head[u]=tmp++; } void dij(){ memset(dis,0x1f,sizeof(dis));memset(vis,0,sizeof(vis)); q.push({0,start});dis[start]=0; while(!q.empty()){ ll u=q.top().second;q.pop(); if(vis[u]) continue; vis[u]=1; for(ll i=head[u];i;i=e[i].ne){ ll v=e[i].to; if(!vis[v] && dis[u]+e[i].w<dis[v]){ dis[v]=dis[u]+e[i].w; if(flag) pre[v]=i; q.push({dis[v],v}); } } } } int main(){ ios::sync_with_stdio(false); cin.tie(0); while(cin>>n>>m){ init(); for(ll i=1;i<=m;i++){ ll u,v,w;cin>>u>>v>>w; add(u,v,w);add(v,u,w); } start=1;flag=1; dij(); ll ans=dis[n]; flag=0; for(ll v=n;v!=1;){ ll i=pre[v]; ll w=e[i].w; e[i].w=e[i^1].w=INF; dij(); ans=max(ans,dis[n]); e[i].w=e[i^1].w=w; v=e[i].fr; } cout<<ans<<endl; } return 0; }
|