uvalive5843(最大流)
生活随笔
收集整理的這篇文章主要介紹了
uvalive5843(最大流)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
轉(zhuǎn)載自:http://vawait.com/uvalive-5843/
題意:
給出n個工人,給出m個命令,每條命令給出這條命令的最早開始時間、最晚結(jié)束時間和完成這條命令所需要的時間,求出一種方案使得完成時間最早。
思路:
源點到每個工作添加流為工作時間,從工作到每個時間點添加流為1,從每個時間點到匯點添加流為工人數(shù)目。
代碼:
#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; }總結(jié)
以上是生活随笔為你收集整理的uvalive5843(最大流)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: uva1515(模拟)
- 下一篇: uvalive5790(DP)