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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| #include<bits/stdc++.h> #define int long long #define ull unsigned long long #define maxn 400005 #define put() putchar('\n') #define Tp template<typename T> #define Ts template<typename T,typename... Ar> using namespace std; Tp void read(T &x){ int f=1;x=0;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();} x*=f; } namespace Debug{ Tp void _debug(char* f,T t){cerr<<f<<'='<<t<<endl;} Ts void _debug(char* f,T x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);} #define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__) }using namespace Debug; #define fi first #define se second #define mk make_pair const int mod=1e9+7; int power(int x,int y=mod-2) { int sum=1; while (y) { if (y&1) sum=sum*x%mod; x=x*x%mod;y>>=1; } return sum; } int n,m; struct yyy { int to,w,id; bool operator <(const yyy &tmp) const { return w<tmp.w; } }; struct node { int x,y,z; }e[maxn]; vector<yyy>to[maxn]; int dis[maxn]; int L[maxn],R[maxn],vis[maxn][2]; priority_queue<pair<int,pair<int,int> > >q; signed main(void){ int i,x,y,z,l,r,fl; read(n);read(m); for (i=1;i<=m;i++) { read(x);read(y);read(z); to[x].push_back((yyy){y,z,i}); to[y].push_back((yyy){x,z,i+m}); e[i].x=x,e[i].y=y,e[i].z=z; e[i+m].x=y,e[i+m].y=x,e[i+m].z=z; } for (i=1;i<=n;i++) sort(to[i].begin(),to[i].end()),R[i]=to[i].size()-1; memset(dis,0x3f,sizeof(dis)); for (auto tmp:to[1]) { dis[tmp.id]=tmp.w; q.push(mk(-dis[tmp.id],mk(tmp.id,1))); q.push(mk(-dis[tmp.id]-tmp.w,mk(tmp.id,0))); } while (!q.empty()) { i=q.top().se.fi;fl=q.top().se.se;q.pop(); if (vis[i][fl]) continue;vis[i][fl]=1; x=e[i].y; if (fl==0) { l=L[x]; while (l<to[x].size()&&to[x][l].w<=e[i].z) { if (dis[to[x][l].id]>dis[i]+e[i].z) { dis[to[x][l].id]=dis[i]+e[i].z; q.push(mk(-dis[to[x][l].id],mk(to[x][l].id,1))); q.push(mk(-dis[to[x][l].id]-e[to[x][l].id].z,mk(to[x][l].id,0))); } l++; } L[x]=l; } if (fl==1) { r=R[x]; while (r>=0&&to[x][r].w>=e[i].z) { if (dis[to[x][r].id]>dis[i]+e[to[x][r].id].z) { dis[to[x][r].id]=dis[i]+e[to[x][r].id].z; q.push(mk(-dis[to[x][r].id],mk(to[x][r].id,1))); q.push(mk(-dis[to[x][r].id]-e[to[x][r].id].z,mk(to[x][r].id,0))); } r--; } R[x]=r; } } int ans=1e18; for (i=1;i<=m*2;i++) if (e[i].y==n) ans=min(ans,dis[i]+e[i].z); printf("%lld\n",ans); return 0; }
|