[SHOI 2007] 善意的投票
2024-09-04 23:15:54
[题目链接]
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 ;
}
最新文章
- Oracle SQL Developer 连接 MySQL
- Android开发学习之路-关于Exception
- [PHP] - Laravel - 用户登陆中间件
- Druid数据库连接池配置
- [ubuntu]给ubuntu server安装xubuntu(xfce)窗口管理器
- 初识 TextKit
- 解决浏览器background-image属性不支持css3动画
- 万能poi导入功能模板
- 【AI】Exponential Stochastic Cellular Automata for Massively Parallel Inference - 大规模并行推理的指数随机元胞自动机
- @__CheckForDebuggerJustMyCode@4
- CSS那些事!这个篇幅是我特意开的,不是因为帮助小菜之类的,而是在多人的团队配合中各种命名冲突的规范让人蛋疼
- 程序媛计划——mysql基本操作
- 2017-2018-2 《网络对抗技术》 20155322 第五周 Exp2 后门原理与实践
- 24-[jQuery]-案例
- jquery中的each
- JDBC基本操作介绍
- oracle按照时间过滤
- Silverlight &; Blend动画设计系列六:动画技巧(Animation Techniques)之对象与路径转化、波感特效
- python-class(5)
- Sql注入基础_mysql注入
热门文章
- jsp网页在浏览器中不显示图片_eclipse环境下配置tomcat中jsp项目的虚拟路径
- LattePanda 之深入学习 Firmata通讯
- 3.nginx反向代理服务器+负载均衡
- git clean
- Andriod三步学会安卓自己定义视图及其属性
- 升级iOS8和iOS9系统后,保险箱Pro、私人保险箱、私密相冊打开就闪退的官方解决方式
- 惊艳的cygwin——Windows下的Linux命令行环境的配置和使用
- C# - Garbage Collection
- MVC入门——详细页
- EasyDarwin云存储方案调研:海康萤石云采用的是MPEG-PS打包的方式进行的存储