首页 → 名言 → 美文
poj1149 PIGS -電腦資料
日期:2023-03-09 14:47:10    编辑:网络投稿    来源:互联网
poj1149 PIGS -電腦資料 電腦資料 時間:2019-01-01 我要投稿 【www.unjs.
为你推荐:
  • 女朋友說說
  • poj1149 PIGS -電腦資料 電腦資料 時間:2019-01-01 我要投稿 【www.unjs.com - 電腦資料】

    PIGSTime Limit:1000MSMemory Limit:10000KTotal Submissions:19068Accepted:8697

    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 arrives, 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 33 1 102 1 2 22 1 3 31 2 6

    Sample Output

    7

    Source

    Croatia OI 2002 Final Exam - First day

    最大流,poj1149 PIGS,電腦資料《poj1149 PIGS》(https://www.unjs.com)。

    構圖方式:

    ①把每個顧客看作除源點和匯點以外的節點。

    ②從源點向每個豬圈的第一個顧客連一條邊,容量為該豬圈最初的豬的數量。

    ③每個豬圈的前后兩個顧客之間連一條邊,容量為正無窮。因為可以任意分配每個豬圈中的豬的數量。

    ④從每個顧客向匯點連一條邊,容量為要購買的豬的數量。

    這道題的構圖方法很巧妙。

    #include<iostream>#include<cstdio>#include#include<cmath>#include<cstring>#include<cstdlib>#include<queue>#define F(i,j,n) for(int i=j;i<=n;i++)#define D(i,j,n) for(int i=j;i>=n;i--)#define LL long long#define pa pair<int,int>#define MAXN 105#define MAXM 1005#define INF 1000000000using namespace std;int n,m,k,x,s,t,cnt=1,ans=0;int pre[MAXM],head[MAXN],cur[MAXN],dis[MAXN],c[MAXN],a[MAXM];bool vst[MAXM],f[MAXN];struct edge_type{int next,to,v;}e[10005];inline int read(){int x=0,f=1;char ch=getchar();while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}inline void add_edge(int x,int y,int v){e[++cnt]=(edge_type){head[x],y,v};head[x]=cnt;e[++cnt]=(edge_type){head[y],x,0};head[y]=cnt;}inline bool bfs(){queue<int>q;memset(dis,-1,sizeof(dis));dis[s]=0;q.push(s);while (!q.empty()){int tmp=q.front();q.pop();if (tmp==t) return true;for(int i=head[tmp];i;i=e[i].next) if (e[i].v&&dis[e[i].to]==-1){dis[e[i].to]=dis[tmp]+1;q.push(e[i].to);}}return false;}inline int dfs(int x,int f){int tmp,sum=0;if (x==t) return f;for(int &i=cur[x];i;i=e[i].next){int y=e[i].to;if (e[i].v&&dis[y]==dis[x]+1){tmp=dfs(y,min(f-sum,e[i].v));e[i].v-=tmp;e[i^1].v+=tmp;sum+=tmp;if (sum==f) return sum;}}if (!sum) dis[x]=-1;return sum;}inline void dinic(){while (bfs()){F(i,1,n+2) cur[i]=head[i];ans+=dfs(s,INF);}}int main(){memset(head,0,sizeof(head));memset(c,0,sizeof(c));memset(vst,false,sizeof(vst));m=read();n=read();s=n+1;t=n+2;F(i,1,m) a[i]=read();F(i,1,n){memset(f,false,sizeof(f));k=read();while (k--){x=read();if (!vst[x]) {vst[x]=true;c[i]+=a[x];}if (pre[x]&&!f[pre[x]]) add_edge(pre[x],i,INF),f[pre[x]]=true;pre[x]=i;} x=read();if (x) add_edge(i,t,x);}F(i,1,n) if (c[i]) add_edge(s,i,c[i]);dinic();printf("%d\n",ans);}</int></int,int></queue></cstdlib></cstring></cmath></cstdio></iostream>

    这里有更多你想看的
  • 幾十條超贊的人生感
    • 本类最新
    • 精品图文
    • 时尚
    • 新闻
    • 生活
    • 视觉
    • 微爱
      栏目ID=88的表不存在(操作类型=0)

    头条推荐

    热门推荐

    特别推荐

    返回顶部