Harry Potter notices some Death Eaters try to slip into Castle. The Death Eaters hide in the most depths of Forbidden Forest. Harry need stop them as soon as.

The Forbidden Forest is mysterious. It consists of N nodes numbered from 0 to N-1. All of Death Eaters stay in the node numbered 0. The position of Castle is node n-1. The nodes connected by some roads. Harry need block some roads by magic and he want to minimize the cost. But it’s not enough, Harry want to know how many roads are blocked at least.
InputInput consists of several test cases. The Forbidden Forest is mysterious. It consists of N nodes numbered from 0 to N-1. All of Death Eaters stay in the node numbered 0. The position of Castle is node n-1. The nodes connected by some roads. Harry need block some roads by magic and he want to minimize the cost. But it’s not enough, Harry want to know how many roads are blocked at least.
The first line is number of test case.
Each test case, the first line contains two integers n, m, which means the number of nodes and edges of the graph. Each node is numbered 0 to n-1.
Following m lines contains information about edges. Each line has four integers u, v, c, d. The first two integers mean two endpoints of the edges. The third one is cost of block the edge. The fourth one means directed (d = 0) or undirected (d = 1).
Technical Specification
1. 2 <= n <= 1000
2. 0 <= m <= 100000
3. 0 <= u, v <= n-1
4. 0 < c <= 1000000
5. 0 <= d <= 1
OutputFor each test case:
Output the case number and the answer of how many roads are blocked at least.
Sample Input
3 4 5 0 1 3 0 0 2 1 0 1 2 1 1 1 3 1 1 2 3 3 1 6 7 0 1 1 0 0 2 1 0 0 3 1 0 1 4 1 0 2 4 1 0 3 5 1 0 4 5 2 0 3 6 0 1 1 0 0 1 2 0 1 1 1 1 1 2 1 0 1 2 1 0 2 1 1 1Sample Output
Case 1: 3 Case 2: 2 Case 3: 2
题意:
给出一个混合图,求要使s不能到达t,至少应该删除哪些边,使得删除的边的边权和最小,而且要求删除的边数达到最小;(有些图是双向的,有些是单向的)。
思路:
我们知道最小割是不唯一的,这里要我们求割边最少的最小割,(第一次接触,未完全理解,先记住方法,然后再议):
首先我们可以求出混合图的最大流,最小割等于最大流, 然后对于割边的处理,可以这样做:
将每条边的边权加1,再跑一次最大流. 这样可以求出割边最小的那个割集,因为割边越多,最小割会越大,假设存在割边 更小的割集,那么就你跑最大流就会跑出更小的流量出来. 最后直接两次最大流的结果相减即为答案;
代码:
#include<map>
#include<vector>
#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#define maxn 1000+100
#define maxm 200000+100
#define PI acos(-1.0)
#define INF 1e9
using namespace std;
typedef long long ll;
struct Edge{
int from,to,cap,flow;
Edge(){}
Edge(int f,int t,int c,int fl):from(f),to(t),cap(c),flow(fl){}
};
struct Dinic{
int n,m,s,t;
vector<Edge>edges;
vector<int>G[maxm];
bool vis[maxm];
int d[maxm];
int cur[maxm];
void init(int mm,int s,int t){
this->s=s;
this->t=t;
for(int i=0;i<=2*mm;i++){
G[i].clear();
}
edges.clear();
}
void AddEdge(int from,int to,int cap){
edges.push_back((Edge){from,to,cap,0});
edges.push_back((Edge){to,from,0,0}) ;
m=edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
bool BFS(){
memset(vis,0,sizeof(vis));
queue<int>Q;
Q.push(s);
d[s] = 0;
vis[s]=1;
while(!Q.empty()){
int x=Q.front();
Q.pop();
for(int i=0;i<(int)G[x].size();i++){
Edge &e=edges[G[x][i]];
if(!vis[e.to]&&e.cap>e.flow){
vis[e.to]=1;
d[e.to]=d[x]+1;
Q.push(e.to);
}
}
}
return vis[t] ;
}
int DFS(int x,int a) {
if(x==t||a==0) return a;
int flow=0,f;
for(int& i=cur[x];i<(int )G[x].size();i++){
Edge& e =edges[G[x][i]];
if(d[x]+1==d[e.to]&&(f=DFS(e.to,min(a,e.cap-e.flow)))>0){
e.flow+=f;
edges[G[x][i]^1].flow-=f;
flow+=f;
a-=f;
if(a==0) break;
}
}
return flow;
}
int Maxflow(){
int flow=0;
while(BFS()){
memset(cur,0,sizeof(cur));
flow+=DFS(s,INF);
}
return flow;
}
}DC;
struct EDGE{
int from,to,cap,d;
EDGE(){}
EDGE(int f,int t,int c,int dd):from(f),to(t),cap(c),d(dd){};
}p[maxm*2];
int main()
{
int T;
int n,m;
scanf("%d",&T);
for(int cas=1;cas<=T;cas++){
scanf("%d%d",&n,&m);
int src=0,dst=n-1;
DC.init(m,src,dst);
int u,v,c,d;
for(int i=0;i<m;i++){
scanf("%d%d%d%d",&u,&v,&c,&d);
DC.AddEdge(u,v,c);
if(d)
DC.AddEdge(v,u,c);
p[i]=EDGE(u,v,c,d);
}
int ans=DC.Maxflow();
DC.init(m,src,dst);
for(int i=0;i<m;i++){
DC.AddEdge(p[i].from,p[i].to,p[i].cap+1);
if(p[i].d)
DC.AddEdge(p[i].to,p[i].from,p[i].cap+1);
}
int flow=DC.Maxflow();
printf("Case %d: %d\n",cas,flow-ans);
}
return 0;
}