题目链接
很好的一道利用每个时间更新之后再去跑最大流,我们从1时刻开始,每次放入对应时间点可能存在的边,然后最后去跑最大流,直到我们放到月球的人数大于等于我们要驼的K个人即可。
具体思路:我们知道0是起始点,-1是终点,但是由于-1的特殊,我把0看作了N+1,-1看作了N+2来做,那么我们就该去跑时间戳了,每一个时间,飞船就会像公交汽车那样子往后走一格,我们只要让飞船不断的往下走下去即可,但是可能会存在把人放下来让他原地等待下一个别的航班的情况,所以,我们还要对于每个点建立一个原地等待的边,接下去,看到飞船每个时刻都会飞向下一个地点,那么对于该时刻,我们建立这样子的,对于下一个时刻的对应边,容量就是飞船的载客量。
有个细节,就是对于源点和汇点,我们不把它随时间去变化。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 105, maxE = 3e4 + 7;
int N, M, K, mp[maxN][maxN], num[maxN], root[maxN], head[805], cur[805], cnt, deep[805], S, T, sum, up[maxN];
queue<int> Q;
int fid(int x) { return x == root[x] ? x : root[x] = fid(root[x]); }
inline int get_id(int ti, int i) { return (i == S || i == T) ? i : (ti - 1) * (N + 2) + i; }
void mix(int x, int y)
{
int u = fid(x), v = fid(y);
if(u != v) root[u] = v;
}
struct Eddge
{
int nex, to, flow;
Eddge(int a=-1, int b=0, int c=0):nex(a), to(b), flow(c) {}
}edge[maxE];
inline void addEddge(int u, int v, int flow)
{
edge[cnt] = Eddge(head[u], v, flow);
head[u] = cnt++;
}
inline void _add(int u, int v, int flow) { addEddge(u, v, flow); addEddge(v, u, 0); }
bool bfs()
{
memset(deep, 0, sizeof(deep));
deep[S] = 1;
Q.push(S);
while(!Q.empty())
{
int u = Q.front(); Q.pop();
for(int i=head[u], v, flow; ~i; i=edge[i].nex)
{
v = edge[i].to; flow = edge[i].flow;
if(flow && !deep[v])
{
deep[v] = deep[u] + 1;
Q.push(v);
}
}
}
return deep[T];
}
int dfs(int u, int dist)
{
if(u == T) return dist;
for(int &i=cur[u], v, flow, di; ~i; i=edge[i].nex)
{
v = edge[i].to; flow = edge[i].flow;
if(deep[v] == deep[u] + 1 && flow)
{
di = dfs(v, min(flow, dist));
if(di)
{
edge[i].flow -= di;
edge[i^1].flow += di;
return di;
}
}
}
return 0;
}
int Dinic()
{
int ans = 0, tmp;
while(bfs())
{
memcpy(cur, head, sizeof(cur));
while((tmp = dfs(S, INF))) ans += tmp;
}
return ans;
}
inline void init()
{
cnt = sum = 0; T = N + 2; S = N + 1;
for(int i=1; i<=N+2; i++) root[i] = i;
memset(head, -1, sizeof(head));
memset(num, 0, sizeof(num));
}
int main()
{
scanf("%d%d%d", &N, &M, &K);
init();
for(int i=1, nn, las, pos=0; i<=M; i++)
{
scanf("%d%d", &up[i], &nn);
scanf("%d", &las);
if(las == 0) las = S;
else if(las == -1) las = T;
mp[i][num[i]++] = las;
for(int j=2; j<=nn; j++)
{
scanf("%d", &pos);
if(pos == 0) pos = S; //地球
else if(pos == -1) pos = T; //月球
mp[i][num[i]++] = pos;
mix(las, pos);
las = pos;
}
}
if(fid(S) != fid(T)) { printf("0\n"); return 0; }
for(int ans=1; ; ++ans)
{
for(int i=1, now_id, to_id; i<=N; i++)
{
now_id = get_id(ans, i);
to_id = get_id(ans + 1, i);
_add(now_id, to_id, INF);
}
for(int i=1, now_id, to_id; i<=M; i++)
{
now_id = get_id(ans, mp[i][(ans - 1)%num[i]]);
to_id = get_id(ans + 1, mp[i][ans%num[i]]);
_add(now_id, to_id, up[i]);
}
sum += Dinic();
if(sum >= K) { printf("%d\n", ans); return 0; }
}
return 0;
}