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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
| #include<bits/stdc++.h> #define int long long #define ull unsigned long long #define maxn 200005 #define put() putchar('\n') #define Tp template<typename Ty> #define Ts template<typename Ty,typename... Ar> using namespace std; inline void read(int &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,Ty t){cerr<<f<<'='<<t<<endl;} Ts void _debug(char* f,Ty x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);} Tp ostream& operator<<(ostream& os,vector<Ty>& V){os<<"[";for(auto& vv:V) os<<vv<<",";os<<"]";return os;} #define gdb(...) _debug(#__VA_ARGS__,__VA_ARGS__) }using namespace Debug; int n,m; const int inf=1e10; int head=1,h[maxn]; struct node{ int to,z,w,flag; inline void add(int x,int y,int val) { to=y;z=h[x];h[x]=head;w=val; } }a[maxn*4]; int vis[maxn],diss[maxn],dist[maxn],flag[maxn],pre[maxn]; int stac[maxn],tot,L[maxn],R[maxn],id[maxn]; inline int dfs1(int x) { if (flag[x]) return L[x]=x; if (L[x]) return L[x]; return L[x]=dfs1(a[pre[x]^1].to); } inline int dfs2(int x) { if (flag[x]) return R[x]=x; if (R[x]) return R[x]; return R[x]=dfs2(a[pre[x]^1].to); } int f[maxn<<2]; inline void Update(int l,int r,int rt,int head,int tail,int k) { if (head<=l&&r<=tail) return f[rt]=min(f[rt],k),void(); int mid=l+r>>1; if (head<=mid) Update(l,mid,rt<<1,head,tail,k); if (tail>mid) Update(mid+1,r,rt<<1|1,head,tail,k); } inline int Query(int l,int r,int rt,int head) { if (l==r) return f[rt]; int mid=l+r>>1,tmp=inf; if (head<=mid) tmp=Query(l,mid,rt<<1,head); else tmp=Query(mid+1,r,rt<<1|1,head); return min(tmp,f[rt]); } int ans=0,anss=0; priority_queue<pair<int,int> >q; signed main(void){
int i,x,y,z,tmp; read(n);read(m); for (i=1;i<=m;i++) { read(x);read(y);read(z); a[++head].add(x,y,z); a[++head].add(y,x,z); } memset(f,0x3f,sizeof(f)); memset(diss,0x3f,sizeof(diss)); memset(dist,0x3f,sizeof(dist)); diss[1]=0;q.push({0,1}); while (!q.empty()) { x=q.top().second;q.pop(); if(vis[x])continue;vis[x]=1; for (i=h[x];i;i=a[i].z) if (diss[a[i].to]>diss[x]+a[i].w) { pre[a[i].to]=i; diss[a[i].to]=diss[x]+a[i].w; q.push({-diss[a[i].to],a[i].to}); } } int now=n;flag[n]=1,stac[tot=1]=n; while (pre[now]) { i=pre[now]; flag[a[i^1].to]=1;a[i^1].flag=a[i].flag=1; stac[++tot]=a[i^1].to,now=a[i^1].to; } reverse(stac+1,stac+1+tot); for (i=1;i<=tot;i++) id[stac[i]]=i;
for (i=1;i<=n;i++) L[i]=dfs1(i); dist[n]=0; memset(pre,0,sizeof(pre)); memset(vis,0,sizeof(vis)); while (!q.empty()) q.pop(); q.push({0,n}); while (!q.empty()) { x=q.top().second;q.pop(); if (vis[x]) continue; vis[x]=1; for (i=h[x];i;i=a[i].z) if ((a[i].flag&&pre[a[i].to]!=i&&dist[a[i].to]>=dist[x]+a[i].w)||dist[a[i].to]>dist[x]+a[i].w) { pre[a[i].to]=i; dist[a[i].to]=dist[x]+a[i].w;
q.push({-dist[a[i].to],a[i].to}); } } for (i=1;i<=n;i++) R[i]=dfs2(i); for (i=2;i<=head;i++) { x=a[i^1].to,y=a[i].to,z=a[i].w; if (a[i].flag) continue;
if (id[L[x]]<id[R[y]]) Update(1,tot-1,1,id[L[x]],id[R[y]]-1,diss[x]+z+dist[y]); } for (i=1;i<tot;i++) { tmp=Query(1,tot-1,1,i);
if (tmp>1e15) continue; if (ans<tmp) ans=tmp,anss=1;else if (ans==tmp) anss++; } if (ans==diss[n]) printf("%lld %lld\n",diss[n],m);else printf("%lld %lld\n",ans,anss); return 0; }
|