求最小割集的最少边数。
很神的方法 http://hi.baidu.com/mengyun1993/blog/item/c30d193c9a85932870cf6cda.html
用的党的模板。
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
#include <string.h>
#include <string>
#include <algorithm>
#define MID(x,y) ( ( x + y ) >> 1 )
#define L(x) ( x << 1 )
#define R(x) ( x << 1 | 1 )
#define FOR(i,s,t) for(int i=s; i<t; i++)
#define BUG puts("here!!!")
const long long INF = 2100000000000560ll;
using namespace std;
class Arc{
public:
int to;
long long w;
Arc *next,*anti;
Arc *ass( int tt,long long ww,Arc* &b ){
to = tt;w = ww;next = b;b = this;return this;
}
};
template<class Tp,int Vsize, int Esize >
class Network{
private:
Arc A[ 2*Esize+10 ],*biao[ Vsize ];
int countt,d[ Vsize ],S,T,N;
int bfs( void ){
queue<int> q;
fill( d,d+Vsize,-1 );
d[ S ] = 0;
q.push( S );
while( !q.empty() ){
int u = q.front();q.pop();
for( Arc *p=biao[u]; p != NULL; p=p->next )
if( d[p->to] == -1 && p->w > 0 )
d[p->to] = d[u]+1,q.push(p->to);
}
return d[T] != -1;
}
Tp dinic( int u,Tp sum ){
Tp f,o;
if( u == T ) return sum;
o = sum;
for( Arc *p=biao[u]; p != NULL && sum; p=p->next )
if( d[p->to] == d[u]+1 && p->w > 0 ){
f = dinic(p->to,min(p->w,sum));
p->w -= f;p->anti->w += f;
sum -= f;
}
return o-sum;
}
public:
Network( void ) {}
Network( int n ) { init(n) ;}
void init( int n ){
countt = 0;N = n;
for( int i = 0; i <= n; i++ )
biao[i] = NULL;
}
void add( int from,int to,Tp w ){
Arc *p1 = A[countt++].ass( to,w ,biao[from]);
Arc *p2 = A[countt++].ass( from,0,biao[ to] );
p1->anti = p2; p2->anti = p1;
}
Tp MaxFlowDinic( int s, int t ){
S = s;T = t;
Tp total = 0;
while( bfs() )
total += dinic( S,INF );
return total;
}
};
Network<long long, 1010, 400010> net;
int main()
{
int n, m, ncases;
int u, v, cost, dir;
int ind = 1;
scanf("%d", &ncases);
while( ncases-- )
{
scanf("%d%d", &n, &m);
net.init(n);
FOR(i, 0, m)
{
scanf("%d%d%d%d", &u, &v, &cost, &dir);
net.add(u, v, cost*1ll*(m+1)+1);
if( dir)
net.add(v, u, cost*1ll*(m+1)+1);
}
long long ans = net.MaxFlowDinic(0, n-1);
printf("Case %d: %I64d\n", ind++, ans%(m+1));
}
return 0;
}