星际转移问题

S向地球连k的边,每天每个地方由上一天连INF边,每天月亮向T连边
枚举天数获取每天飞船的位置,由上一天的位置向这一天连满载的边
跑到人都送完位置,在合适的时候(玄学)break输出无解

# include <stdio.h>
# include <stdlib.h>
# include <iostream>
# include <string.h>
# include <math.h>
# define ll long long
# define RG register
# define IL inline
# define Mem(a, b) memset(a, b, sizeof(a))
# define Max(a, b) (((a) > (b)) ? (a) : (b))
# define Min(a, b) (((a) < (b)) ? (a) : (b))
using namespace std;

IL int Get(){
    RG char c = '!'; RG int x = 0, z = 1;
    for(; c > '9' || c < '0'; c = getchar()) z = (c == '-') ? -1 : 1;
    for(; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0';
    return x * z;
}

const int INF = 2147483647, MAXN = 50001, MAXM = 2000001;
int r[MAXN], h[MAXN], n, m, ft[MAXN], cnt, ans, map[31][31], Q[MAXM], level[MAXN], cur[MAXN], k;
struct Edge{
    int to, nt, f;
} edge[MAXM];

IL void Add(RG int u, RG int v, RG int f){
    edge[cnt] = (Edge){v, ft[u], f}; ft[u] = cnt++;
    edge[cnt] = (Edge){u, ft[v], 0}; ft[v] = cnt++;
}

IL int Dfs(RG int u, RG int T, RG int maxf){
    if(u == T) return maxf;
    RG int ret = 0;
    for(RG int &e = cur[u]; e != -1; e = edge[e].nt){
        RG int v = edge[e].to;
        if(level[v] == level[u] + 1 && edge[e].f){
            RG int f = Dfs(v, T, Min(maxf - ret, edge[e].f));
            edge[e].f -= f; edge[e ^ 1].f += f;
            ret += f;
            if(ret == maxf) break;
        }
    }
    return ret;
}

IL bool Bfs(RG int S, RG int T){
    Mem(level, 0);
    RG int head = 0, tail = 0;
    Q[0] = S; level[S] = 1;
    while(head <= tail){
        RG int u = Q[head++];
        if(u == T) return 1;
        for(RG int e = ft[u]; e != -1; e = edge[e].nt){
            RG int v = edge[e].to;
            if(!level[v] && edge[e].f){
                level[v] = level[u] + 1;
                Q[++tail] = v;
            }
        }
    }
    return 0;
}

int main(){
    Mem(ft, -1);
    n = Get(); m = Get(); k = Get();
    RG int S = 0, T = 50000;
    for(RG int i = 1; i <= m; i++){
        h[i] = Get(); r[i] = Get();
        for(RG int j = 1; j <= r[i]; j++)
            map[i][j] = Get(), map[i][j] += 2;
    }
    Add(S, 2, k);
    for(RG int i = 1; i <= (k + 2) * (n + 2); i++){
        //枚举天
        RG int t1 = (i - 1) * (n + 2), t2 = i * (n + 2);
        for(RG int j = 1; j <= m; j++){
            RG int u = t1 + map[j][(i - 1) % r[j] + 1], v = t2 + map[j][i % r[j] + 1];
            Add(u, v, h[j]);
            //当前位置 
        }
        for(RG int j = 1; j <= n + 2; j++)
            Add(t1 + j, t2 + j, INF);
        //昨天向今天
        Add(t2 + 1, T, INF);
        //每天月亮
        while(Bfs(S, T)){
            for(RG int j = S; j <= T; j++)
                cur[j] = ft[j];
            ans += Dfs(S, T, INF);
        }
        if(ans >= k) return !printf("%d\n", i);
    }
    printf("0\n");
    return 0;
}

转载于:https://www.cnblogs.com/cjoieryl/p/8206328.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值