Loading

代码-CF1082G Petya and Graph

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
#define x first
#define y second
#define bg begin()
#define ed end()
#define pb push_back
#define mp make_pair
#define sz(a) int((a).size())
#define R(i,n) for(int i(0);i<(n);++i)
#define L(i,n) for(int i((n)-1);~i;--i)
const int iinf=0x3f3f3f3f;
const ll linf=0x3f3f3f3f3f3f3f3f;

//Data
const int N=2e3+2;
int n,m;

//Flows
int fn,s,t;
vector<int> e[N],to,fw;
void adde(int u,int v,int w){
    e[u].pb(sz(to)),to.pb(v),fw.pb(w);
    e[v].pb(sz(to)),to.pb(u),fw.pb(0);
}
int pre[N];
bool bfs(){
    R(i,fn) pre[i]=-1;
    static queue<int> q; q.push(s);
    while(sz(q)){
        int u=q.front(); q.pop();
        for(int v:e[u])if(fw[v]&&!~pre[to[v]])
            pre[to[v]]=v,q.push(to[v]);
    }
    return ~pre[t];
}
ll flow(){
    ll res(0);
    while(bfs()){
        int f=iinf;
        for(int u=t;u^s;u=to[pre[u]^1]) f=min(f,fw[pre[u]]);
        for(int u=t;u^s;u=to[pre[u]^1]) fw[pre[u]]-=f,fw[pre[u]^1]+=f;
        res+=f;
    }
    return res;
}

//Main
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin>>n>>m,fn=(t=(s=n+m)+1)+1;
    R(i,n){int a; cin>>a,adde(i,t,a);}
    ll ns(0);
    R(i,m){
        int u,v,a; cin>>u>>v>>a,--u,--v;
        adde(i+n,u,iinf),adde(i+n,v,iinf);
        adde(s,i+n,a),ns+=a;   
    }
    cout<<ns-flow()<<'\n';
    return 0;
}
posted @ 2020-11-13 21:03  George1123  阅读(44)  评论(0编辑  收藏  举报