#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
#define R0(i,n) for(int i=0;i<n;++i)
#define R1(i,n) for(int i=1;i<=n;++i)
#define cl(x,c) memset(x,c,sizeof x)
#define maxn 15020
#define INF 707406378
using namespace std;
typedef long long ll;
int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
struct Edge {int from, to, cap, flow, cost;};
int S, T;ll ans=0;
vector<int> G[maxn];
vector<Edge> edges;
void add(int from, int to, int cap, int cost) {
edges.push_back((Edge){from, to, cap, 0, cost});
edges.push_back((Edge){to, from, 0, 0, -cost});
int m = edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
int d[maxn], p[maxn], A[maxn];
bool inq[maxn];
int a[801];
bool bf() {
cl(inq,0);
for(int i = S; i <= T; i++) d[i] = INF;
d[S] = 0; inq[S] = 1; p[S] = 0; A[S] = INF;
queue<int> Q;
Q.push(S);
while(!Q.empty()) {
int x = Q.front(); Q.pop();
inq[x] = 0;
R0(i,G[x].size()){
Edge& e = edges[G[x][i]];
if(e.cap > e.flow && d[e.to] > d[x] + e.cost) {
d[e.to] = d[x] + e.cost;
A[e.to] = min(A[x], e.cap-e.flow);
p[e.to] = G[x][i];
if(!inq[e.to]) { Q.push(e.to); inq[e.to] = 1; }
}
}
}
if(d[T] == INF) return 0;
ans += d[T]*A[T];
int x = T;
while(x != S) {
edges[p[x]].flow += A[T];
edges[p[x]^1].flow -= A[T];
x = edges[p[x]].from;
}
return 1;
}
int main(){
int n=read(),m=read();T=2*n+1;
R1(i,n)a[i]=read();
R1(i,n)add(0,i,1,0),add(i+n,T,1,0),add(0,i+n,1,a[i]);
R1(i,m){
int u=read(),v=read(),w=read();
if(u>v)swap(u,v);
add(u,v+n,1,w);
}
while(bf());
printf("%d\n",ans);
return 0;
}
codevs2133 星际竞速
最新推荐文章于 2018-11-25 10:12:55 发布