【BZOJ4155】[Ipsc2015]Humble Captains 最小割+DP
【BZOJ4155】[Ipsc2015]Humble Captains
Description
Input
Output
Sample Input
3 3
1 2
2 3
1 3
3 1
1 3
Sample Output
1 0
HINT
题解:第一问不会的去练最小割,第二问不会的去做阿狸和桃子的游戏。
然而本题第二问求的是差最小,那么把贪心换成 搭建双塔 即可。
但是复杂度有点高,用bitset优化即可。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <bitset>
using namespace std;
int n,m,T,cnt,ans;
int pa[300],pb[300],to[100000],next[100000],val[100000],head[300],d[300],D[300];
bitset<80010> f;
queue<int> q;
inline void add(int a,int b)
{
to[cnt]=b,val[cnt]=1,next[cnt]=head[a],head[a]=cnt++;
to[cnt]=a,val[cnt]=1,next[cnt]=head[b],head[b]=cnt++;
}
int dfs(int x,int mf)
{
if(x==2) return mf;
int i,temp=mf,k;
for(i=head[x];i!=-1;i=next[i]) if(d[to[i]]==d[x]+1&&val[i])
{
k=dfs(to[i],min(temp,val[i]));
if(!k) d[to[i]]=0;
val[i]-=k,val[i^1]+=k,temp-=k;
if(!temp) break;
}
return mf-temp;
}
inline int bfs()
{
memset(d,0,sizeof(d));
while(!q.empty()) q.pop();
int i,u;
d[1]=1,q.push(1);
while(!q.empty())
{
u=q.front(),q.pop();
for(i=head[u];i!=-1;i=next[i]) if(!d[to[i]]&&val[i])
{
d[to[i]]=d[u]+1;
if(to[i]==2) return 1;
q.push(to[i]);
}
}
return 0;
}
inline int rd()
{
int ret=0,f=1; char gc=getchar();
while(gc<'0'||gc>'9') {if(gc=='-') f=-f; gc=getchar();}
while(gc>='0'&&gc<='9') ret=ret*10+gc-'0',gc=getchar();
return ret*f;
}
void work()
{
n=rd(),m=rd(),ans=0;
int i,a,b;
memset(head,-1,sizeof(head)),memset(D,0,sizeof(D)),cnt=0;
for(i=1;i<=m;i++) a=rd(),b=rd(),add(a,b),D[a]++,D[b]++;
while(bfs()) ans+=dfs(1,1<<30);
printf("%d ",m-ans);
f.reset(),f[n*n+D[1]-D[2]]=1;
for(i=3;i<=n;i++) f=(f<<D[i])|(f>>D[i]);
for(i=0;i<=n*n;i++) if(f[n*n+i]||f[n*n-i])
{
printf("%d\n",i>>1);
return ;
}
}
int main()
{
T=rd();
while(T--) work();
return 0;
}
【BZOJ4155】[Ipsc2015]Humble Captains 最小割+DP的更多相关文章
- BZOJ4155 : [Ipsc2015]Humble Captains
第一问最小割,第二问: 设du[i]表示i点的度数,则要最小化$\frac{|1集合的du[i]之和-2集合的du[i]之和|}{2}$, 压位01背包即可. #include<cstdio&g ...
- 【BZOJ4155】[Ipsc2015]Humble Captains
题解: 第一问裸的最小割 第二问考虑贪心 我们把边权平均分配给两个点 然后就变成了给n个数分两组差最小 np-hard问题 暴力背包,操作存在区间左移,右移,or bieset优化
- 最小割dp Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) E
http://codeforces.com/contest/724/problem/E 题目大意:有n个城市,每个城市有pi件商品,最多能出售si件商品,对于任意一队城市i,j,其中i<j,可以 ...
- CF724E Goods transportation 最小割 DP
照惯例CF的题不放原题链接... 题意:一个序列上有n个点,每个点有权值pi和si.表示这个点一开始有pi个物品,最多可以卖出si个物品,每个点都可以把物品向编号更大的点运输,但是对于i < j ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) E - Goods transportation 最大流转最小割转dp
E - Goods transportation 思路:这个最大流-> 最小割->dp好巧妙哦. #include<bits/stdc++.h> #define LL long ...
- Codeforces 724E Goods transportation(最小割转DP)
[题目链接] http://codeforces.com/problemset/problem/724/E [题目大意] 每个城市有pi的物品可以运出去卖,si个物品可以买, 编号小的城市可以往编号大 ...
- HDU1565 方格取数(1) —— 状压DP or 插头DP(轮廓线更新) or 二分图点带权最大独立集(最小割最大流)
题目链接:https://vjudge.net/problem/HDU-1565 方格取数(1) Time Limit: 10000/5000 MS (Java/Others) Memory L ...
- CodeForces E. Goods transportation【最大流+dp最小割】
妙啊 首先暴力建图跑最大流非常简单,s向每个i连流量为p[i]的边,每个i向t连流量为s[i]的边,每个i向j连流量为c的边(i<j),但是会又T又M 考虑最大流=最小割 然后dp求最小割,设f ...
- Wannafly挑战赛26-F-msc的棋盘[最小割转化dp]
题意 一个大小为 \(n*m\) 的棋盘,知道每一列放了多少棋子,求有多少摆放方案满足要求. \(n,m\leq 50\) . 分析 如果是求是否有方案的话可以考虑网络流,行列连边,列容量为 \(b_ ...
随机推荐
- 解决Cocos2d-x3.0、3.1 "_opendir$INODE64"symbol(s) not found错误
升级系统和XCode后.在IOS8上编译之前的项目会报例如以下错误: Undefined symbols for architecture x86_64: "_opendir$INODE64 ...
- 进程控制函数(2)-setpgid() 修改当前进程的进程组ID
定义:int setpgid(pid_t pid,pid_t pgid); 表头文件:#include<unistd.h> 说明:setpgid()将参数pid 指定进程所属的组识别码设为 ...
- 每日英语:China's Bad Earth
In Dapu, a rain-drenched rural outpost in the heart of China's grain basket, a farmer grows crops th ...
- 在windows中使用Navicat连接Linux虚拟机中的mysql数据库
今天想用navicat远程连接虚拟机中的MySQL数据库,一直连不上,在网上搜索了一下,发现原因是MySQL对远程用户登陆的授权问题.这里说一下我的解决方法.(本人小白) 首先,我用navicat去远 ...
- tableview 与 tableview cell
1.tableview cell: import Foundationimport UIKit class CjwtCell: UITableViewCell { @IBOutlet var lb_c ...
- 基于jquery垂直缩略图切换相册
今天给大家分享一款垂直缩略图切换jQuery相册,这是一款垂直缩略图左右滚动切换响应式jQuery图片相册代码.该 插件适用浏览器:IE8.360.FireFox.Chrome.Safari.Oper ...
- EF调用存储过程、函数
一.ef4.1 codeFirst 修改表结构 增加字段等 EF code first需要重新生成库导致数据丢失的问题 说这个问题前 首先先说下 我使用ef4.1 codefirst的目的. 是因为可 ...
- [初学WPF]控件大小自适应
想在Win上自己写点小工具用,GUI自然是免不了的,于是决定学一学WPF,直接拖控件是很方便啊.控件拖出来以后发现运行时改变窗口大小控件不会重绘,搜索了一下发现了解决办法:使用Viewbox控件. V ...
- 使用info命令查看Redis信息和状态
redis-cli连接服务器后,使用info命令查看Redis信息和状态: ? 1 info 其中memory段显示了redis的内存使用状态. 以下内容复制自:http://redisdoc.com ...
- C#调用SQL Server有参的存储过程
一.使用SqlParameter的方式 代码: using System; using System.Collections.Generic; using System.ComponentModel; ...