SPOJ1693 COCONUTS - Coconuts
传送门[洛谷]
自闭QAQ 什么玩意QAQ
不是很理解到底在干啥 问了巨佬以后大概是这个样子的
可以看出是最小割模型
对于每一个人 反悔的话就是代价+1
那么连接(s,i) (i,t)分别表示他最后选择赞同还是反对
根据初始状态来填代价
然后针对基友关系 他们之间连 代价为1的无向边
为什么是无向边呢 是因为 他们无论双方在哪个方向反对 只要不属于同一边的话就是有代价的
Edelweiss的PDF里还有这样的一个小补充
对于求一个函数 的最小值(其中
)
对于(二者取其一)(不同有代价)这样的模型我们也可以用类似的方法建立最小割来求解
比较有趣。附代码。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#define inf 20021225
#define ll long long
#define mxm 360010
#define mxn 310
using namespace std;
struct edge{int to,lt,f;}e[mxm];
int in[mxn],s,t,cnt=1,dep[mxn];
void add(int x,int y,int f)
{
e[++cnt].to=y;e[cnt].lt=in[x];e[cnt].f=f;in[x]=cnt;
e[++cnt].to=x;e[cnt].lt=in[y];e[cnt].f=0;in[y]=cnt;
}
queue<int> que;
bool bfs()
{
memset(dep,0,sizeof(dep));
dep[s]=1;while(!que.empty()) que.pop();
que.push(s);
while(!que.empty())
{
int x=que.front();que.pop();
for(int i=in[x];i;i=e[i].lt)
{
int y=e[i].to;if(dep[y]||!e[i].f) continue;
dep[y]=dep[x]+1;que.push(y);
if(y==t) return true;
}
}
return false;
}
int dfs(int x,int flow)
{
if(x==t||!flow) return flow;
int cur=flow;
for(int i=in[x];i;i=e[i].lt)
{
int y=e[i].to;
if(dep[y]==dep[x]+1&&e[i].f)
{
int tmp=dfs(y,min(e[i].f,cur));
cur-=tmp;e[i].f-=tmp;e[i^1].f+=tmp;
if(!cur) return flow;
}
}
dep[x]=-1;return flow-cur;
}
int dinic()
{
int ans=0;
while(bfs())
ans+=dfs(s,inf);
return ans;
}
int main()
{
int n,m,x,y;
while(scanf("%d%d",&n,&m))
{
if(n==0&&m==0) break;
s=n+1;t=s+1;
memset(in,0,sizeof(in));
cnt=1;
for(int i=1;i<=n;i++)
{
scanf("%d",&x);
if(x) add(s,i,1),add(i,t,0);
else add(s,i,0),add(i,t,1);
}
for(int i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
add(x,y,1);add(y,x,1);
}
printf("%d\n",dinic());
}
return 0;
}
SPOJ1693 COCONUTS - Coconuts的更多相关文章
- spoj 1693 COCONUTS - Coconuts【最小割】
s向所有信仰1的人连(s,i,1),所有信仰0的人连(i,t,1),对于朋友关系,连接双向边,流量为1.跑最大流的结果即为答案. 考虑这样做的意义.最小割就是把总点集分割为两个点集S,T,使得所有\( ...
- 2016 长春东北赛---Coconuts(离散化+DFS)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5925 Problem Description TanBig, a friend of Mr. Frog ...
- hdu 5925 Coconuts 离散化+dfs
Coconuts Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem ...
- HDU 5925 Coconuts 【离散化+BFS】 (2016CCPC东北地区大学生程序设计竞赛)
Coconuts Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- SPOJ Coconuts 最大流 最小割
A group of n castle guards are voting to determine whether African swallows can carry coconuts. Whil ...
- Coconuts HDU - 5925 (二维离散化求连通块的个数以及大小)
题目链接: D - Coconuts HDU - 5925 题目大意:首先是T组测试样例,然后给你n*m的矩阵,原先矩阵里面都是白色的点,然后再输入k个黑色的点.这k个黑色的点可能会使得原先白色的点 ...
- Coconuts, Revisited(递推+枚举+模拟)
Description The short story titled Coconuts, by Ben Ames Williams, appeared in the Saturday Evening ...
- HDU 5925 Coconuts 离散化
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5925 Coconuts Time Limit: 9000/4500 MS (Java/Others) ...
- Coconuts HDU - 5925 二维离散化 自闭了
TanBig, a friend of Mr. Frog, likes eating very much, so he always has dreams about eating. One day, ...
随机推荐
- 关于Reporting Services网站
1.http://www.c-sharpcorner.com/search/sql%20server%20reporting%20services 2.https://msdn.microsoft.c ...
- C# get和set
Day1:知识点 1.封装 自己的理解: 可复用,低耦合.就是拿过来就用 只需要关注名称,返回值,参数及参数类型,不需要知道里面怎么实现的. 字段封装的快捷键 (ctrl+r+e) 将光标放在将要封 ...
- 洛谷P2661 信息传递(最小环,并查集)
洛谷P2661 信息传递 最小环求解采用并查集求最小环. 只适用于本题的情况.对于新加可以使得两个子树合并的边,总有其中一点为其中一棵子树的根. 复杂度 \(O(n)\) . #include< ...
- 接口自动化之cookies登录
现在有很多网站有验证码,跳过验证码实现登录可以使用cookies登录 目录 1.requests的添加cookies的方法 2.举个栗子 1.requests的添加cookies的方法 request ...
- 鸿蒙系统开源学习经验分享HarmonyOS[www.allharmonyos.com]
分享鸿蒙系统开源知识,分享学习经验,分享鸿蒙系统开发经验 www.allharmonyos.com https://gitee.com/allharmonyos https://github.com/ ...
- composer的自动加载机制(autoload)
composer的出现真是让人们眼前一亮,web开发从此变成了一件很『好玩』的事情,开发一个CMS就像在搭积木,从packagist中取出『积木』搭建在自己的代码中,一点一点搭建出一个属于自己的王国. ...
- MySQL 查询语句--------------进阶9:联合查询
#进阶9:联合查询 /* union 联合 合并:将多条查询语句的结果合并成一个结果 语法: 查询语句1 union 查询语句2 union..... 应用场景:要查询的结果来自于多个表,且多个表没有 ...
- servlet--response、request
请求响应流程图 response 1 response概述 response是Servlet.service方法的一个参数,类型为javax.servlet.http.HttpServle ...
- 《JAVA设计模式》之工厂方法模式 (Factory)
在阎宏博士的<JAVA与模式>一书中开头是这样描述工厂方法模式的: 工厂方法模式是类的创建模式,又叫做虚拟构造子(Virtual Constructor)模式或者多态性工厂(Polymor ...
- vscode内置c++ define选项
https://stackoverflow.com/questions/46415076/how-can-i-define-macros-for-the-c-intellisense-engine