Starlight237 @ 2019-02-09 21:07:20
#include <bits/stdc++.h>
using namespace std;
#define reg register
#define ll long long
static char in[50000000],out[20],ch[20],*p=in,*q=out,*t=ch;
inline int read(){
reg int x=0;
while(*p<48)++p;
while(*p>47)x=(x+(x<<2)<<1)+(*p++^48);
return x;
}
inline void write(reg ll x){
if(!x)putchar('0');
else {
while(x)*t++=x%10+48,x/=10;
while(t!=ch)*q++=*--t;
}
}
static int n,m,dis[1000001],head[1000001],head1[1000001],N;
struct Node{
int u,v,w,nxt;
}edges[1000001],edges1[1000001];
struct node{
int dis,to;
};
struct cmp{
inline bool operator()(node a,node b){
return a.dis>b.dis;
}
};
priority_queue<node,vector<node>,cmp > Q;
inline void addedge(int u,int v,int w){
edges[++N]=Node{u,v,w,head[u]},head[u]=N;
}
inline void addedge1(int u,int v,int w){
edges1[N]=Node{u,v,w,head1[u]},head1[u]=N;
}
int main(){
freopen("1.in","r",stdin);
fread(in,1,10000000,stdin);
n=read(),m=read();
while(m--){
int u=read(),v=read(),w=read();
u!=v&&(addedge(u,v,w),addedge1(v,u,w),0);
}
int *S=dis,*E=dis+n;
while(S!=E)*++S=2000000000;
dis[1]=0,Q.push(node{0,1});
while(!Q.empty()){
node now=Q.top();Q.pop();
if(now.dis==dis[now.to]){
reg int v,w,tp=now.to;
for(reg int i=head[now.to];i;i=edges[i].nxt)
v=edges[i].v,w=edges[i].w,
dis[v]>dis[tp]+w&&(dis[v]=dis[tp]+w,Q.push(node{dis[v],v}),0);
}
}reg long long ans=0;
S=dis+1,E=dis+n;
while(S!=E)ans+=*++S;
S=dis+1,E=dis+n;
while(S!=E)*++S=2000000000;
Q.push(node{0,1});
while(!Q.empty()){
node now=Q.top();Q.pop();
if(now.dis==dis[now.to]){
reg int v,w,tp=now.to;
for(reg int i=head1[now.to];i;i=edges1[i].nxt)
v=edges1[i].v,w=edges1[i].w,
dis[v]>dis[tp]+w&&(dis[v]=dis[tp]+w,Q.push(node{dis[v],v}),0);
}
}
S=dis+1,E=dis+n;
while(S!=E)ans+=*++S;
write(ans);
fwrite(out,1,q-out,stdout);
return 0;
}