[SHOI 2007] 善意的投票
[题目链接]
https://www.lydsy.com/JudgeOnline/problem.php?id=1934
[算法]
首先 , 选择睡觉的人和不选择睡觉的人构成两个集合
这启发我们用最小割解决该问题 :
1. 将源点与每个睡觉的人连边 , 将每个不睡觉的人与汇点连边 , 割掉这样的一条边的含义是 : 有一个人放弃了睡觉 / 不睡觉 , 产生了1冲突
2. 将朋友之间连边 , 割掉这样一条边的含义是 : 这两个人产生了冲突
求解这个图的最小割即可
时间复杂度 : O(dinic(N , M))
[代码]
#include<bits/stdc++.h>
using namespace std;
#define N 310
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int inf = 1e9; struct edge
{
int to , w , nxt;
} e[N * N * ]; int tot , n , m , S , T;
int head[N] , dep[N]; template <typename T> inline void chkmin(T &x , T y) { x = min(x , y); }
template <typename T> inline void chkmax(T &x , T y) { x = max(x , y); }
template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
inline void addedge(int u , int v , int w)
{
++tot;
e[tot] = (edge){v , w , head[u]};
head[u] = tot;
++tot;
e[tot] = (edge){u , , head[v]};
head[v] = tot;
}
inline bool bfs(int s)
{
queue< int > q;
memset(dep , , sizeof(dep));
q.push(s);
dep[s] = ;
while (!q.empty())
{
int cur = q.front();
q.pop();
for (int i = head[cur]; i; i = e[i].nxt)
{
int v = e[i].to , w = e[i].w;
if (w > && dep[v] == -)
{
dep[v] = dep[cur] + ;
q.push(v);
if (v == T) return true;
}
}
}
return false;
}
inline int dinic(int u , int flow)
{
int rest = flow;
if (u == T)
return flow;
for (int i = head[u]; i && rest; i = e[i].nxt)
{
int v = e[i].to , w = e[i].w;
if (w > && dep[v] == dep[u] + )
{
int k = dinic(v , min(rest , w));
if (!k) dep[v] = ;
rest -= k;
e[i].w -= k;
e[i ^ ].w += k;
}
}
return flow - rest;
} int main()
{ read(n); read(m);
tot = ;
S = n + , T = S + ;
for (int i = ; i <= n; i++)
{
int x;
read(x);
if (!x) addedge(S , i , );
else addedge(i , T , );
}
for (int i = ; i <= m; i++)
{
int x , y;
read(x); read(y);
addedge(x , y , );
addedge(y , x , );
}
int ans = ;
while (bfs(S))
{
while (int flow = dinic(S , inf)) ans += flow;
}
printf("%d\n" , ans); return ;
}
[SHOI 2007] 善意的投票的更多相关文章
- [SHTSC 2007] 善意的投票
我就是来复习Dinic算法的,仅10天不写,我已经退化成写一遍+调试需要接近一个小时了,当然其中不乏在网上乱逛的时间… 赞成从S源点连一条单向边,反对向T汇点连一条单向边,朋友关系连双向边. 但是总感 ...
- 「SHOI2007」「Codevs2341」 善意的投票(最小割
2341 善意的投票 2007年省队选拔赛上海市队选拔赛 时间限制: 5 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 幼儿园里有n个小朋 ...
- C++之路进阶——bzoj1934(善意的投票)
F.A.Qs Home Discuss ProblemSet Status Ranklist Contest ModifyUser hyxzc Logout 捐赠本站 Notice:由于本OJ建立在 ...
- BZOJ-1934 Vote 善意的投票 最大流+建图
1934: [Shoi2007]Vote 善意的投票 Time Limit: 1 Sec Memory Limit: 64 MB Submit: 1551 Solved: 951 [Submit][S ...
- bzoj1934: [Shoi2007]Vote 善意的投票
最大流..建图方式都是玄学啊.. //Dinic是O(n2m)的. #include<cstdio> #include<cstring> #include<cctype& ...
- BZOJ 1934: [Shoi2007]Vote 善意的投票 最小割
1934: [Shoi2007]Vote 善意的投票 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnl ...
- 1934: [Shoi2007]Vote 善意的投票
1934: [Shoi2007]Vote 善意的投票 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 1174 Solved: 723[Submit][S ...
- 【BZOJ1934】善意的投票(网络流)
[BZOJ1934]善意的投票(网络流) 题面 Description 幼儿园里有n个小朋友打算通过投票来决定睡不睡午觉.对他们来说,这个问题并不是很重要,于是他们决定发扬谦让精神.虽然每个人都有自己 ...
- BZOJ_1934_[Shoi2007]Vote 善意的投票
BZOJ_1934_[Shoi2007]Vote 善意的投票 Description 幼儿园里有n个小朋友打算通过投票来决定睡不睡午觉.对他们来说,这个问题并不是很重要,于是他们决定发扬谦让精神.虽然 ...
随机推荐
- 线性回归,logistic回归分类
学习过程 下面是一个典型的机器学习的过程,首先给出一个输入数据,我们的算法会通过一系列的过程得到一个估计的函数,这个函数有能力对没有见过的新数据给出一个新的估计,也被称为构建一个模型.就如同上面的线性 ...
- 安装mongoDB遇见的一个路径问题
如果安装路径不存在,则不会解压EXE软件! 安装monogoDB后,它不会自动添加执行路径! 意思就是安装路径是D盘下面的mongoDB文件夹,假如不存在这个文件夹,则不会安装成功 你需要添加路径: ...
- match excel test search replace 用法
1 test:测试string是否包含有匹配结果,包含返回true,不包含返回false. 2 reg.test(str) 3 <script type="text/javascrip ...
- xhtml三种元素类型
xhtml三种元素类型:块级元素/内联元素/可变元素 块级元素:独占一行.可自定义自己的宽度和高度.作为其他元素的容器,可容纳其他内联元素和块级元素,喻做一个盒子.内联元素:始终以行内逐个显示.不能设 ...
- 【转载】How to develop your own Boot Loader【怎么样开发自己的bootloader】
How to develop your own Boot Loader 怎么样开发自己的bootloader Table of content[目录] 1. Who may be interested ...
- jquery+easyui主界面布局一例
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="workbench.aspx ...
- RecyclerView的那点事儿
RecyclerView 控件简单介绍 ListView的升级版 LinearLayoutManager GridLayoutManager StaggeredGridLayoutManager 定制 ...
- EasyRTMP实现的一套简单、高效、易用的全平台(Windows/Linux/ARM/Android/iOS)RTMP直播推送库
本文转自EasyDarwin开源团队成员Kim的博客:http://blog.csdn.net/jinlong0603/article/details/52938980 EasyRTMP介绍 Easy ...
- easyui datagrid 加载静态文件中的json数据
本文主要介绍easyui datagrid 怎么加载静态文件里的json数据,开发环境vs2012, 一.json文件所处的位置 二.json文件内容 {"total":28,&q ...
- 无感知的用同步的代码编写方式达到异步IO的效果和性能,避免了传统异步回调所带来的离散的代码逻辑和陷入多层回调中导致代码无法维护
golang/goroutine 和 swoole/coroutine 协程性能测试对比 - Go语言中文网 - Golang中文社区 https://studygolang.com/articles ...