对每个时间点添加到终点流为工人数量,从起点对每个工作添加流为所需时间,对工作的时间点添加流为1.
网络流
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define red(i, a, b) for (int i = (a); i >= (b); --i)
#define clr( x , y ) memset(x,y,sizeof(x))
#define sqr(x) ((x) * (x))
#define mp make_pair
#define pb push_back
typedef long long lint;
int t , n , m , sum , s[110] , d[110] , w[110];
bool v[110][510];
int a[750] , vh[750] , h[750];
struct nodd
{
int y , d , n;
} b[300000];
void add(int x,int y,int d)
{
b[++t].y = y; b[t].d = d;
b[t].n = a[x]; a[x] = t;
b[++t].y = x; b[t].d = 0;
b[t].n = a[y]; a[y] = t;
}
int sap(int t,int d)
{
int minh = 650 , max = d , x , y;
if ( t == 610 ) return d;
for ( int p = a[t]; p; p = b[p].n ) if ( b[p].d ) {
y = b[p].y;
if ( h[y] + 1 == h[t] ) {
x = sap( y , max > b[p].d ? b[p].d : max );
b[p].d -= x; b[ p ^ 1 ].d += x;
max -= x;
if ( !max || h[0] > 650 ) return d - max;
}
if ( h[y] < minh ) minh = h[y];
}
if ( max == d ) {
if ( !( -- vh[h[t]] ) ) h[0] = 660;
vh[ h[t] = minh + 1 ] ++;
}
return d - max;
}
void init()
{
scanf("%d%d",&n,&m);
rep(i,1,m) scanf("%d%d%d",&s[i],&w[i],&d[i]);
clr( v , 0 );
clr( a , 0 );
clr( h , 0 );
clr( vh , 0 ); vh[0] = 700;
sum = 0; t = 1;
rep(i,1,m) sum += w[i];
rep(i,1,m)
rep(j,s[i],d[i]-1) add( i + 500 , j , 1 );
rep(i,1,m) add( 0 , i + 500 , w[i] );
rep(i,1,500) add( i , 610 , n );
}
void work()
{
while ( h[0] < 610 ) sum -= sap( 0 , 10000000 );
if ( sum ) {
puts("0");
return;
}
rep(i,1,m)
for ( int p = a[i+500]; p ; p = b[p].n ) if ( !b[p].d ) v[i][b[p].y] = 1;
vector < int > g[110];
rep(i,1,m) {
rep(j,1,500) if ( v[i][j] && !v[i][j-1] ) {
int x = j + 1;
while ( v[i][x] ) x ++;
g[i].pb( j );
g[i].pb( x );
}
}
rep(i,1,m) {
int x = g[i].size();
printf("%d",x/2);
rep(j,0,x-1) printf(" %d",g[i][j]);
puts("");
}
}
int main()
{
int t;
cin >> t;
while ( t -- ) {
init();
work();
}
return 0;
}