p1345 奶牛的电信Telecowmunication
题目
农夫约翰的奶牛们喜欢通过电邮保持联系,于是她们建立了一个奶牛电脑网络,以便互相交流。这些机器用如下的方式发送电邮:如果存在一个由c台电脑组成的序列a1,a2,...,a(c),且a1与a2相连,a2与a3相连,等等,那么电脑a1和a(c)就可以互发电邮。
很不幸,有时候奶牛会不小心踩到电脑上,农夫约翰的车也可能碾过电脑,这台倒霉的电脑就会坏掉。这意味着这台电脑不能再发送电邮了,于是与这台电脑相关的连接也就不可用了。
有两头奶牛就想:如果我们两个不能互发电邮,至少需要坏掉多少台电脑呢?请编写一个程序为她们计算这个最小值。
输入格式:
第一行
四个由空格分隔的整数:N,M,c1,c2.N是电脑总数(1<=N<=100),电脑由1到N编号。M是电脑之间连接的总数(1<=M<=600)。最后的两个整数c1和c2是上述两头奶牛使用的电脑编号。连接没有重复且均为双向的(即如果c1与c2相连,那么c2与c1也相连)。两台电脑之间至多有一条连接。电脑c1和c2不会直接相连。
第2到M+1行 接下来的M行中,每行包含两台直接相连的电脑的编号。
输出格式:
一个整数表示使电脑c1和c2不能互相通信需要坏掉的电脑数目的最小值。
分析
此题是一个最小割问题,但普通的最小割是割边而这道题是要个点,所以我们考虑将一个点拆成两点一边,然后进行朴素的求解最小割即可。注意为了方便期间我们把边从0开始存。
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
const int inf=1e9+;
int s,t,level[],cur[],cnt=-;
int head[],to[],nxt[],w[];
inline void add(int x,int y,int v){
to[++cnt]=y;
nxt[cnt]=head[x];
head[x]=cnt;
w[cnt]=v;
to[++cnt]=x;
nxt[cnt]=head[y];
head[y]=cnt;
w[cnt]=;
}
inline bool bfs(){
memset(level,-,sizeof(level));
queue<int>q;
level[s]=;
q.push(s);
while(!q.empty()){
int x=q.front();
q.pop();
for(int i=head[x];i;i=nxt[i]){
int y=to[i];
if(level[y]==-&&w[i]){
level[y]=level[x]+;
if(y==t)return ;
q.push(y);
}
}
}
return ;
}
inline int dfs(int x,int y,int f){
if(x==y)return f;
int res=;
if(!cur[x])cur[x]=head[x];
for(int i=cur[x];i;i=nxt[i]){
cur[x]=i;
int j=to[i];
if(level[j]==level[x]+&&w[i]){
int nf=dfs(j,y,min(f-res,w[i]));
res+=nf;
w[i]-=nf;
w[i^]+=nf;
}
}
if(!res)level[x]=-;
return res;
}
int main()
{ int n,m,i,j,k,x,y,ans=;
memset(head,-,sizeof(head));
scanf("%d%d%d%d",&n,&m,&s,&t);
for(i=;i<=n;i++)
add(i,i+n,);
for(i=;i<=m;i++){
scanf("%d%d",&x,&y);
add(x+n,y,inf);
add(y+n,x,inf);
}
s+=n;
while(bfs()){
memset(cur,,sizeof(cur));
while(int a=dfs(s,t,inf))
ans+=a;
}
printf("%d\n",ans);
return ;
}
p1345 奶牛的电信Telecowmunication的更多相关文章
- P1345 [USACO5.4]奶牛的电信Telecowmunication
P1345 [USACO5.4]奶牛的电信Telecowmunication 题目描述 农夫约翰的奶牛们喜欢通过电邮保持联系,于是她们建立了一个奶牛电脑网络,以便互相交流.这些机器用如下的方式发送电邮 ...
- 洛谷P1345 [USACO5.4]奶牛的电信Telecowmunication【最小割】分析+题解代码
洛谷P1345 [USACO5.4]奶牛的电信Telecowmunication[最小割]分析+题解代码 题目描述 农夫约翰的奶牛们喜欢通过电邮保持联系,于是她们建立了一个奶牛电脑网络,以便互相交流. ...
- 洛谷——P1345 [USACO5.4]奶牛的电信Telecowmunication
P1345 [USACO5.4]奶牛的电信Telecowmunication 题目描述 农夫约翰的奶牛们喜欢通过电邮保持联系,于是她们建立了一个奶牛电脑网络,以便互相交流.这些机器用如下的方式发送电邮 ...
- 题解 P1345 【[USACO5.4]奶牛的电信Telecowmunication】
P1345 [USACO5.4]奶牛的电信Telecowmunication 题目描述 农夫约翰的奶牛们喜欢通过电邮保持联系,于是她们建立了一个奶牛电脑网络,以便互相交流.这些机器用如下的方式发送电邮 ...
- [Luogu P1345] [USACO5.4]奶牛的电信Telecowmunication (最小割)
题面 传送门:https://www.luogu.org/problemnew/show/P1345 ] Solution 这道题,需要一个小技巧了解决. 我相信很多像我这样接蒟蒻,看到这道题,不禁兴 ...
- AC日记——[USACO5.4]奶牛的电信Telecowmunication 洛谷 P1345
[USACO5.4]奶牛的电信Telecowmunication 思路: 水题: 代码: #include <cstdio> #include <cstring> #inclu ...
- [USACO5.4]奶牛的电信Telecowmunication(网络流)
P1345 [USACO5.4]奶牛的电信Telecowmunication 题目描述 农夫约翰的奶牛们喜欢通过电邮保持联系,于是她们建立了一个奶牛电脑网络,以便互相交流.这些机器用如下的方式发送电邮 ...
- 洛谷P1345 [USACO5.4]奶牛的电信Telecowmunication
题目描述 农夫约翰的奶牛们喜欢通过电邮保持联系,于是她们建立了一个奶牛电脑网络,以便互相交流.这些机器用如下的方式发送电邮:如果存在一个由c台电脑组成的序列a1,a2,...,a(c),且a1与a2相 ...
- 洛谷P1345 [USACO5.4]奶牛的电信Telecowmunication(最小割)
题目描述 农夫约翰的奶牛们喜欢通过电邮保持联系,于是她们建立了一个奶牛电脑网络,以便互相交流.这些机器用如下的方式发送电邮:如果存在一个由c台电脑组成的序列a1,a2,...,a(c),且a1与a2相 ...
随机推荐
- Data Structure Binary Tree: Lowest Common Ancestor in a Binary Tree
http://www.geeksforgeeks.org/lowest-common-ancestor-binary-tree-set-1/ #include <iostream> #in ...
- P4773 红鲤鱼与绿鲤鱼
P4773 红鲤鱼与绿鲤鱼 暑假比赛的一个水题 总情况数:\(\dfrac{(a+b)!}{a!b!}\) 就是\(a+b\)条鲤鱼中选\(a\) or \(b\)的情况 反正我们会用完鲤鱼,则红鲤鱼 ...
- 算法(Algorithms)第4版 练习 1.5.14
package com.qiusongde; import edu.princeton.cs.algs4.StdIn; import edu.princeton.cs.algs4.StdOut; pu ...
- oarcle12c打开本地数据库
--显示当前数据库的链接db,是cdb还是pdb. show con_name;--在oracle12c中打开本地数据库,否则本地数据库无法链接alter pluggable database pdb ...
- 文件复制的另一种选择,使用FileChannel复制文件
通常来说,FileChannel比普通的缓冲输入输出流有更高的效率 import java.io.File; import java.io.FileInputStream; import ja ...
- linux应用之yum命令的软件源的更换(centos)
[1] 首先备份/etc/yum.repos.d/CentOS-Base.repo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/Cent ...
- L99
You're not obligated to win. You're obligated to keep trying.你不一定要获胜,但你必须不断尝试.He announced an expans ...
- leetcode 34 Search for a Range(二分法)
Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...
- CodeForces - 1C:Ancient Berland Circus (几何)
Nowadays all circuses in Berland have a round arena with diameter 13 meters, but in the past things ...
- bzoj 4501: 旅行 01分数规划+概率期望dp
题目大意: http://www.lydsy.com/JudgeOnline/problem.php?id=4501 题解: 首先我们不考虑可以删除边的情况下,如何计算期望边数. 然后我们发现这是个有 ...