zoukankan      html  css  js  c++  java
  • HDU 5619 Jam's store

    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(1T100) means T Case
    For each case
    The first line is M and N(1M,N20) means the number of staffs and guests
    Now given a Matrix with NM each number means the i guests to the j staff time (1Tij1000)

     

    Output

    Output one line about the time at least they need to wait

     

    Sample Input

    1 4 3 4 4 1 5 8 2 5 6 4 5 10 5

     

    Sample Output

    7

    Hint

    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

     
    思路:
    很经典的建图,需要记住。
    此题中,顾客的修理时间会影响到后续顾客的等待时间,而该名顾客何时修理并不能确定,所以枚举该位顾客在所有店员的修理队伍的位置以及对后续顾客的时间影响。
    所以我们如下建图:先建立虚拟起点到每个顾客的边,然后我们将每个店员拆成 N 个点,第 i 个顾客是电源 j 的倒数第 k 个顾客:add( i, j‘, k * c ),其中 j' 是拆点后的点的编号,c 即是顾客在该店员修理花费的时间。最后将所有店员都连接到虚拟汇点即可。
     
    代码如下:
      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 }
    View Code
  • 相关阅读:
    我(webabcd)的文章索引
    学习重构的一些思考
    学习重构的一些思考
    软件编程思想读后感
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
  • 原文地址:https://www.cnblogs.com/Hitman_47/p/5178808.html
Copyright © 2011-2022 走看看