这题明显要根据时间进行拆点,问题是不知道要拆多少个。最开始我想用二分,但最坏情况下跑网络流会超时,所以无法判断上界。尔对于这种情况,我们可以动态加点,然后在上一次的残余网络上继续跑最大流。这样的好处就是不用判断上届而且每次跑网络流的时候增广的比较少,时间复杂度得到保证。(猜想时间复杂度应该就和直接在最终的图上跑网络流的复杂度差不多)。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef int lint;
const lint maxn = 10000 + 5;
const lint maxm = 20000+ 5;
const lint inf = 0x3f3f3f3f;
struct dinic{
lint he[maxn],ne[maxm],ver[maxm];
LL edge[maxm],d[maxn];
lint s,t,tot;
queue<lint> que;
void init( lint n ){
for( lint i = 0;i <= n;i++ ){
he[i] = 0;
}
tot = 1;
}
void add( lint x,lint y,LL z ){
ver[++tot] = y; ne[ tot ] = he[x]; he[x] = tot; edge[ tot ] = z;
ver[++tot] = x; ne[tot] = he[y]; he[y] = tot;edge[tot] = 0;
}
bool bfs(){
memset( d,0,sizeof( d ) );
while( que.size() ) que.pop();
que.push( s );
d[s] = 1;
while( que.size() ){
lint x = que.front();
que.pop();
for( lint cure = he[x];cure;cure = ne[cure] ){
lint y = ver[cure];
if( edge[cure] && !d[y] ){
que.push( y );
d[ y ] = d[x] + 1;
if( y == t ) return 1;
}
}
}
return 0;
}
LL dfs( lint x,LL flow ){
if( x== t ) return flow;
LL rest = flow,k;
for( lint cure = he[x];cure && rest;cure = ne[cure] ){
lint y = ver[cure];
if( edge[cure] && d[ y ] == d[x] + 1 ){
k = dfs( y,min( rest,edge[cure] ) );
if( !k ) d[ y ] = 0;
edge[cure] -= k;
edge[ cure^(LL)1 ] += k;
rest -= k;
}
}
return flow - rest;
}
LL max_flow( lint x,lint y ){
s = x; t = y;
LL maxflow = 0;
LL flow = 0;
while( bfs() )
while( flow = dfs( s,inf ) ) maxflow += flow;
return maxflow;
}
} g;
lint num[30];
lint ver[maxm],cost[maxm],he[maxn],ne[maxm],tot,nn[maxm],cnt[maxm];
void add( lint x,lint y,lint c,lint m,lint r ){
ver[++tot] = y;
ne[tot] = he[x];
he[x] = tot;
cost[tot] = c;
nn[tot] = m;
cnt[tot] = r;
}
void init(){
tot = 1;
g.tot = 1;
}
lint r[maxn];
int main(){
init();
lint n,m,k,sum = 0;
scanf("%d%d%d",&n,&m,&k);
lint ss = 9999,sss = 9998,tt = 10000,ttt = 10001, tim = 0;
g.add( sss,ss,k );
g.add( tt,ttt,k );
for( lint x,i = 1;i <= m;i++ ){
scanf("%d%d",&x,&r[i]);
num[i] = x;
lint pre,fir;
sum += r[i];
for( lint j = 0;j < r[i];j++ ){
scanf("%d",&x);
if( x == -1 ){
x = n+1;
}
if( j == 0 ) fir = x;
if( j != 0 ){
add( pre,x,num[i],j-1,r[i] );
}
if( j == r[i]-1 ){
add( x,fir,num[i],j,r[i] );
}
pre = x;
}
}
g.add( ss,0,inf );g.add( n+1,tt,inf );
LL res = 0;
lint pp = 0;
while(1){
for( lint i = 0;i <= n+1;i++ ){
if( i == 0 ){
g.add( ss,i+(tim+1)*(n+2),inf );
}
g.add( i+tim*(n+2),i+(tim+1)*(n+2),inf );
for( lint cure = he[i];cure;cure = ne[cure] ){
lint y = ver[cure];
lint p = nn[cure];
if( tim%(cnt[cure]) != p ) continue;
g.add( i+tim*(n+2),y+(tim+1)*(n+2),cost[cure] );
}
if( i == n+1 ){
g.add( i+(tim+1)*(n+2),tt,inf );
}
}
tim++;
LL dt = g.max_flow(sss,ttt);
res += dt;
if( dt ){
pp = tim;
}
if( tim - pp > k*sum ){
cout << 0;
return 0;
}
if( res == k ){
break;
}
}
cout << tim << endl;
return 0;
}