HDU 6126.Give out candies 最小割
Give out candies
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 218 Accepted Submission(s): 66
For each test case:
The first line contain three positive integers n,m,k(1≤n,m≤50,1≤k≤150).
The next n lines, the ith line contains m positive integers, the jth integer denotes wi,j.
The next k lines, each line contains three integers x,y,z(1≤x,y≤n,∣z∣<233), denoting a piece of requirement.
A single line contains a nonnegative integer, denoting the answer. Particularly, if there is no way to give out candies, print -1.
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6126
题意:有n个人,每个人i获得j个糖果会有一个满意度wij,现在有一些约数条件xi,yi,zi,即xi获得的糖果数量-yi获得的糖果数量不超过zi,求最大满意度。
思路:最小割。应为求的是最大满意度,说以先要化为最小。约数条件就建立inf边,这样就不会进行穿过inf边的分割,约数条件也就成立了。具体建图看代码。
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
#define PI acos(-1.0)
const int maxn=1e5+,maxm=1e5+,inf=0x3f3f3f3f,mod=1e9+;
const ll INF=1e18+;
struct edge
{
int from,to,cap,flow;
};
vector<edge>es;
vector<int>G[maxn];
bool vis[maxn];
int dist[maxn];
int iter[maxn];
void init(int n)
{
for(int i=; i<=n+; i++) G[i].clear();
es.clear();
}
void addedge(int from,int to,int cap)
{
es.push_back((edge)
{
from,to,cap,
});
es.push_back((edge)
{
to,from,,
});
int x=es.size();
G[from].push_back(x-);
G[to].push_back(x-);
}
bool BFS(int s,int t)
{
memset(vis,,sizeof(vis));
queue <int> Q;
vis[s]=;
dist[s]=;
Q.push(s);
while(!Q.empty())
{
int u=Q.front();
Q.pop();
for (int i=; i<G[u].size(); i++)
{
edge &e=es[G[u][i]];
if (!vis[e.to]&&e.cap>e.flow)
{
vis[e.to]=;
dist[e.to]=dist[u]+;
Q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int u,int t,int f)
{
if(u==t||f==) return f;
int flow=,d;
for(int &i=iter[u]; i<G[u].size(); i++)
{
edge &e=es[G[u][i]];
if(dist[u]+==dist[e.to]&&(d=DFS(e.to,t,min(f,e.cap-e.flow)))>)
{
e.flow+=d;
es[G[u][i]^].flow-=d;
flow+=d;
f-=d;
if (f==) break;
}
}
return flow;
}
int Maxflow(int s,int t)
{
int flow=;
while(BFS(s,t))
{
memset(iter,,sizeof(iter));
int d=;
while(d=DFS(s,t,inf)) flow+=d;
}
return flow;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
int s=,t=n*m+;
for(int i=; i<=n; i++)
{
addedge(s,(i-)*m+,inf);
for(int j=; j<=m; j++)
{
int x;
scanf("%d",&x);
if(j<m) addedge((i-)*m+j,(i-)*m+j+,-x);
else addedge((i-)*m+j,t,-x);
}
}
for(int i=; i<=k; i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
for(int i=; i<=m; i++)
{
for(int j=; j<=m; j++)
{
if(i-j>z)
{
if(j<m) addedge((x-)*m+i,(y-)*m+j+,inf);
else addedge((x-)*m+i,t,inf);
}
}
}
}
int ans=Maxflow(s,t);
if(ans>=inf) printf("-1\n");
else printf("%d\n",*n-ans);
init(n*m);
}
return ;
}
最小割
HDU 6126.Give out candies 最小割的更多相关文章
- hdu 6126 Give out candies
hdu 6126 Give out candies(最小割) 题意: 有\(n\)个小朋友,标号为\(1\)到\(n\),你要给每个小朋友至少\(1\)个且至多\(m\)个的糖果.小朋友们共提出\(k ...
- 【HDU 6126】Give out candies 最小割
题意 有$n$个小朋友,给每个人分$1~m$个糖果,有k个限制 限制形如$(x,y,z)$ 表示第$x$个人分到的糖数减去第$y$个人分到的糖数不大于$z$,给第$i$个人$j$颗糖获 ...
- HDU 4289:Control(最小割)
http://acm.hdu.edu.cn/showproblem.php?pid=4289 题意:有n个城市,m条无向边,小偷要从s点开始逃到d点,在每个城市安放监控的花费是sa[i],问最小花费可 ...
- HDU 3452 Bonsai(网络流之最小割)
题目地址:HDU 3452 最小割水题. 源点为根节点.再另设一汇点,汇点与叶子连边. 对叶子结点的推断是看度数是否为1. 代码例如以下: #include <iostream> #inc ...
- HDU 5889 Barricade 【BFS+最小割 网络流】(2016 ACM/ICPC Asia Regional Qingdao Online)
Barricade Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- HDU 3526 Computer Assembling(最小割)
http://acm.hdu.edu.cn/showproblem.php?pid=3526 题意:有个屌丝要配置电脑,现在有n个配件需要购买,有两家公司出售这n个配件,还有m个条件是如果配件x和配件 ...
- HDU 6214 Smallest Minimum Cut 最小割,权值编码
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6214 题意:求边数最小的割. 解法: 建边的时候每条边权 w = w * (E + 1) + 1; 这 ...
- HDU 3251 Being a Hero(最小割+输出割边)
Problem DescriptionYou are the hero who saved your country. As promised, the king will give you some ...
- HDU 3691 Nubulsa Expo(全局最小割)
Problem DescriptionYou may not hear about Nubulsa, an island country on the Pacific Ocean. Nubulsa i ...
随机推荐
- table 合并内容相同的第一列
function mergeCells() { var tbodyTlth = $("#datatable_ajax1 tbody").find("tr").l ...
- nutch笔记
1.Nutch 是一个开源Java实现的搜索引擎.它提供了我们运行自己的搜索引擎所需的全部工具.包括全文搜索和Web爬虫.
- SpringCloud报错:Caused by: org.yaml.snakeyaml.parser.ParserException: while parsing MappingNode
今天在配置eureka集群时,SpringCloud报错如下: Caused by: org.yaml.snakeyaml.parser.ParserException: while parsing ...
- AttributeError: 'dict' object has no attribute 'iteritems'
在python3.6中运行 sortedClassCount = sorted(classCount.iteritems(), key=operator.itemgetter(1), reverse= ...
- codeforces 722D Generating Sets 【优先队列】
You are given a set Y of n distinct positive integers y1, y2, ..., yn. Set X of n distinct positive ...
- Codeforces Beta Round #57 (Div. 2)
Codeforces Beta Round #57 (Div. 2) http://codeforces.com/contest/61 A #include<bits/stdc++.h> ...
- TZOJ 2546 Electricity(去掉割点后形成的最大连通图数)
描述 Blackouts and Dark Nights (also known as ACM++) is a company that provides electricity. The compa ...
- ubuntu下安装、破解securtCRT工具
官网:https://www.vandyke.com/ 下载好的安装包以及破解工具的网盘地址:链接: https://pan.baidu.com/s/1UEKEy-aob7WdfPLR4PNJmg 提 ...
- 将jsp页面转pdf
网上好多思路啊,大部分都是将html转pdf,这种方法我试了很多,都不能很好地支持jsp,稍微复杂一点根本不起作用,也不知他们的博客都怎么写的,还真是应了那句话天下博客一大抄,自己都不验证的 下面说下 ...
- session高级(session入库)
我们知道,session是一种会话技术,用来实现跨脚本共享数据. 在之前的php会话技术中我们介绍过,session是存放在服务器端的文件里的,因此session有可能因为文件数量过多,会在查询ses ...