ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1000. A+B Problem

This problem is very hard(easy),I cannot(can) solve it!
Posted by Accepted 29 Sep 2013 21:14
we can use the netflow algorithm(+ operator) to solve this problem.
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAXN 5
using namespace std;
const int INF=1<<30;
int n,m,flow[MAXN][MAXN],pre[MAXN],cur[MAXN],gap[MAXN],d[MAXN],g[MAXN],l[MAXN][MAXN];
int Maxflow_Isap(int s,int t,int n) {
    memset(d,-1,sizeof(d));
    memset(pre,-1,sizeof(pre));
    memset(gap,0,sizeof(gap));
    gap[s]=n;
    int u=pre[s]=s,v,maxflow=0;
    while (d[s]<n) {
      v=n+1;
      for (int i=cur[u];i<=g[u];i++)
        if (flow[u][l[u][i]] && d[u]==d[l[u][i]]+1) {
          v=l[u][i];cur[u]=i;break;
        }
      if (v<=n) {
        pre[v]=u;u=v;
        if (v==t) {
          int dflow=INF;u=s;
          for (int i=v;i!=s;i=pre[i]) dflow=min(dflow,flow[pre[i]][i]);
          maxflow+=dflow;
          for (int i=v;i!=s;i=pre[i]) {
            flow[pre[i]][i]-=dflow;
            flow[i][pre[i]]+=dflow;
          }
        }
      }
      else{
        int mindist=n;
        for (int i=1;i<=g[u];i++)
          if (flow[u][l[u][i]]) mindist=min(mindist,d[l[u][i]]);
        if (!--gap[d[u]]) return maxflow;
        gap[d[u]=mindist+1]++;cur[u]=1;
        u=pre[u];
      }
    }
    return maxflow;
}
void setsize(int x,int y,int c) {
    flow[x][y]=c;
    l[x][++g[x]]=y;
    l[y][++g[y]]=x;
}
int main() {
    scanf("%d%d",&n,&m);
    setsize(1,2,n);
    setsize(2,4,n);
    setsize(1,3,m);
    setsize(3,4,m);
    printf("%d\n",Maxflow_Isap(1,4,4));
    return 0;
}
Re: This problem is very hard(easy),I cannot(can) solve it!
Posted by Dan4Life 1 Jan 2021 04:46
Thank you for this code, I'm curious to know, is there any shorter form of this i can use for a contest?