SPOJ Coconuts 最大流 最小割
A group of n castle guards are voting to determine whether African swallows can carry coconuts. While each guard has his own personal opinion on the matter, a guard will often vote contrary to his beliefs in order to avoid disagreeing with the votes of his friends.
You are given a list of guards who either do or do not believe in the coconut-carrying capacity of African swallows, and a list of all pairs of guards who are friends. Your task is to determine how each guard must vote in order to minimize the sum of the total number of disagreements between friends and the total number of guards who must vote against their own beliefs.
Input
The input to this problem will contain multiple test cases. Each test case begins with a single line containing an integer n (where 2 <= n <= 300), the number of guards, and an integer m (where 1 <= m <= n(n-1)/2), the number of pairs of guards who are friends. The second line of the test case contains n integers, where the ith integer is 1 if the ith guard believes in the ability of African swallows to carry coconuts, and 0 otherwise. Finally, the next m lines of the test case each contain two distinct integers i and j (where 1 <= i, j <= n), indicating that guards i and j are friends. Guards within each pair of friends may be listed in any order, but no pair of guards will be repeated. The input is terminated by an invalid test case with n = m = 0, which should not be processed.
Output
For each input test case, print a single line containing the minimum possible sum of the total number of disagreements between all friends plus the total number of guards who must vote against their own beliefs.
Example
- Input:
- 3 3
- 1 0 0
- 1 2
- 1 3
- 3 2
- 6 6
- 1 1 1 0 0 0
- 1 2
- 2 3
- 4 2
- 3 5
- 4 5
- 5 6
- 0 0
- Output:
- 1
- 2
- 题解:
这是经典的2选1问题 建图方法:
将所有为1的点建(s,i,1),(i,t,0)
所有为0的点建(s,i,0),(i,t,1)
然后每一组好朋友就连(i,j,1)(j,i,1)
然后跑一遍最大流就是答案
自己的理解:流只会从权为1的点流出来,并且只会流向权为0的点然后到t 产生1点流量 表示产生一组矛盾或不得不改变自己意愿,流满以后就表示 所有不得不产生的矛盾都产生了,所以最大流就是答案
SPOJ Coconuts 最大流 最小割的更多相关文章
- 最大流-最小割 MAXFLOW-MINCUT ISAP
简单的叙述就不必了. 对于一个图,我们要找最大流,对于基于增广路径的算法,首先必须要建立反向边. 反向边的正确性: 我努力查找了许多资料,都没有找到理论上关于反向边正确性的证明. 但事实上,我们不难理 ...
- 最大流&最小割 - 专题练习
[例1][hdu5889] - 算法结合(BFS+Dinic) 题意 \(N\)个点\(M\)条路径,每条路径长度为\(1\),敌人从\(M\)节点点要进攻\(1\)节点,敌人总是选择最优路径即最短路 ...
- UVa11248 Frequency Hopping(最大流+最小割)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=33206 [思路] 最大流最小割. 可以确定的是如果不可行需要修改的 ...
- matlab练习程序(最大流/最小割)
学习这个算法是为学习图像处理中的图割算法做准备的. 基本概念: 1.最大流是一个有向图. 2.一个流是最大流,当且仅当它的残余网络中不包括增广路径. 3.最小割就是网络中所有割中值最小的那个割,最小割 ...
- 【BZOJ2400】Spoj 839 Optimal Marks 最小割
[BZOJ2400]Spoj 839 Optimal Marks Description 定义无向图中的一条边的值为:这条边连接的两个点的值的异或值. 定义一个无向图的值为:这个无向图所有边的值的和. ...
- 「网络流24题」「LuoguP2774」方格取数问题(最大流 最小割
Description 在一个有 m*n 个方格的棋盘中,每个方格中有一个正整数.现要从方格中取数,使任意 2 个数所在方格没有公共边,且取出的数的总和最大.试设计一个满足要求的取数算法.对于给定的方 ...
- HDU6582 Path【优先队列优化最短路 + dinic最大流 == 最小割】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6582 来源:2019 Multi-University Training Contest 1 题目大意 ...
- ISAP 最大流 最小割 模板
虽然这道题用最小割没有做出来,但是这个板子还是很棒: #include<stdio.h> #include<math.h> #include<string.h> # ...
- Codeforces 965 枚举轮数贪心分糖果 青蛙跳石头最大流=最小割思想 trie启发式合并
A /*#include<cstring>#include<algorithm>#include<queue>#include<vector>#incl ...
随机推荐
- 每日冲刺报告——Day3(Java-Team)
第三天报告(11.4 周六) 团队:Java-Team 成员: 章辉宇(284) 吴政楠(286) 陈阳(PM:288) 韩华颂(142) 胡志权(143) github地址:https://git ...
- 02-移动端开发教程-CSS3新特性(中)
1. 新的背景 背景在CSS3中也得到很大程度的增强,比如背景图片尺寸.背景裁切区域.背景定位参照点.多重背景等. 1.1 background-size设置背景图片的尺寸 cover会自动调整缩放比 ...
- 【转】Python处理wave文件
#本文PDF版下载 Python解析Wav文件并绘制波形的方法 #本文代码下载 Wav波形绘图代码 #本文实例音频文件night.wav下载 音频文件下载 (石进-夜的钢琴曲) 前言 在现在繁忙的生活 ...
- python time、datetime、random、os、sys模块
一.模块1.定义模块:用来从逻辑上组织Python代码(变量,函数,类,逻辑:实现一个功能),本质就是.py结尾的python文件(文件名:test.py,对应的模块名:test)包:用来从逻辑上组织 ...
- selenium的Python使用(一)浏览器驱动的安装及使用
一.selenium的安装 直接使用pip进行安装 pip install selenium #(安装最新版本) pip install selenium==3.6.0 #(安装指定版本) ...
- JavaScript 实现二叉树
JavaScript 实现二叉树: // JavaScript 实现二叉树 function BinaryTree () { var Node = function (key) { this.key ...
- JavaScript 基础学习1-day14
JavaScript 基础学习1 知识预览JavaScript概述二 JavaScript的基础三 JavaScript的对象BOM对象DOM对象实例练习js扩展 JavaScript概述 JavaS ...
- 20165230 2017-2018-2 《Java程序设计》第4周学习总结
20165230 2017-2018-2 <Java程序设计>第4周学习总结 教材学习内容总结 子类与继承 通过class 子类名 extends 父类名定义子类.子类只能继承一个父类,关 ...
- 九、Python+Selenium模拟用QQ登陆腾讯课堂,并提取报名课程(练习)
研究QQ登录规则的话,得分析大量Javascript的加密解密,比较耗时间.自己也是练习很少,短时间成功不了.所以走了个捷径. Selenium是一个WEB自动化测试工具,它运行时会直接实例化出一个浏 ...
- Java基础语法<六> 数组 Arrays
笔记整理 来源于<Java核心技术卷 I > <Java编程思想> 允许数组长度为0 new element[0] 数组长度为0与null不同 1. 数组拷贝 允许将一 ...