PIGS_POJ1149
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 20253 | Accepted: 9252 |
Description
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 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
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的更多相关文章
随机推荐
- 「每日一题」面试官问你对Promise的理解?可能是需要你能手动实现各个特性
关注「松宝写代码」,精选好文,每日一题 加入我们一起学习,day day up 作者:saucxs | songEagle 来源:原创 一.前言 2020.12.23日刚立的flag,每日一题,题目类 ...
- Sublime Text 2 强大的编辑功能
多行编辑功能:1) 同时编辑多行 (Ctrl+Shift+L (Win) 或 Command+Shift+L (Mac))如要在选中的多行文本的最后面同时添加一个字符"a",先选 ...
- [LeetCode]547. Friend Circles朋友圈数量--不相邻子图问题
/* 思路就是遍历所有人,对于每一个人,寻找他的好友,找到好友后再找这个好友的好友 ,这样深度优先遍历下去,设置一个flag记录是否已经遍历了这个人. 其实dfs真正有用的是flag这个变量,因为如果 ...
- CentOS 8 安装mysql8.0.21
1.使用dnf安装mysql(遇到暂停按y)dnf install @mysql 2.安装完成使用下面命令启动MySQL服务systemctl enable --now mysqld 3.查看服务是否 ...
- python保存二维列表到txt文件,读取txt文件里面的数据转化为二维列表
源码: # 读文件里面的数据转化为二维列表 def Read_list(filename): file1 = open(filename+".txt", "r" ...
- 【STL 源码剖析】浅谈 STL 迭代器与 traits 编程技法
大家好,我是小贺. 点赞再看,养成习惯 文章每周持续更新,可以微信搜索「herongwei」第一时间阅读和催更,本文 GitHub : https://github.com/rongweihe/Mor ...
- java连接mongodb数据库
最近毕设需要用到这个数据库.然而又不会,没办法,只能上网学习学习. 记录一下java连接mongodb数据库的简单方法.这里只是记录一下学习.熟悉一下CURD方法. 但是毕业用到的是SpringBoo ...
- spark集群运行模式
spark的集中运行模式 Local .Standalone.Yarn 关闭防火墙:systemctl stop firewalld.service 重启网络服务:systemctl restart ...
- HotSpot学习(二):虚拟机的启动过程源码解析
1. 前言 上文介绍了HotSpot编译和调试的方法,而这篇文章将迈出正式调试的第一步--调试HotSpot的启动过程. 学习启动过程可以帮助我们了解程序的入口,并对虚拟机的运行有个整体的把握,方便日 ...
- [ABP教程]第四章 集成测试
Web应用程序开发教程 - 第三章: 集成测试 //[doc-params] { "UI": ["MVC","NG"], "DB& ...