【BZOJ - 1305】dance跳舞(拆点网络流,建图,最大流,残留网络上跑最大流)

题干:

一次舞会有n个男孩和n个女孩。每首曲子开始时,所有男孩和女孩恰好配成n对跳交谊舞。每个男孩都不会和同一个女孩跳两首(或更多)舞曲。有一些男孩女孩相互喜欢,而其他相互不喜欢(不会“单向喜欢”)。每个男孩最多只愿意和k个不喜欢的女孩跳舞,而每个女孩也最多只愿意和k个不喜欢的男孩跳舞。给出每对男孩女孩是否相互喜欢的信息,舞会最多能有几首舞曲?

Input

第一行包含两个整数n和k。以下n行每行包含n个字符,其中第i行第j个字符为'Y'当且仅当男孩i和女孩j相互喜欢。

Output

仅一个数,即舞曲数目的最大值。

Sample Input

3 0

YYY

YYY

YYY

Sample Output

3

Hint

N<=50 K<=30

解题报告:

  这题网络流,做法很多。可以直接二分可以跳多少支舞,然后网络流check一下。也可以每次每次的跑,直到第一次凑不满n个人为止。也可以每次给他一条流量为1的空间,让他凑n个人,然后再给他流量为1的空间,让他在残留网络上凑n个人,这样直到第一次凑不出n个人的时候break。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
int n,k,N;
int tot;
struct Edge {int to,ne,w;
} e[100005 * 2];
int head[10005];
int st,ed;
int dis[10050],q[10005];
void add(int u,int v,int w,int rw=0) {e[++tot].to=v;e[tot].w=w;e[tot].ne=head[u];head[u]=tot;e[++tot].to=u;e[tot].w=rw;e[tot].ne=head[v];head[v]=tot;	
}
bool bfs(int st,int ed) {memset(dis,-1,sizeof(dis));int front=0,tail=0;q[tail++]=st;dis[st]=0;while(front<tail) {int cur = q[front];if(cur == ed) return 1;front++;for(int i = head[cur]; i!=-1; i = e[i].ne) {if(e[i].w&&dis[e[i].to]<0) {q[tail++]=e[i].to;dis[e[i].to]=dis[cur]+1;}}}if(dis[ed]==-1) return 0;return 1;
}
int dfs(int cur,int limit) {//limit为源点到这个点的路径上的最小边权 if(limit==0||cur==ed) return limit;int w,flow=0;for(int i = head[cur]; i!=-1; i = e[i].ne) {		if(e[i].w&&dis[e[i].to]==dis[cur]+1) {w=dfs(e[i].to,min(limit,e[i].w));e[i].w-=w;e[i^1].w+=w;flow+=w;limit-=w;if(limit==0) break;}}if(!flow) dis[cur]=-1;return flow;
}
int dinic() {int ans = 0;while(bfs(st,ed)) ans+=dfs(st,0x7fffffff);return ans;
}
char s[55][55];
int main() {cin>>n>>k;st=4*n+1,ed=st+1;N=ed+1;//init tot=1;for(int i = 1; i<=N; i++) head[i] = -1;for(int i = 1; i<=n; i++) {scanf("%s",s[i]+1);add(i,i+n,k);add(i+2*n,i+3*n,k);		}for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) {if(s[i][j] == 'Y') add(i,j+3*n,1);else add(i+n,j+2*n,1);}}for(int ans = 0; ;ans++) {for(int i = 1; i<=n; i++) {add(st,i,1);add(3*n+i,ed,1);}if(dinic() != n) {printf("%d\n",ans);break;}				}			return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440306.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

《TCP/IP详解》学习笔记(四):ICMP 协议、ping 和 Traceroute

ICMP 协议介绍 前面讲到了&#xff0c;IP 协议并不是一个可靠的协议&#xff0c;它不保证数据被成功送达&#xff0c;那么自然的&#xff0c;保证数据送达的工作应该由其他的模块来完 成。其中一个重要的模块就是 ICMP(网络控制报文)协议。 当传送 IP 数据包发生错误--比如主机…

【HDU - 1530】Maximum Clique(最大团问题,图论)

题干&#xff1a; Given a graph G(V, E), a clique is a sub-graph g(v, e), so that for all vertex pairs v1, v2 in v, there exists an edge (v1, v2) in e. Maximum clique is the clique that has maximum number of vertex. Input Input contains multiple tests. …

Apollo自动驾驶入门课程第①讲—无人驾驶概览

目录 1. 全面了解自动驾驶主要模块 2. 了解无人车的运作方式 3. 开放式软件栈 4. 本节其他重点 本文转自微信公众号&#xff1a;Apollo开发者社区 原创&#xff1a; 阿波君 Apollo开发者社区 8月1日 在Apollo 3.0发布的同时&#xff0c;我们面向更多对自动驾驶感兴趣的开发…

【CF - 699C】 Vacations (日程安排 dp)

题干&#xff1a; Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day…

《TCP/IP详解》学习笔记(五):IP选路、动态选路

静态 IP 选路 1一个简单的路由表 选路是 IP 层最重要的功能之一。前面的部分已经简单的讲过路由器是如何根据 IP 数据包的 IP 地址来选择路由的。 这里就不重复了。首先来看看一个简单的系统路由表&#xff1a; 对于一个给定的路由器,可以打印出五种不同的 flag&#xff1a; …

LeetCode刷题实战(43):Multiply Strings

题目描述&#xff1a; 43Multiply Strings28.7%Medium Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string. Example 1: Input: num1 "2", num2 "3" Output…

【2018山东省赛 - A】Anagram(贪心,费用流,KM算法)

题干&#xff1a; Problem Description Orz has two strings of the same length: A and B. Now she wants to transform A into an anagram of B (which means, a rearrangement of B) by changing some of its letters. The only operation the girl can make is to “incr…

【2018ACM山东省赛 - B】Bullet(二分 + 二分图匹配,匈牙利算法,卡常)

题干&#xff1a; Problem Description In GGO, a world dominated by gun and steel, players are fighting for the honor of being the strongest gunmen. Player Shino is a sniper, and her aimed shot kills one monster at a time. Now she is in an nnn \times nnn m…

13.Data Leakage

本教程是ML系列的一部分。在此步骤中&#xff0c;你将学习什么是data leakage及如何预防它。 What is Data Leakage 数据泄漏是数据科学家需要理解的最重要问题之一。 如果您不知道如何防止它&#xff0c;则会频繁出现泄漏&#xff0c;并且会以最微妙和危险的方式破坏您的模…

0.Overview----Machine Learning

本文为Kaggle Learn的Machine Learning课程的中文翻译&#xff0c;原文链接为&#xff1a;https://www.kaggle.com/learn/machine-learning 1.How Models Work The first step if youre new to machine learning 2.Explore Your Data Load data and set up your environment …

【2018ACM山东省赛 - C】Cities(最小生成树变形优化,贪心思维)

题干&#xff1a; Problem Description There are nnn cities in Byteland, and the ithi_{th}ith​ city has a value aia_iai​. The cost of building a bidirectional road between two cities is the sum of their values. Please calculate the minimum cost of connec…

项目总结1:微信扫码自动识别设备类型并跳转到相应的应用下载页面(apk或App Store)之解决方案

问题分析&#xff1a;普通页面一般无法调用微信的扫一扫接口&#xff0c;从而否定通过微信扫一扫功能给我们判断当前扫码的设备类型。 解决方案&#xff1a;通过应用下载页面自身来获取当前访问的客户端设备类型&#xff08;iPhone、Android、iPad&#xff09;&#xff0c;然后…

《TCP/IP详解》学习笔记(六):UDP 协议

UDP 简要介绍 UDP 是传输层协议&#xff0c;和 TCP 协议处于一个分层中&#xff0c;但是与 TCP 协议不同&#xff0c;UDP 协议并不提供超时重传&#xff0c;出错重传等功能&#xff0c;也就是说其是不可靠的协议。 UDP 协议头 1UDP 端口号 由于很多软件需要用到 UDP 协议&am…

【2018ACM山东省赛 - E】Sequence(树状数组,思维,优化)

题干&#xff1a; We define an element aia_iai​ in a sequence "good", if and only if there exists a j(1≤j<i)j(1\le j < i)j(1≤j<i) such that aj<aia_j < a_iaj​<ai​. Given a permutation ppp of integers from 111 to nnn. Remove …

Apollo自动驾驶入门课程第②讲 — 高精地图

目录 1. 高精地图与传统地图 2. 高精地图与定位、感知规划的关系 2.1 高精地图用于定位 2.2 高精地图用于感知 2.3 高精地图用于规划 3. Apollo高精度地图与构建 3.1 Apollo高精地图 3.2 Apollo高精地图的构建 本文转自微信公众号&#xff1a; Apollo开发者社区 原创&a…

项目总结2:ionic3开发跨平台App如何设置和替换应用图标及启动图

前言&#xff1a; 和原生开发一样&#xff0c;ionic官方提供的设置方式也很简单&#xff0c;只不过多了一个步骤&#xff1a;基于ionic命令的方式自动修改全局的配置文件config.xml。 设置或替换应用图标和应用启动图&#xff1a; 把UI提供的图标拿过来改成特定的名称"i…

【2018ACM山东省赛 - G】Games(Nim博弈 + dp)

题干&#xff1a; Problem Description Alice and Bob are playing a stone game. There are nnn piles of stones. In each turn, a player can remove some stones from a pile (the number must be positive and not greater than the number of remaining stones in the …

LeetCode刷题实战(13):Roman to Integer

题目描述&#xff1a; 13 Roman to Integer 49.5%Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D …

项目总结3:ionic3开发的App在启动过程中部分手机出现白屏或黑屏问题之终极解决方案

问题场景&#xff1a;采用ionic3开发的App&#xff0c;当项目比较大的时候&#xff0c;会出现部分真机设备在启动的过程中有白屏或黑屏的情况。 原因预测&#xff1a;个别手机&#xff0c;尤其是安卓手机的性能比较差&#xff0c;App在启动后进入首页或登录页前的初始化工作还…

1.Intro to Deep Learning and Computer Vision

Intro 这是Kaggle深度学习教育课程的第一课。 在本课程结束后&#xff0c;您将了解卷积。 卷积是计算机视觉&#xff08;以及许多其他应用程序&#xff09;中深度学习模型的基本构建块。 之后&#xff0c;我们将很快开始使用世界一流的深度学习模型。 Lesson [1] from IPy…