• <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 - 7,comments - 3,trackbacks - 0
            PIGS
            Time Limit: 1000MSMemory Limit: 10000K
            Total Submissions: 10566Accepted: 4622

            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


            最大流,構圖思想:用人當點,因為每個人是按順序的,所以第i個人第一次打開第k個豬圈,第j個人第二次打開第k個豬圈時,相當于第i個人向第j個人增流。
            所以,如果第i個人持有第k個豬圈的鑰匙,如果是第一次打開,就從源點引邊向該點,邊權累加豬圈的值(一個人可能持有多個鑰匙,所以要累加),如果不是第一次,那么從上一次那個人引出一條無限大的邊到i點作為增流。每個點到匯點的權值是該人需要的個數,最大流一次就行了。
            SAP,時間很好看。
            代碼:
            #include<stdio.h>
            #include<string.h>
            #include
            <iostream>
            #include
            <queue>
            #define N 1010
            using namespace std;
            const int maxnode=60000;
            const int maxedge=320000;
            const int  inf = 1 << 30;
            int S,T,cnt,n,m;
            int head[maxnode],gap[maxnode],pre[maxnode],cur[maxnode],dis[maxnode];
            struct Edge
            {
                
            int S,t;
                
            int next;
                
            int w;
            }st[maxedge];
            void init()
            {
                memset(head,
            -1,sizeof(head));
                cnt
            =0;
            }
            void AddEdge(int S,int t,int w)
            {
                st[cnt].S
            =S;
                st[cnt].t
            =t;
                st[cnt].w
            =w;
                st[cnt].next
            =head[S];
                head[S]
            =cnt;
                cnt
            ++;
                st[cnt].S
            =t;
                st[cnt].t
            =S;
                st[cnt].w
            =0;
                st[cnt].next
            =head[t];
                head[t]
            =cnt;
                cnt
            ++;
            }
            void bfs()
            {
                memset(gap,
            0,sizeof(gap));
                memset(dis,
            -1,sizeof(dis));
                queue
            <int >Q;
                Q.push(T);
                dis[T]
            =0;
                gap[
            0]=1;
                
            int k,t;
                
            while(!Q.empty())
                {
                    k
            =Q.front();
                    Q.pop();
                    
            for(int i=head[k];i!=-1;i=st[i].next)
                    {
                        t
            =st[i].t;
                        
            if(dis[t]==-1&&st[i^1].w>0)
                        {
                            dis[t]
            =dis[k]+1;
                            gap[dis[t]]
            ++;
                            Q.push(t);
                        }
                    }
                }
            }
            int sap()
            {
                
            int i;
                
            for( i=S;i<=T;i++)cur[i]=head[i];
                pre[S]
            =S;
                
            int u=S,v;
                
            int  flow=0;
                
            int aug=inf;
                
            bool flag;
                
            while(dis[S]<=T)
                {
                    flag
            =false;
                    
            for( i=cur[u];i!=-1;i=st[i].next)
                    {
                        v
            =st[i].t;
                        
            if(st[i].w>0&&dis[u]==dis[v]+1)
                        {
                            cur[u]
            =i;
                            flag
            =true;
                            pre[v]
            =u;
                            aug
            =(aug>st[i].w)?st[i].w:aug;
                            u
            =v;
                            
            if(v==T)
                            {
                                flow
            +=aug;
                                
            for(u=pre[u];v!=S;u=pre[u])
                                {
                                    v
            =u;
                                    st[cur[u]].w
            -=aug;
                                    st[cur[u]
            ^1].w+=aug;
                                }
                                aug
            =inf;
                            }
                            
            break;
                        }
                    }
                    
            if(flag==true)continue;
                    
            int mint=T;
                    
            for( i=head[u];i!=-1;i=st[i].next)
                    {
                        v
            =st[i].t;
                        
            if(st[i].w>0&&mint>dis[v])
                        {
                            cur[u]
            =i;
                            mint
            =dis[v];
                        }
                    }
                    gap[dis[u]]
            --;
                    
            if(gap[dis[u]]==0)break;
                    gap[dis[u]
            =mint+1]++;
                    u
            =pre[u];
                    
            if(u==S)aug=inf;
                }
                
            return flow;
            }

            int main()
            {
                
            int map[N][N];
                
            int num[N];
                
            int f[N];
                
            while (scanf("%d%d"&m, &n) != EOF)
                {
                    memset(map, 
            0sizeof(map));
                    memset(f, 
            0sizeof(f));
                    init();
                    S 
            = 0;
                    T 
            = n + 1;
                    
            for (int i = 1; i <= m; ++i)
                        scanf(
            "%d"&num[i]);
                    
            for (int i = 1; i <= n; ++i)
                    {
                        
            int a, b;
                        scanf(
            "%d"&a);
                        
            for (int j = 1; j <= a; ++j)
                        {
                            
            int x;
                            scanf(
            "%d"&x);
                            
            if (f[x] == 0)
                            {
                                map[S][i] 
            += num[x];
                                f[x] 
            = i;
                            }
                            
            else
                            {
                                map[f[x]][i] 
            = inf;
                                f[x] 
            = i;
                            }
                        }
                        scanf(
            "%d"&b);
                        map[i][T] 
            += b;
                    }
                    
            for (int i = S; i <= T; ++i)
                        
            for (int j = S; j <= T; ++j)
                        
            if (map[i][j])
                        {
                            
            //printf("%d %d %d\n", i, j, map[i][j]);
                            AddEdge(i, j, map[i][j]);
                        }
                    bfs();
                    printf(
            "%d\n", sap());
                }
            }

            /*
            4 2
            1 2 3 5
            3 1 2 3 10
            1 4 5

            */
            posted on 2011-10-15 22:12 LLawliet 閱讀(162) 評論(0)  編輯 收藏 引用 所屬分類: 網絡流
            亚洲欧美国产精品专区久久| 7777精品久久久大香线蕉| 久久天天躁狠狠躁夜夜不卡| 久久精品国产亚洲AV蜜臀色欲| 狠狠色婷婷久久综合频道日韩| 人妻精品久久久久中文字幕69 | 国内精品综合久久久40p| 久久精品国产精品亚洲精品| 精品无码久久久久久尤物| 久久五月精品中文字幕| 久久精品中文无码资源站| 久久天天躁狠狠躁夜夜躁2O2O| 久久精品国产国产精品四凭| 国产精品久久久久久搜索| 亚洲国产婷婷香蕉久久久久久| 久久久精品国产sm调教网站 | 性欧美大战久久久久久久久| 欧美与黑人午夜性猛交久久久 | 97超级碰碰碰碰久久久久| 色婷婷综合久久久久中文 | 免费一级欧美大片久久网| 久久99精品国产麻豆| 伊人久久大香线蕉精品不卡| 久久久国产精品福利免费| 久久综合狠狠色综合伊人| 久久精品国产亚洲av麻豆图片| AA级片免费看视频久久| 国产成人精品久久亚洲| 91久久精品91久久性色| 亚洲国产一成人久久精品| 久久久久久国产a免费观看黄色大片| 久久97久久97精品免视看| 久久久免费观成人影院| AAA级久久久精品无码区| 天天久久狠狠色综合| 久久精品www| 中文字幕人妻色偷偷久久| 久久99精品久久久大学生| 久久久久久国产精品无码下载| 久久久国产亚洲精品| 一本一本久久a久久综合精品蜜桃|