• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 18,  comments - 5,  trackbacks - 0
            一、題目描述

            Description

            Mirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlock any pighouse because he doesn't have the keys. Customers come to the farm one after another. Each of them has keys to some pig-houses and wants to buy a certain number of pigs.
            All data concerning customers planning to visit the farm on that particular day are available to Mirko early in the morning so that he can make a sales-plan in order to maximize the number of pigs sold.
            More precisely, the procedure is as following: the customer arives, opens all pig-houses to which he has the key, Mirko sells a certain number of pigs from all the unlocked pig-houses to him, and, if Mirko wants, he can redistribute the remaining pigs across the unlocked pig-houses.
            An unlimited number of pigs can be placed in every pig-house.
            Write a program that will find the maximum number of pigs that he can sell on that day.

            Input

            The first line of input contains two integers M and N, 1 <= M <= 1000, 1 <= N <= 100, number of pighouses and number of customers. Pig houses are numbered from 1 to M and customers are numbered from 1 to N.
            The next line contains M integeres, for each pig-house initial number of pigs. The number of pigs in each pig-house is greater or equal to 0 and less or equal to 1000.
            The next N lines contains records about the customers in the following form ( record about the i-th customer is written in the (i+2)-th line):
            A K1 K2 ... KA B It means that this customer has key to the pig-houses marked with the numbers K1, K2, ..., KA (sorted nondecreasingly ) and that he wants to buy B pigs. Numbers A and B can be equal to 0.

            Output

            The first and only line of the output should contain the number of sold pigs.

            Sample Input

            3 3
            3 1 10
            2 1 2 2
            2 1 3 3
            1 2 6

            Sample Output

            7

            二、分析
                  這道題的見(jiàn)圖有些復(fù)雜,不過(guò)看到豬圈有1000個(gè),而顧客有100個(gè),可以發(fā)現(xiàn)不可能以豬圈為頂點(diǎn)。新設(shè)兩個(gè)頂點(diǎn),一個(gè)s,一個(gè)t,當(dāng)?shù)谝粋€(gè)顧客有個(gè)一個(gè)豬圈的鑰匙時(shí),就有一條從s到該顧客的邊,容量為豬圈中豬的數(shù)量,以后另外有顧客有這個(gè)豬圈的鑰匙是,則有一條從前一個(gè)顧客到這個(gè)顧客的邊,容量為無(wú)窮大,同時(shí)每個(gè)顧客都有到t的邊,容量為顧客的需求量。求最大流,使用Relabel-To-Front算法,具體算法:最大流問(wèn)題
            三、代碼

              1#include<iostream>
              2#include<list>
              3using namespace std;
              4#define MAXM 1001
              5#define MAXN 101
              6int s, t;
              7int n, m;
              8int pig[MAXM], record[MAXM];
              9int c[MAXN+2][MAXN+2];
             10int f[MAXN+2][MAXN+2];
             11int e[MAXN+2];
             12int h[MAXN+2];
             13list<int> l;
             14list<int>::iterator lit;
             15list<int> neb[MAXN+2];
             16list<int>::iterator nit[MAXN+2];
             17void push(int u, int v)
             18{
             19    int d = min(e[u], c[u][v] - f[u][v]);
             20    f[u][v] += d;
             21    f[v][u] = -f[u][v];
             22    e[u] -= d;
             23    e[v] += d;
             24}

             25void relabel(int u)
             26{
             27    int mh = INT_MAX;
             28    for(int i=0; i<n+2; i++)
             29    {
             30        if(c[u][i] > f[u][i])
             31            mh = min(mh, h[i]);
             32    }

             33    h[u] = mh + 1;
             34}

             35void discharge(int u)
             36{
             37    while(e[u] > 0)
             38    {
             39        if(nit[u] == neb[u].end())
             40        {
             41            relabel(u);
             42            nit[u] = neb[u].begin();
             43            continue;
             44        }

             45        int v = *nit[u];
             46        if(c[u][v] > f[u][v] && h[u] == h[v]+1)
             47            push(u, v);
             48        else
             49            nit[u]++;
             50    }

             51}

             52void init_preflow()
             53{
             54    memset(h, 0sizeof(h));
             55    memset(e, 0sizeof(e));
             56    h[s] = n+2;
             57    for(int i=0; i<n+2; i++)
             58    {
             59        if(c[s][i] == 0)
             60            continue;
             61        f[s][i] = c[s][i];
             62        f[i][s] = -f[s][i];
             63        e[i] = c[s][i];
             64        e[s] -= c[s][i];
             65    }

             66}

             67void relabel_to_front()
             68{
             69    init_preflow();
             70    for(int i=1; i<=n; i++)
             71    {
             72        l.push_back(i);
             73        nit[i] = neb[i].begin();
             74    }

             75    lit = l.begin();
             76    while(lit != l.end())
             77    {
             78        int u = *lit;
             79        int old = h[u];
             80        discharge(u);
             81        if(h[u] > old)
             82        {
             83            l.erase(lit);
             84            l.push_front(u);
             85            lit = l.begin();
             86        }

             87        lit++;
             88    }

             89}

             90int main()
             91{
             92    memset(c, 0sizeof(c));
             93    memset(f, 0sizeof(f));
             94    memset(record, -1sizeof(record));
             95    scanf("%d%d"&m, &n);
             96    s = 0; t = n+1;
             97    for(int i=1; i<=m; i++)
             98        scanf("%d"&pig[i]);
             99    for(int i=1; i<=n; i++)
            100    {
            101        int a, k, b;
            102        scanf("%d"&a);
            103        for(int j=0; j<a; j++)
            104        {
            105            scanf("%d"&k);
            106            if(record[k] == -1)
            107            {
            108                if(c[s][i] == 0)
            109                {
            110                    c[s][i] = pig[k];
            111                    neb[s].push_back(i);
            112                    neb[i].push_back(s);
            113                }

            114                else
            115                    c[s][i] += pig[k];
            116                record[k] = i;
            117            }

            118            else
            119            {
            120                c[record[k]][i] = INT_MAX;
            121                neb[record[k]].push_back(i);
            122                neb[i].push_back(record[k]);
            123            }

            124        }

            125        scanf("%d"&b);
            126        c[i][t] = b;
            127        neb[i].push_back(t);
            128        neb[t].push_back(i);
            129    }

            130    relabel_to_front();
            131    printf("%d\n", e[t]);
            132}

             

            posted on 2009-06-26 16:15 Icyflame 閱讀(1329) 評(píng)論(0)  編輯 收藏 引用 所屬分類: 解題報(bào)告
            欧洲性大片xxxxx久久久| 无码任你躁久久久久久| 久久人妻无码中文字幕| 国产精品99久久精品爆乳| 久久无码人妻一区二区三区午夜| 亚洲欧美久久久久9999| 久久久久久久综合综合狠狠| 亚洲狠狠综合久久| 久久精品国产精品亚洲精品 | 欧美久久久久久| 久久影视综合亚洲| 一本一道久久a久久精品综合| 久久成人国产精品一区二区| 久久精品成人一区二区三区| 精品国产婷婷久久久| 精品国产热久久久福利| 久久久久黑人强伦姧人妻| 日韩AV毛片精品久久久| 日日狠狠久久偷偷色综合免费| 青青久久精品国产免费看| 久久久久无码国产精品不卡| 中文字幕无码久久人妻| 国产精品久久久久久久人人看 | 久久精品国产99久久久香蕉| 久久久91人妻无码精品蜜桃HD| 久久亚洲国产成人影院网站| 久久一区二区三区免费| 欧美精品国产综合久久| 久久精品夜夜夜夜夜久久| 久久国产精品一区二区| 国产午夜电影久久| 天堂无码久久综合东京热| 国产成人久久精品一区二区三区 | 亚洲国产成人久久精品99 | 一级做a爰片久久毛片16| 国产日韩久久免费影院| 国产精品久久久久久久人人看| 久久久久女人精品毛片| 国产精品熟女福利久久AV| 久久久国产99久久国产一| 精品久久人妻av中文字幕|