Bnuoj 1268 pigs

Source: Internet
Author: User
Pigstime limit: 1000 msmemory limit: 10000 kbthis problem will be judged on PKU. Original ID: 1149
64-bit integer Io format: % LLD Java class name: main 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. MERs 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 pig into ss 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 operate on that day. inputthe first line of input contains two integers m and n, 1 <= m <= 1000, 1 <= n <= 100, number of pighouses and number of MERs. 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 pig. The number of pig in each pig-house is greater or equal to 0 and less or equal to 1000.
The next n lines contains records about the MERs 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. outputthe first and only line of the output shoshould contain the number of sold pig. sample Input
3 33 1 102 1 2 22 1 3 31 2 6
Sample output
7
Sourcecroatia oi 2002 Final exam-first day problem solving: maximum flow problem. Buy pig. Creating a graph is the key! 0 is the source n + 1 is the sink. Consider the buyer as a point on the stream network. First, select a pigsty to build an edge with the source point. The capacity is the number of Pig heads in the pigsty, the second choice is a pigsty and the first buyer who chooses the pigsty establishes an edge with an infinite capacity. And so on... Finally, the buyer who establishes edge with a pigsty and establishes edge with sink. The number is the buyer's purchase volume! Why do we create a graph like this? The first is the source point! From the source point to the first buyer, we consider not only the purchasing power of the first buyer, but also the purchasing power of other people, so we simply sell the whole pig! It is assumed that the product can be sold out! As for the middle side, we can leave the pig as much as possible to the people behind, but the purchasing power of the last person is limited, so we can only buy so many! So the value of the edge adjacent to the sink is the buyer's purchase volume! Finally, the number of pigs sold is collected. The reason for this is that the first person to open a pigsty won't be shut down, and the pig can be sold to others at will.
 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <vector> 6 #include <climits> 7 #include <algorithm> 8 #include <cmath> 9 #include <queue>10 #define LL long long11 #define INF 0x3f3f3f3f12 using namespace std;13 const int maxn = 1005;14 int n,m,a[maxn],p[maxn];15 int arc[maxn][maxn],pre[maxn],pigs[maxn];16 int bfs(){17     int u,v,f = 0;18     queue<int>q;19     while(true){20         while(!q.empty()) q.pop();21         memset(a,0,sizeof(a));22         memset(p,0,sizeof(p));23         a[0] = INF;24         q.push(0);25         while(!q.empty()){26             u = q.front();27             q.pop();28             for(v = 0; v <= n+1; v++){29                 if(!a[v] && arc[u][v] > 0){30                     a[v] = min(a[u],arc[u][v]);31                     p[v] = u;32                     q.push(v);33                 }34             }35             if(a[n+1]) break;36         }37         if(!a[n+1]) break;38         for(u = n+1; u; u = p[u]){39             arc[p[u]][u] -= a[n+1];40             arc[u][p[u]] += a[n+1];41         }42         f += a[n+1];43     }44     return f;45 }46 int main(){47     int i,j,k,buy,e;48     while(~scanf("%d%d",&m,&n)){49         memset(arc,0,sizeof(arc));50         memset(pre,0,sizeof(pre));51         for(i = 1; i <= m; i++)52             scanf("%d",pigs+i);53         for(i = 1; i <= n; i++){54             scanf("%d",&k);55             while(k--){56                 scanf("%d",&e);57                 if(pre[e]) arc[pre[e]][i] = INF;58                 else arc[pre[e]][i] += pigs[e];59                 pre[e] = i;60             }61             scanf("%d",&buy);62             arc[i][n+1] += buy;63         }64         cout<<bfs()<<endl;65     }66     return 0;67 }
View code

 

Related Keywords:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.