#include<stdio.h> #include<stdlib.h> #include<string.h> #include<iostream> #include<algorithm> #define max(x,y) ((x)>(y)?(x):(y)) #define min(x,y) ((x)<(y)?(x):(y)) #define For(i,j,k) for (i=j;i<=k;i++) const int dmax=100100,INF=1000000000; int begin[dmax],to[dmax],w[dmax],next[dmax],c[dmax],st[dmax]; int e,n,m,ans,q[dmax],d[dmax],f[dmax]; bool p[dmax]; void add(int x,int y,int o,int l){ st[e]=x; to[e]=y; w[e]=l; c[e]=o; next[e]=begin[x]; begin[x]=e; e++; } void addedge(int x,int y,int o,int l){ add(x,y,o,l); add(y,x,0,-l); } int spfa(){ int l=0,r=1,i,k; For(i,0,n) d[i]=INF; memset(p,0,sizeof(p)); q[1]=0; d[0]=0; p[0]=1; f[0]=-1; while (l<r){ k=q[++l],p[k]=0; for (i=begin[k];i!=-1;i=next[i]){ if (c[i] && d[to[i]]>d[k]+w[i]){ d[to[i]]=d[k]+w[i]; f[to[i]]=i; if (!p[to[i]]){ q[++r]=to[i]; p[to[i]]=1; } } } } return d[n]!=INF; } void plus(){ int i,k,min=INF; for (i=f[n];i!=-1;i=f[st[i]]) min=min(c[i],min); for (i=f[n];i!=-1;i=f[st[i]]){ c[i]-=min; c[i^1]+=min; ans+=min*w[i]; } } int main(){ int i,j,k,x,y,z; memset(begin,-1,sizeof(begin)); scanf("%d%d",&n,&m); For(i,1,m){ scanf("%d%d%d",&x,&y,&z); addedge(x,y,1,z); addedge(y,x,1,z); } addedge(0,1,2,0); addedge(n,n+1,2,0); n++; while (spfa()) plus(); printf("%d\n",ans); return 0; }