PIGS
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 20253   Accepted: 9252

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 3
3 1 10
2 1 2 2
2 1 3 3
1 2 6

Sample Output

7

DINIC练习,题目的建图值的注意!
  1 #include<cstdio>
2 #include<iostream>
3 #include<cstring>
4 #include<queue>
5 #include<vector>
6
7 using namespace std;
8 const int inf=0x7fffffff;
9 int n,m;
10 int map[110][110];
11 int house[1010];
12 int fir[1010]={0};
13 int lays[110];
14 bool vis[110]={0};
15 bool bfs()
16 {
17 memset(lays,-1,sizeof(lays));
18 queue<int>q;
19 lays[0]=0;
20 q.push(0);
21 while(!q.empty())
22 {
23 int u=q.front();q.pop();
24 for(int i=1;i<=n+1;i++)
25 if(map[u][i]>0&&lays[i]==-1)
26 {
27 lays[i]=lays[u]+1;
28 if(i==n+1)return 1;
29 else q.push(i);
30 }
31 }
32 return 0;
33 }
34 int dinic()
35 {
36 int maxf=0;
37 vector<int>q;
38 while(bfs())
39 {
40 memset(vis,0,sizeof(vis));
41 q.push_back(0);
42 vis[0]=1;
43 while(!q.empty())
44 {
45 int nd=q.back();
46 if(nd==n+1)
47 {
48 int minn,minx=0x7fffffff;
49 for(int i=1;i<q.size();i++)
50 {
51 int u=q[i-1],v=q[i];
52 if(map[u][v]<minx)
53 {
54 minx=map[u][v];
55 minn=u;
56 }
57 }
58 maxf+=minx;
59 for(int i=1;i<q.size();i++)
60 {
61 int u=q[i-1],v=q[i];
62 map[u][v]-=minx;
63 map[v][u]+=minx;
64 }
65 while(!q.empty()&&q.back()!=minn)
66 {
67 vis[q.back()]=0;
68 q.pop_back();
69 }
70 }
71 else
72 {
73 int i;
74 for(i=0;i<=n+1;i++)
75 {
76 if(map[nd][i]>0&&lays[i]==lays[nd]+1&&!vis[i])
77 {
78 q.push_back(i);
79 vis[i]=1;
80 break;
81 }
82 }
83 if(i>n+1)q.pop_back();
84 }
85 }
86 }
87 return maxf;
88 }
89 int main()
90 {
91 cin>>m>>n;
92 for(int i=1;i<=m;i++)
93 scanf("%d",house+i);
94 for(int i=1;i<=n;i++)
95 {
96 int keys;
97 scanf("%d",&keys);
98 for(int j=0;j<keys;j++)
99 {
100 int keyn;
101 scanf("%d",&keyn);
102 if(fir[keyn]==0)map[0][i]+=house[keyn];
103 else map[fir[keyn]][i]=inf;
104 fir[keyn]=i;
105 }
106 int pigs;
107 scanf("%d",&pigs);
108 map[i][n+1]=pigs;
109 }
110 cout<<dinic()<<endl;
111 return 0;
112 }

PIGS_POJ1149的更多相关文章

随机推荐

  1. 新下载的Chrome 不能用,设置搜索引擎,谷歌浏览器不能用,chrome浏览器不能用,google chrome 不能用

    新下载的chrome默认搜索引擎 是google搜索,而google搜索引擎在国内是不能使用的,要设置为 百度或.360.搜狗搜索引擎才能使用. 设置方法如下: 1.打开 Chrome. 2.点击右上 ...

  2. Redis中的常用命令哪些?

    a.hset 存储一个哈希键值对的集合 b.hget获取一个哈希键的值c.hdel 删除一个或多个字段 d.hgetall 获取一个哈希是键值对的集合 e.lpush key value向链表左侧添加 ...

  3. Linux服务器以及系统性能排查常用命令

    一.在Linux系统中排查CPU故障的方法和技巧 1.top命令 Linux内部命令,可以查看实时的CPU的使用情况,也可以查看CPU最近一段时间CPU的使用情况 Linux下常用的性能分析工具,能够 ...

  4. python之 logging 模块(上篇)

    一.日志关概念 日志是一种可以追踪某些软件运行时所发生事件的方法.软件开发人员可以向他们的代码中调用日志记录相关的方法来表明发生了某些事情.一个事件可以用一个可包含可选变量数据的消息来描述.此外,事件 ...

  5. Django中一种常见的setting与账密保存/读取方式

    前言 在查看别人Django代码的时候,发现很多的manager文件都是类似于 #!/usr/bin/env python import os import sys if __name__ == '_ ...

  6. web渗透之常见shell反弹姿势

    常见反弹shell总结: 原文链接请点击:https://ruoli-s.github.io/posts/b956.html 一.bash反弹 通用 ① 在kali机里面开启端口监听: nc -lvv ...

  7. maven依赖与传递性依赖

    目录 依赖范围 传递性依赖 依赖调节 可选依赖 本文主要是针对<maven实战>书中关键知识点的学习记录,未免有纰漏或描述不到之处,建议购买阅读原书 首先贴出一个pom常见的一些元素释义 ...

  8. 简要MR与Spark在Shuffle区别

    一.区别 ①本质上相同,都是把Map端数据分类处理后交由Reduce的过程. ②数据流有所区别,MR按map, spill, merge, shuffle, sort, r educe等各阶段逐一实现 ...

  9. 最新详解android自动化无障碍服务accessibilityservice以及高版本问题_1_如何开启获得无障碍

    前言 无障碍服务accessibilityservice是什么 简单来说 无障碍服务就是一个为残障人士 尤其是视觉障碍人士提供的一个帮助服务.具体就是可以识别控件 文字 可以配合语音助手 操作和 使用 ...

  10. java锁的对象引用

    当访问共享的可变数据时,通常需要同步.一种避免使用同步的方式就是不共享数据. 如果数据仅在单线程内访问,就不需要同步,这种技术称为"线程封闭",它是实现线程安全性最简单方式之一. ...