# 「NOI2018」归程

## Code:

#include<bits/stdc++.h>
#define int long long
#define mp make_pair
#define fir first
#define sec second
using namespace std;
const int N=2e5+11;
const int M=4e5+11;
const int inf=4000000000;
int lastans,f[M][20],dep[M],v[M],dis[M];
vector<int> g[M];
priority_queue<pair<int,int> > q;
struct Edge{int nxt,to,val;}edge[M<<1];
struct Ed{int x,y,val;}E[M];
inline bool cmp(Ed a,Ed b){return a.val>b.val;}
inline int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
void ins(int x,int y,int z){
edge[cnt].val=z;
}
g[x].push_back(y);
g[y].push_back(x);
}
void dijkstra(){
for(int i=2;i<=2*n;i++) dis[i]=inf;
q.push(mp(0,1));
while(!q.empty()){
auto u=q.top();q.pop();
if(dis[u.sec]!=-u.fir) continue;
int x=u.sec;
int y=edge[i].to;
if(dis[y]>dis[x]+edge[i].val){
dis[y]=dis[x]+edge[i].val;
q.push(mp(-dis[y],y));
}
}
}
}
void kruskal(){
tot=n;
for(int i=1;i<=n;i++) fa[i]=i;
sort(E+1,E+m+1,cmp);
for(int i=1;i<=m;i++){
int x=E[i].x,y=E[i].y;
x=find(x),y=find(y);
if(x==y) continue;
++tot;v[tot]=E[i].val;
rt=fa[x]=fa[y]=fa[tot]=tot;
}
for(int i=1;i<=tot;i++) find(i);
}
void dfs(int x){
for(int i=0;i<g[x].size();i++){
int y=g[x][i];
if(y==f[x][0]) continue;
dep[y]=dep[x]+1;
f[y][0]=x;dfs(y);
dis[x]=min(dis[y],dis[x]);
}
}
void trans(){
for(int i=1;i<=19;i++)
for(int j=1;j<=tot;j++)
f[j][i]=f[f[j][i-1]][i-1];
}
int lca(int x,int y){
if(dep[y]>dep[x]) swap(x,y);
for(int i=19;i>=0;i--)
if(dep[f[x][i]]>=dep[y]) x=f[x][i];
if(x==y) return x;
for(int i=19;i>=0;i--)
if(f[x][i]!=f[y][i]) x=f[x][i],y=f[y][i];
return f[x][0];
}
void decode(int &x,int &limit){
x=(x+K*lastans-1)%n+1;
limit=(limit+K*lastans)%(S+1);
}
int calc(int x,int lim){
for(int i=19;i>=0;i--)
if(f[x][i]&&v[f[x][i]]>lim) x=f[x][i];
return dis[x];
}
void format(){
lastans=cnt=0;
memset(f,0,sizeof(f));
memset(v,0,sizeof(v));
memset(dep,0,sizeof(dep));
for(int i=1;i<=tot;i++) g[i].clear();
}
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
while(isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;
}
signed main(){
freopen("return.in","r",stdin);
freopen("return.out","w",stdout);
while(T--){
format();
for(int i=1;i<=m;i++){
ins(x,y,z),ins(y,x,z);
E[i]=(Ed){x,y,h};
}
dijkstra();kruskal();
dfs(rt);trans();