• 「网络流 24 题」星际转移


    大意: $n$个空间站, 标号$1,...,n$, 地球在$0$, 月球在$-1$, $m$个飞船, 第$i$艘船每次只能载$h_i$人, 周期性停靠在集合$S_i$, 船每移动一次耗时为$1$. 初始$k$个人在地球, 求如何坐船能使$k$个人全到月球的时间最短.

    记第$i$个空间站$t$时刻的状态为$(i,t)$.

    $S$向$(0,0)$连边$k$, $(-1,t)$向$T$连边$INF$, $(i,t)$向$(i,t+1)$连边$INF$.

    枚举时间, 每次将飞船的转移添进网络内, 若当前最大流为$k$, 则可以到达.

    #include <iostream>
    #include <sstream>
    #include <algorithm>
    #include <cstdio>
    #include <math.h>
    #include <set>
    #include <map>
    #include <queue>
    #include <string>
    #include <string.h>
    #include <bitset>
    #include <unordered_map>
    #define REP(i,a,n) for(int i=a;i<=n;++i)
    #define PER(i,a,n) for(int i=n;i>=a;--i)
    #define hr putchar(10)
    #define pb push_back
    #define lc (o<<1)
    #define rc (lc|1)
    #define mid ((l+r)>>1)
    #define ls lc,l,mid
    #define rs rc,mid+1,r
    #define x first
    #define y second
    #define io std::ios::sync_with_stdio(false)
    #define endl '
    '
    #define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
    using namespace std;
    typedef long long ll;
    typedef pair<int,int> pii;
    const int P = 1e9+7, P2 = 998244353, INF = 0x3f3f3f3f;
    ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
    ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
    ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
    inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
    //head
    
    
    
    const int N = 1e6+10;
    int n, m, k, S, T, tot;
    int h[N], r[N], fa[N];
    vector<int> g[N];
    struct edge {
        int v,w,next;
    } e[N];
    int head[N], dep[N], vis[N], cur[N], cnt=1;
    queue<int> Q;
    void add(int u, int v, int w) {
        e[++cnt] = {v,w,head[u]};
        head[u] = cnt;
        e[++cnt] = {u,0,head[v]};
        head[v] = cnt;
    }
    int bfs() {
        REP(i,1,tot) dep[i]=INF,vis[i]=0,cur[i]=head[i];
        dep[S]=0,Q.push(S);
        while (Q.size()) {
            int u = Q.front(); Q.pop();
            for (int i=head[u]; i; i=e[i].next) {
                if (dep[e[i].v]>dep[u]+1&&e[i].w) {
                    dep[e[i].v]=dep[u]+1;
                    Q.push(e[i].v);
                }
            }
        }
        return dep[T]!=INF;
    }
    int dfs(int x, int w) {
        if (x==T) return w;
        int used = 0;
        for (int i=cur[x]; i; i=e[i].next) {
            cur[x] = i;
            if (dep[e[i].v]==dep[x]+1&&e[i].w) {
                int flow = dfs(e[i].v,min(w-used,e[i].w));
                if (flow) {
                    used += flow;
                    e[i].w -= flow;
                    e[i^1].w += flow;
                    if (used==w) break;
                }
            }
        }
        return used;
    }
    int dinic() {
        int ans = 0;
        while (bfs()) ans+=dfs(S,INF);
        return ans;
    }
    
    int Find(int x) {return fa[x]?fa[x]=Find(fa[x]):x;}
    void add(int x, int y) {
    	x = Find(x), y = Find(y);
    	if (x!=y) fa[x] = y;
    }
    map<pii,int> s;
    int id(int x, int y) {
    	if (s.count(pii(x,y))) return s[pii(x,y)];
    	return s[pii(x,y)]=++tot;
    }
    
    int main() {
    	scanf("%d%d%d", &n, &m, &k);
    	REP(i,1,m) {
    		scanf("%d%d", h+i, r+i);
    		REP(j,1,r[i]) {
    			int x;
    			scanf("%d", &x);
    			g[i].pb(x);
    			add(g[i].front()+2,g[i].back()+2);
    		}
    		if (r[i]==1) g[i].clear(),--i,--m;
    	}
    	if (Find(1)!=Find(2)) return puts("0"),0;
    	int mf = 0;
    	S = ++tot, T = ++tot;
    	add(S,id(0,0),k);
    	for (int t=1; ; ++t) {
    		add(id(0,t-1),id(0,t),INF);
    		REP(i,1,n) { 
    			add(id(i,t-1),id(i,t),INF);
    		}
    		REP(i,1,m) {
    			int L = id(g[i][(t%r[i]-1+r[i])%r[i]],t-1), R = id(g[i][t%r[i]],t);
    			add(L,R,h[i]);
    		}
    		add(id(-1,t),T,INF);
    		if ((mf+=dinic())==k) return printf("%d
    ",t),0;
    	}
    }
    
  • 相关阅读:
    windows下重新安装pip
    让 Windows 中的 Vim 支持 Python
    【anaconda】处理 WARNING: Ignoring invalid distribution ip
    vim界面语言改为英语
    在windows下vim安装pythonmod插件
    Mac启动,停止重启Mysql命令
    树的问题(二)
    显示数字二进制数
    vector<vector<int>>初始化
    ios作品-东风标致408
  • 原文地址:https://www.cnblogs.com/uid001/p/10991378.html
Copyright © 2020-2023  润新知