[BZOJ4108][Wf2015]Catering (with source and remittance fee flow)

Title:

I'm a hyperlink

Explanation:

There is another condition of this question: every point except point 1 can only pass once
So it's very similar to the last question. The only difficulty of this question is the number of 2-n+1
Since ta has a company, we also need a corresponding point to control the total flow, which just uses 1,n+1+1

Drawing method of original drawing:
Split 2-n+1 points xi,yi
s->1,[0,k],0
1->xi,[0,inf],0
yi->t,[0,inf],0
xi->yi,[1,1],0
For the given cost, if the cost of I - > J is c
yi->xj,[0,inf],c

Then the original drawing is transformed to find the minimum cost and maximum flow

code:

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define INF 1e9
const int N=100000;
int tot=-1,point[N],v[N],remind[N],nxt[N],last[N],c[N],dis[N],mincost,d[N];
bool vis[N];
void addline(int x,int y,int cap,int cc)
{
    ++tot; nxt[tot]=point[x]; point[x]=tot; v[tot]=y; remind[tot]=cap; c[tot]=cc;
    ++tot; nxt[tot]=point[y]; point[y]=tot; v[tot]=x; remind[tot]=0; c[tot]=-cc;
}
int addflow(int s,int t)
{
    int now=t,ans=INF;
    while (now!=s)
    {
        ans=min(ans,remind[last[now]]);
        now=v[last[now]^1];
    }
    now=t;
    while (now!=s)
    {
        remind[last[now]]-=ans;
        remind[last[now]^1]+=ans;
        now=v[last[now]^1];
    }
    return ans;
}
bool spfa(int s,int t)
{
    queue<int>q;
    memset(dis,0x7f,sizeof(dis));
    dis[s]=0;
    memset(vis,0,sizeof(vis));
    q.push(s);
    while (!q.empty())
    {
        int x=q.front(); q.pop(); vis[x]=0;
        for (int i=point[x];i!=-1;i=nxt[i])
          if (dis[v[i]]>dis[x]+c[i] && remind[i])
          {
            dis[v[i]]=dis[x]+c[i];
            last[v[i]]=i;
            if (!vis[v[i]]) vis[v[i]]=1,q.push(v[i]);
          }
    }
    if (dis[t]>INF) return 0;
    int flow=addflow(s,t);
    mincost+=flow*dis[t];
    return 1; 
}
int main()
{
    int n,k,x;
    scanf("%d%d",&n,&k);
    int s=1,t=n+n+3,ss=t+1,tt=ss+1;
    memset(point,-1,sizeof(point));
    addline(s,n+1+1,k,0);
    for (int i=2;i<=n+1;i++) d[i]--,d[i+n+1]++,addline(i+n+1,t,INF,0);
    for (int i=1;i<=n;i++) 
      for (int j=1;j<=n-i+1;j++)
        scanf("%d",&x),addline(i+n+1,i+j,INF,x);
    addline(t,s,INF,0);
    for (int i=1;i<=t;i++)
    {
        if (d[i]>0) addline(ss,i,d[i],0);
        if (d[i]<0) addline(i,tt,-d[i],0);
    }
    while (spfa(ss,tt));
    printf("%d",mincost);
}

Posted on Thu, 30 Apr 2020 23:29:33 -0700 by cobaswosa