P1344 [USACO4.4] to track down the bad milk Pollutant Control minimum cut

  

Title Description

The first day you take over Sanlu milk company has had an unfortunate thing: the company accidentally sent a group of melamine milk. Unfortunately, when you find it, there are melamine milk has entered the delivery network. The delivery network is large and complex relationship. Do you know which of these milk to be sent to retailers, but put his hand to these milk There are many ways. Delivery network consists of a number of warehouses and trucks make up, each truck are each fixed between two warehouses unidirectional transport of milk. When tracing the milk of melamine, it is necessary to ensure that it will not be delivered to the retailers, it is necessary to make certain transport truck transport stop, but stopped each truck will have a certain economic losses. Your task is, to ensure not to bad milk premise retailers to develop a truck stop solution to minimize losses.

Input and output formats

Input formats:

 

The first line: two integers N (2 <= N <= 32), M (0 <= M <= 1000), N represents the number of warehouse, M is the number of trucks. 1 warehouse delivery on behalf of factories, warehouses N represents the melamine milk to be sent to the retailer. First 2..M + 1 line: each line an integer of 3 Si, Ei, Ci. Where Si, Ei represents the departure warehouse truck, purpose warehouse. Ci (0 <= C i <= 2,000,000) represented let the truck stop loss transport.

 

Output formats:

 

Two integers C, T: C represents the minimum loss, T represents the number of trucks with minimal loss of the premise, at least to stop.

 

Sample input and output

Input Sample # 1:  Copy
4 5
1 3 100
3 2 50
2 4 60
1 2 40
2 3 80
Output Sample # 1:  Copy
601 


Number of edge set is clearly a subject Minimal Cut Cuts also requires minimum required

may be adopted a practice tricky

 

#include<bits/stdc++.h>
using namespace std;
//input by bxd
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i>=(b);--i)
#define RI(n) scanf("%d",&(n))
#define RII(n,m) scanf("%d%d",&n,&m)
#define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define RS(s) scanf("%s",s);
#define ll long long
#define pb push_back
#define REP(i,N)  for(int i=0;i<(N);i++)
#define CLR(A,v)  memset(A,v,sizeof A)
//////////////////////////////////
#define inf 0x3f3f3f3f
const int N=4e3+44;
const int M=4e3+54;

struct edge {
    ll to, next, w;
} e[M << 1];
int head[N], pos = 1;
void add(ll x, ll y, ll z) {
    e[++pos] = (edge){y, head[x], z};
    head[x] = pos;
    e[++pos] = (edge){x, head[y], 0};
    head[y] = pos;
}
int level[N];
bool bfs(int s, int t) {
    memset(level, 0, sizeof level);
    queue<ll> q;
    level[s] = 1;
    q.push(s);
    while (!q.empty()) {
        int pos = q.front();
        q.pop();
        for (int i = head[pos]; i; i = e[i].next) {
            int nx = e[i].to;
            if (!e[i].w || level[nx]) continue;
            level[nx] = level[pos] + 1;
            q.push(nx);
        }
    }
    return level[t];
}
ll dfs(ll s, ll t, ll flow) {
    if (s == t) return flow;
    ll ret = 0;
    for (int i = head[s]; flow && i; i = e[i].next) {
        int nx = e[i].to;
        if (level[nx] == level[s] + 1 && e[i].w) {
            ll tmp = dfs(nx, t, min(flow, e[i].w));
            e[i].w -= tmp;
            e[i ^ 1].w += tmp;
            flow -= tmp;
            ret += tmp;
        }
    }
    if (!ret) level[s] = 0;
    return ret;
}
ll dinic(ll s, ll t) {
    ll ret = 0;
    while (bfs(s, t)) ret += dfs(s, t, inf);
    return ret;
}
ll n,m,s,t,T,a,b,c,x,k,c2,sum;

int main()
{
    cin>>n>>m;
    rep(i,1,m)
    {
        scanf("%lld%lld%lld",&a,&b,&c);add(a,b,c*(m+1)+1);
    }
    ll ans=dinic(1,n);
    printf("%lld %lld",ans/(m+1),ans%(m+1));

    return 0;
}
View Code

 






Guess you like

Origin www.cnblogs.com/bxd123/p/10954485.html