1 /**************************************************************
2 Problem: 3931
3 User: Tunix
4 Language: C++
5 Result: Accepted
6 Time:316 ms
7 Memory:12256 kb
8 ****************************************************************/
9
10 //BZOJ 3931
11 #include<vector>
12 #include<queue>
13 #include<cstdio>
14 #include<cstring>
15 #include<cstdlib>
16 #include<iostream>
17 #include<algorithm>
18 #define rep(i,n) for(int i=0;i<n;++i)
19 #define F(i,j,n) for(int i=j;i<=n;++i)
20 #define D(i,j,n) for(int i=j;i>=n;--i)
21 #define pb push_back
22 using namespace std;
23 typedef long long LL;
24 inline int getint(){
25 int v=0,sign=1; char ch=getchar();
26 while(ch<‘0‘||ch>‘9‘){ if (ch==‘-‘) sign=-1; ch=getchar();}
27 while(ch>=‘0‘&&ch<=‘9‘){ v=v*10+ch-‘0‘; ch=getchar();}
28 return v*sign;
29 }
30 const int N=1010,M=200010;
31 const LL INF=1e15;
32 /******************tamplate*********************/
33
34 int head[N],to[M<<1],nxt[M<<1],l[M<<1],cnt;
35 typedef long long LL;
36 void add(int x,int y,int z){
37 to[++cnt]=y; nxt[cnt]=head[x]; head[x]=cnt; l[cnt]=z;
38 to[++cnt]=x; nxt[cnt]=head[y]; head[y]=cnt; l[cnt]=z;
39 }
40 int n,m,a[N],vis[N],tot;
41 LL ans,dis[N],c[N];
42
43 void Dij();
44 struct edge{int to;LL v;};
45 struct Net{
46 edge E[M<<1];
47 int head[N],nxt[M<<1],cnt;
48 void ins(int x,int y,LL z){
49 E[++cnt]=(edge){y,z}; nxt[cnt]=head[x]; head[x]=cnt;
50 }
51 void add(int x,int y,LL z){
52 ins(x,y,z); ins(y,x,0);
53 }
54 int S,T,d[N],cur[N];
55 queue<int>Q;
56 bool mklevel(){
57 memset(d,-1,sizeof d);
58 d[S]=0;
59 Q.push(S);
60 while(!Q.empty()){
61 int x=Q.front(); Q.pop();
62 for(int i=head[x];i;i=nxt[i])
63 if (d[E[i].to]==-1 && E[i].v){
64 d[E[i].to]=d[x]+1;
65 Q.push(E[i].to);
66 }
67 }
68 return d[T]!=-1;
69 }
70 int dfs(int x,LL a){
71 if (x==T) return a;
72 LL flow=0;
73 for(int &i=cur[x];i && flow<a;i=nxt[i])
74 if (d[E[i].to]==d[x]+1 && E[i].v){
75 LL f=dfs(E[i].to,min(E[i].v,a-flow));
76 E[i].v-=f;
77 E[i^1].v+=f;
78 flow+=f;
79 }
80 if (!flow) d[x]=-1;
81 return flow;
82 }
83 void Dinic(){
84 while(mklevel()){
85 F(i,0,T) cur[i]=head[i];
86 ans+=dfs(S,INF);
87 }
88 }
89 void init(){
90 S=0,T=n*2+1; cnt=1;
91 Dij();
92 F(i,1,n) add(i,n+i,c[i]);
93 add(S,1,INF); add(n+n,T,INF);
94 Dinic();
95 printf("%lld\n",ans);
96 }
97 }G1;
98 typedef pair<int,int> pii;
99 #define mp make_pair
100 #define fi first
101 #define se second
102
103 priority_queue<pii,vector<pii>,greater<pii> >Q;
104 void Dij(){
105 memset(dis,0x3f,sizeof dis);
106 dis[1]=0;
107 Q.push(mp(0,1));
108 while(!Q.empty()){
109 int x=Q.top().se; Q.pop();
110 if (vis[x]) continue;
111 vis[x]=++tot;
112 a[tot]=x;
113 for(int i=head[x];i;i=nxt[i])
114 if (dis[to[i]]>dis[x]+l[i]){
115 dis[to[i]]=dis[x]+l[i];
116 Q.push(mp(dis[to[i]],to[i]));
117 }
118 }
119 F(x,1,n)
120 for(int i=head[x];i;i=nxt[i])
121 if (dis[to[i]]==dis[x]+l[i])
122 G1.add(x+n,to[i],INF);
123 }
124 int main(){
125 #ifndef ONLINE_JUDGE
126 freopen("3931.in","r",stdin);
127 freopen("3931.out","w",stdout);
128 #endif
129 n=getint(); m=getint();
130 F(i,1,m){
131 int x=getint(),y=getint(),z=getint();
132 add(x,y,z);
133 }
134 F(i,1,n) c[i]=getint();
135 c[1]=c[n]=INF;
136 G1.init();
137 return 0;
138 }