上下界网络流专题

  • BZOJ 2324ZJOI2011营救皮卡丘
  • XJTU校赛 贪吃蛇

BZOJ 2324([ZJOI2011]营救皮卡丘)

给定n点m边无向图,用k个人从起点出发,一个人走一条路代价为路的长度Li,你希望按照0,1,2,…,n的顺序依次经过这些点,其中经过的定义是任何1人经过该点,问k个人最小的道路总和。

N ≤ 150, M ≤ 20 000, 1 ≤ K ≤ 10, Li ≤ 10 000

考虑每次只有1个人走1步,已经过t点,
则每次其中一人走向t+1点,距离是所在点到t+1点的最短路(不经过t+1以上的点)

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p]) 
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (300+10)
#define MAXM ((20000)*12+10)
#define eps (1e-3)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
class Cost_Flow  
{  
public:  
    int n,s,t;  
    int q[MAXM];  
    int edge[MAXM],next[MAXM],pre[MAXN],weight[MAXM],size;  
    int cost[MAXM];  
    void addedge(int u,int v,int w,int c)    
    {    
        edge[++size]=v;    
        weight[size]=w;    
        cost[size]=c;    
        next[size]=pre[u];    
        pre[u]=size;    
// cout<<u<<' '<<v<<' '<<w<<endl;
    }    
    void addedge2(int u,int v,int w,int c){addedge(u,v,w,c),addedge(v,u,0,-c);}   
    bool b[MAXN];  
    int d[MAXN];  
    int pr[MAXN],ed[MAXN];  
    bool SPFA(int s,int t)    
    {    
        For(i,n) d[i]=INF,b[i]=0; 
        d[q[1]=s]=0;b[s]=1;    
        int head=1,tail=1;    
        while (head<=tail)    
        {    
            int now=q[head++];    
            Forp(now)    
            {    
                int &v=edge[p];    
                if (weight[p]&&d[now]+cost[p]<d[v])    
                {    
                    d[v]=d[now]+cost[p];    
                    if (!b[v]) b[v]=1,q[++tail]=v;    
                    pr[v]=now,ed[v]=p;    
                }    
            }    
            b[now]=0;    
        }    
        return d[t]!=INF;    
    }   
    int totcost;    

    int CostFlow(int s,int t)    
    {    
        int maxflow=0;
        while (SPFA(s,t))    
        {    
            int flow=INF;    
            for(int x=t;x^s;x=pr[x]) flow=min(flow,weight[ed[x]]); 
            totcost+=flow*d[t]; 
            maxflow+=flow;   
            for(int x=t;x^s;x=pr[x]) weight[ed[x]]-=flow,weight[ed[x]^1]+=flow;         
        }    
// cout<<maxflow<<endl;
        return totcost;    
    }    
    void mem(int n,int t)  
    {  
        (*this).n=n;  
        size=1;  
        totcost=0;  
        MEM(pre) MEM(next)   
    }  
}S1;  
int read()
{
    int x=0,f=1; char ch=getchar();
    while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
    while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
    return x*f;
} 
int n,m,k;
ll f[MAXN][MAXN]={0};
int main()
{
// freopen("bzoj2324.in","r",stdin);
// freopen(".out","w",stdout);
    n=read();m=read();k=read();
    int s=2*(n+1)+1,t=s+1,S=t+1,T=S+1;
    S1.mem(T,T);
    const int inf = INF;

    Rep(i,n+1) Rep(j,n+1) f[i][j]=INF;
    Rep(i,n+1) f[i][i]=0;
    For(i,m) {
        int a=read(),b=read();
        f[a][b]=f[b][a]=min(f[a][b],(ll)read());
    }
    Rep(k,n+1) Rep(i,n+1) Rep(j,n+1) {
        if (k>=max(i,j)) continue;
        f[i][j]=min(f[i][j],f[i][k]+f[k][j]);
    }
// Rep(i,n+1) {
// Rep(j,n+1) cout<<f[i][j]<<' ';
// cout<<endl;
// }
    For(i,n+1) {
        S1.addedge2(S,i+n+1,1,0);
        S1.addedge2(i,T,1,0);
        S1.addedge2(i,i+n+1,inf,0);

    }
    Rep(i,n+1) {
        Fork(j,i+1,n) {
            S1.addedge2(i+1+n+1,j+1,1,f[i][j]);
        }
    } 
    S1.addedge2(s,1,k,0);
    Fork(i,n+2,2*n+2) S1.addedge2(i,t,inf,0);
    S1.addedge2(t,s,k,0);

    cout<<S1.CostFlow(S,T)<<endl; 

    return 0;
}

XJTU校赛 贪吃蛇

将矩阵黑白染色,相邻点连边,由于贪吃蛇长度至少为3,容易用上下界网络流表示环和头尾都在边界的情况

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p]) 
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define pb push_back
#define mp make_pair 
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case %d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
                        For(j,m-1) cout<<a[i][j]<<' ';\
                        cout<<a[i][m]<<endl; \
                        } 
typedef long long ll;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
    int x=0,f=1; char ch=getchar();
    while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
    while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
    return x*f;
} 
class Cost_Flow  
{  
#define MAXN (300+10)
#define MAXM (20000+10)
public:  
    int n,s,t;  
    int q[MAXM];  
    int edge[MAXM],next[MAXM],pre[MAXN],weight[MAXM],size;  
    int cost[MAXM];  
    void addedge(int u,int v,int w,int c)    
    {    
        edge[++size]=v;    
        weight[size]=w;    
        cost[size]=c;    
        next[size]=pre[u];    
        pre[u]=size;    
    }    
    void addedge2(int u,int v,int w,int c){addedge(u,v,w,c),addedge(v,u,0,-c);}   
    bool b[MAXN];  
    int d[MAXN];  
    int pr[MAXN],ed[MAXN];  
    bool SPFA(int s,int t)    
    {    
        For(i,n) d[i]=INF,b[i]=0;
        d[q[1]=s]=0;b[s]=1;    
        int head=1,tail=1;    
        while (head<=tail)    
        {    
            int now=q[head++];    
            Forp(now)    
            {    
                int &v=edge[p];    
                if (weight[p]&&d[now]+cost[p]<d[v])    
                {    
                    d[v]=d[now]+cost[p];    
                    if (!b[v]) b[v]=1,q[++tail]=v;    
                    pr[v]=now,ed[v]=p;    
                }    
            }    
            b[now]=0;    
        }    
        return d[t]!=INF;    
    }   
    int totcost,maxflow;    
    int CostFlow(int s,int t)    
    {    

        while (SPFA(s,t))    
        {    
            int flow=INF;    
            for(int x=t;x^s;x=pr[x]) flow=min(flow,weight[ed[x]]);      
            totcost+=flow*d[t];
            maxflow+=flow;    
            for(int x=t;x^s;x=pr[x]) weight[ed[x]]-=flow,weight[ed[x]^1]+=flow;         
        }    
        return totcost;    
    }    
    void mem(int n,int t)  
    {  
        (*this).n=n;  
        size=1;  maxflow=0;
        totcost=0;  
        MEM(pre) MEM(next)   
    }  
}S1; 
int n,m;
char s[20][20];
int main()
{
//  freopen("bzoj4213.in","r",stdin);
//  freopen(".out","w",stdout);

    n=0;
    while(scanf("%s",s[n+1]+1)!=EOF) s[++n][0]=0;
    m=strlen(s[1]+1);

    int S=n*m+1,T=S+1;
    int SS=T+1,TT=SS+1;
    S1.mem(TT,TT);
    int p=0;
    For(i,n) For(j,m) {
        if (s[i][j]=='#') continue; 
        ++p;
        int now = (i-1)*m+j;
        if ((i+j)&1) {
            S1.addedge2(SS,now,2,0);
            S1.addedge2(S,TT,2,0);

            if (i<n) S1.addedge2(now,now+m,1,0);
            if (i>1) S1.addedge2(now,now-m,1,0);
            if (j<m) S1.addedge2(now,now+1,1,0);
            if (j>1) S1.addedge2(now,now-1,1,0);

            if (i==1||i==n||j==1||j==m) S1.addedge2(now,T,1,1);                     

        } else {
            S1.addedge2(SS,T,2,0);
            S1.addedge2(now,TT,2,0);
            if (i==1||i==n||j==1||j==m) S1.addedge2(S,now,1,1);                     
        } 


    }
    S1.CostFlow(SS,TT);
    if (S1.maxflow<p*2) {
        S1.addedge2(T,S,INF,0);
        S1.CostFlow(SS,TT);
    } 
    if (S1.maxflow<p*2) puts("-1");
    else cout<<S1.totcost/2<<endl;


    return 0;
}

你可能感兴趣的:(上下界网络流专题)