Jam's store
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 61 Accepted Submission(s): 18
Problem Description
Jam didn't study well,then he go to repair the computer in a store,there are M staffs and N guests, given each guests have to spend Tij time to repair the computer by the j staffs.
Now ask the total of time the guest at least to wait.
The staff wiil do the next work after he has done the current work
Input
The first line is T(1≤T≤100) means T Case
For each case
The first line is M and N(1≤M,N≤20) means the number of staffs and guests
Now given a Matrix with N∗M each number means the i guests to the j staff time (1≤Tij≤1000)
Output
Output one line about the time at least they need to wait
Sample Input
Sample Output
7
the first guest choose the third staff the second guest choose the second staff the third gurst choose the third staff the total of time is 4+2+1=7
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef int MyType; 4 const MyType INF = 0x7F7F7F7F; 5 const int MAXN = 1000 + 10; 6 const int MAXM = 1000000 + 10; 7 8 struct Edge { int to, next; MyType cap, cost; }; 9 Edge es[MAXM]; 10 int head[MAXN], dis[MAXN], pre[MAXN], que[MAXM], a[MAXN][MAXN]; 11 bool vis[MAXN]; 12 int n, m, cnt, src, des; 13 14 void add( int u, int v, MyType f, MyType c ) { 15 es[cnt].to = v; es[cnt].cap = f; es[cnt].cost = c; 16 es[cnt].next = head[u]; head[u] = cnt++; 17 es[cnt].to = u; es[cnt].cap = 0; es[cnt].cost = -c; 18 es[cnt].next = head[v]; head[v] = cnt++; 19 return ; 20 } 21 22 bool spfa() { 23 int mf, me; 24 memset( vis, false, sizeof( vis ) ); 25 memset( dis, 0x7F, sizeof( dis ) ); 26 memset( pre, -1, sizeof( pre ) ); 27 mf = me = 0; 28 que[me++] = src; dis[src] = 0; vis[src] = true; 29 while( mf != me ) { 30 int u = que[mf++]; vis[u] = false; 31 if( mf >= MAXM ) mf -= MAXM; 32 for( int i = head[u]; ~i; i = es[i].next ) { 33 int v = es[i].to; 34 if( es[i].cap > 0 && dis[v] > dis[u] + es[i].cost ) { 35 dis[v] = dis[u] + es[i].cost; 36 pre[v] = i; 37 if( !vis[v] ) { 38 vis[v] = true; 39 que[me++] = v; 40 if( me >= MAXM ) me -= MAXM; 41 } 42 } 43 } 44 } 45 return dis[des] != INF; 46 } 47 48 MyType cflow() { 49 MyType flow = INF; 50 int u = des; 51 while( ~pre[u] ) { 52 u = pre[u]; 53 flow = min( flow, es[u].cap ); 54 u = es[u ^ 1].to; 55 } 56 u = des; 57 while( ~pre[u] ) { 58 u = pre[u]; 59 es[u].cap -= flow; 60 es[u ^ 1].cap += flow; 61 u = es[u ^ 1].to; 62 } 63 return flow; 64 } 65 66 MyType MCMF() { 67 MyType mincost, maxflow; 68 mincost = maxflow = 0; 69 while( spfa() ) { 70 MyType flow = cflow(); 71 maxflow += flow; 72 mincost += flow * dis[des]; 73 } 74 return mincost; 75 } 76 77 int main() { 78 int t; 79 scanf( "%d", &t ); 80 while( t-- ) { 81 scanf( "%d%d", &m, &n ); 82 memset( head, -1, sizeof( head ) ); cnt = 0; 83 for( int i = 1; i <= n; ++i ) { 84 for( int j = 1; j <= m; ++j ) 85 scanf( "%d", &a[i][j] ); 86 } 87 src = 0; des = 1000; 88 int ncnt = n + 1; 89 for( int i = 1; i <= n; ++i ) add( src, i, 1, 0 ); 90 for( int i = 1; i <= m; ++i ) { 91 for( int j = 1; j <= n; ++j ) { 92 ++ncnt; 93 for( int k = 1; k <= n; ++k ) { 94 add( k, ncnt, 1, a[k][i] * j ); 95 } 96 add( ncnt, des, 1, 0 ); 97 } 98 } 99 printf( "%d ", MCMF() ); 100 } 101 return 0; 102 }