[ZJOI2009] story of the wolf and the sheep - minimum cut

Given a \ (N \ times M \) cells of the matrix, each grid may be \ (0,1,2 \) values are. It requires square divided edge, while such does not comprise any inner link block \ (1 \) and \ (2 \) lattice.
_________________________
If the other party cells in the form of a matrix of confused, we can consider the form on the map.
Suppose there is a node in FIG \ (W_1 \) , \ (Q_1 \) , \ (S_1 \) , \ (S_2 \) , and \ (W_1 \) with each other among the three side, in addition \ (Q_1 \) and \ (S_1 \) between the side as well. \ (W_1 \) is the value of the node 1, \ (S_1, S_2 \) is the value of the node 2. Now consider to be divided.
We found that the original problem, \ (0 \) value point whether and at what point in a communication block plans are legitimate. In other words, \ (0 \) value of the node here works only for conducting communication. I.e., you can have any number between any two communicating nodes \ (0 \) value of the node.
According to this nature, we stratified on the map. Networking, the \ (W_i \) as a first layer (near the source), \ (S_i \) as the last layer (close to the sink), \ (Q_i \) in the middle. The original one-way connection relationship corresponds to the network:

  • By the \ (W_i \) is connected to an arbitrary point are converted to a capacity \ (1 \) arc
  • By the (S_i \) \ connect to any point are ignored
  • By the \ (Q_i \) to non \ (W_i \) connection points are converted to a capacity \ (1 \) arc

Easy to find, since the picture is undirected graph, we established its oriented in the flow network, so as to avoid double counting of a dividing edge. And according to the orientation is based on \ (W_i> Q_i> S_i \ ) be the priority.
After this map building, the largest network of stream flow is obtained minimal cut, that is the answer.
_________________________
after a complete analysis of the situation on the chart, where the situation becomes simple.
We consider an arbitrary lattice \ ((I, J) \) , around which the node that is above a grid is directly connected. We will diagram of the processing matrix into the box on it.
_______________________

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<queue>
#define G g
using namespace std;
struct Item{int p,v,c;};
Item item(int _p,int _v,int _c){Item it;it.p=_p;it.v=_v;it.c=_c;return it;}
vector <Item> g[100005];
int n,m,t1,t2,t3,dis[100005],d[100005],vis[100005],s,t,t4,costs=0,tans,ans,inc[100005],cnt=0,k,inp[100005][4],edg[100005];
vector <int> oppo[100005];
int dinic_spfa(){
    memset(dis,0xff,sizeof dis);
    memset(d,0x3f,sizeof d);
    memset(vis,0x00,sizeof vis);
    memset(inc,0x00,sizeof inc);
    queue <int> q;
    q.push(s); dis[s]=0; d[s]=0;
    while(!q.empty()){ 
        int p=q.front(); q.pop(); vis[p]=0; inc[p]++;
        for(int i=0;i<g[p].size();i++){
            if(d[g[p][i].p]>d[p]+g[p][i].v&&g[p][i].c>0){
                dis[g[p][i].p]=p; edg[g[p][i].p]=i;
                d[g[p][i].p]=d[p]+g[p][i].v;
                if(vis[g[p][i].p]==0) vis[g[p][i].p]=1, q.push(g[p][i].p);}}}
    return dis[t]>0;}
int dinic_dfs(){
    int p=t,a=0x7fffffff;
    while(p-s) a=min(a,g[dis[p]][edg[p]].c),p=dis[p];
    int lc=costs;
    p=t;
    while(p-s){
        int tc,i=edg[p];
        G[dis[p]][i].c-=a,
        G[p][oppo[dis[p]][i]].c+=a,
        costs+=a*G[dis[p]][i].v;
        p=dis[p];}
    return a;}
int dinic_main(int src,int dest){
    s=src; t=dest; 
    while(dinic_spfa()) ans+=dinic_dfs();
    return ans;}
void build(int w,int x,int y,int z){
    oppo[x].push_back(g[w].size());
    g[w].push_back(item(x,z,y));
    oppo[w].push_back(g[x].size());
    g[x].push_back(item(w,-z,0));} 
int main(){
    scanf("%d%d%d",&n,&m,&k);
    for(int i=1;i<=m;i++) scanf("%d%d%d%d",&inp[i][0],&inp[i][1],&inp[i][2],&inp[i][3]),
        build(inp[i][0],inp[i][1],inp[i][2],0);
    dinic_main(1,n);
    printf("%d ",ans);
    for(int i=1;i<=m;i++)
        build(inp[i][0],inp[i][1],0x7fffffff,inp[i][3]);
    build(0,1,k,0);
    dinic_main(0,n);
    printf("%d\n",costs);
}

Guess you like

Origin www.cnblogs.com/mollnn/p/12324402.html