Codeforces Round # 304 (Div. 2) (CF546E) Soldier and Traveling (maximum flow)

The meaning of problems

Given n cities, m edges. People can only go from the adjacent edges are connected (only go once) city.
You are given a number of initial every city, give each city a number of groups. It asked whether the number can be changed from the current quorum to give. If so, enter "YES" and output scheme, not the output "NO".

http://codeforces.com/contest/546/problem/E

Thinking

When Σa! = Σb time, certainly not.

Build a super source point s and super sink t, s to (1 ~ n) is connected a capacity of a [i] sides, (n + 1 ~ 2 * n) to t even a capacity of b [i] of side, while even then the capacity figures given as an edge coupled inf, such as u and v are connected, then u to v + n and v to u + n have a capacity of even the edge inf. Also his side even with their own, namely i to i + n capacity of even a side inf, because people do not take their own points equivalent to its own point up to his point. Finally, on the reverse side are connected with a running maximum flow Dinic, if the maximum flow == Σa, it can, according to the program flow i i + n reverse side to be solved.

The sample was built following diagram like this (random draw):

Code

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int inf=1e9;
const int N=505;
int n,m,x,y,z,maxflow,deep[N];//deep深度
struct Edge
{
    int next,to,dis;
} edge[N*10];
int num_edge=-1,head[N],cur[N];//cur用于复制head
queue <int> q;

void add_edge(int from,int to,int dis,bool flag)
{
    edge[++num_edge].next=head[from];
    edge[num_edge].to=to;
    if (flag) edge[num_edge].dis=dis;//反图的边权为 0
    head[from]=num_edge;
}

//bfs用来分层
bool bfs(int s,int t)
{
    memset(deep,0x7f,sizeof(deep));
    while (!q.empty()) q.pop();
    for (int i=0; i<=2*n+1; i++) cur[i]=head[i];
    deep[s]=0;
    q.push(s);

    while (!q.empty())
    {
        int now=q.front();
        q.pop();
        for (int i=head[now]; i!=-1; i=edge[i].next)
        {
            if (deep[edge[i].to]>inf && edge[i].dis)//dis在此处用来做标记 是正图还是返图
            {
                deep[edge[i].to]=deep[now]+1;
                q.push(edge[i].to);
            }
        }
    }
    if (deep[t]<inf) return true;
    else return false;
}

//dfs找增加的流的量
int dfs(int now,int t,int limit)//limit为源点到这个点的路径上的最小边权
{
    if (!limit || now==t) return limit;

    int flow=0,f;
    for (int i=cur[now]; i!=-1; i=edge[i].next)
    {
        cur[now]=i;
        if (deep[edge[i].to]==deep[now]+1 && (f=dfs(edge[i].to,t,min(limit,edge[i].dis))))
        {
            flow+=f;
            limit-=f;
            edge[i].dis-=f;
            edge[i^1].dis+=f;
            if (!limit) break;
        }
    }
    return flow;
}

void Dinic(int s,int t)
{
    while (bfs(s,t))
        maxflow+=dfs(s,t,inf);
}
int a[N],b[N];
int main()
{
    memset(head,-1,sizeof(head));
    scanf("%d%d",&n,&m);
    int s1=0,s2=0,s=0,t=2*n+1;
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&a[i]);
        add_edge(s,i,a[i],1);
        add_edge(i,s,a[i],0);
        s1+=a[i];
    }
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&b[i]);
        add_edge(i+n,t,b[i],1);
        add_edge(t,i+n,b[i],0);
        s2+=b[i];
    }
    for (int i=1; i<=m; i++)
    {
        scanf("%d%d",&x,&y);
        add_edge(x,y+n,inf,1);
        add_edge(y+n,x,inf,0);
        add_edge(y,x+n,inf,1);
        add_edge(x+n,y,inf,0);
    }
    if(s1!=s2)
    {
        puts("NO");
        return 0;
    }
    for(int i=1;i<=n;i++)
    {
        add_edge(i,i+n,inf,1);add_edge(i+n,i,inf,0);
    }
    Dinic(s,t);
   // cout<<maxflow<<endl;
    if(maxflow==s1)
    {
        puts("YES");
        int g[N][N];
        for(int i=1;i<=n;i++)
        {
            for(int j=head[i];~j;j=edge[j].next)
            {
                int v=edge[j].to;
                if(v>n)
                {
                    g[i][v-n]=edge[j^1].dis;
                }
            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                printf("%d ",g[i][j]);
            }
            puts("");
        }
    }
    else
    {
        puts("NO");
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/mcq1999/p/TuLun_3.html