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的更多相关文章
随机推荐
- OAuth2.0的四种授权模式(转)
1. OAuth2简易实战(一)-四种模式 1.1. 隐式授权模式(Implicit Grant) 第一步:用户访问页面时,重定向到认证服务器. 第二步:认证服务器给用户一个认证页面,等待用户授权. ...
- [LeetCode]319. Bulb Switcher灯泡开关
智商压制的一道题 这个题有个数学定理: 一般数(非完全平方数)的因子有偶数个 完全平凡数的因子有奇数个 开开关的时候,第i个灯每到它的因子一轮的时候就会拨动一下,也就是每个灯拨动的次数是它的因子数 而 ...
- 使用ThreadLocal
使用ThreadLocal 阅读: 135212 多线程是Java实现多任务的基础,Thread对象代表一个线程,我们可以在代码中调用Thread.currentThread()获取当前线程.例如,打 ...
- 美团在TIDB方面的实践
摘自-https://www.v2ex.com/t/508094 一.背景和现状 在美团,基于 MySQL 构建的传统关系型数据库服务已经难于支撑公司业务的爆发式增长,促使我们去探索更合理的数据存储方 ...
- Lambda 表达式实例
public class Java8Tester {/*** 语法 lambda 表达式的语法格式如下: (parameters) -> expression 或 (parameters) -& ...
- 浅析pagehelper分页原理
原文链接 https://blog.csdn.net/qq_21996541/article/details/79796117 之前项目一直使用的是普元框架,最近公司项目搭建了新框架,主要是由公司的大 ...
- reactor模式:单线程的reactor模式
reactor模式称之为响应器模式,常用于nio的网络通信框架,其服务架构图如下 不同于传统IO的串行调度方式,NIO把整个服务请求分为五个阶段 read:接收到请求,读取数据 decode:解码数据 ...
- css3 知识点积累
-moz- 兼容火狐浏览器-webkit- 兼容chrome 和safari1.角度 transform:rotate(30dge) 水平线与div 第四象限30度 transform: ...
- vue调起微信扫一扫
vue调起微信扫一扫,两个注意的点 1.url必须是不带参的地址栏,如果传了带参数的地址url有可能会出现安卓机能调,苹果机报错或者安卓和苹果都报错 2.this指代问题在vx.ready等等方法里面 ...
- 壁球小游戏详解(附有源码.cpp)
1.在python中安装pygame 2.将下列源码复制过去,运行. #引用 import pygame, sys #初始化 pygame.init() size = width, height = ...