USACO Section 5.4 TeleCowmunication(最小割)

时间:2022-09-26 17:18:11

USACO Section 5.4 TeleCowmunication(最小割)

挺裸的一道最小割。把每台电脑拆成一条容量为1的边,然后就跑最大流。从小到大枚举每台电脑,假如去掉后 最大流=之前最大流+1,那这台电脑就是answer之一了。

--------------------------------------------------------------------------------------

#include<cstdio>
#include<vector>
#include<cstring>
#define rep(i,r) for(int i=0;i<r;i++)
#define clr(x,c) memset(x,c,sizeof(x))
#define Rep(i,l,r) for(int i=l;i<r;i++)
using namespace std;
const int maxn=100*2+5;
const int inf=1<<30;
int x[maxn][2];
struct Edge {
int from,to,cap,flow;
Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f) {}
};
struct ISAP {
int n,m,s,t;
int p[maxn];
int cur[maxn];
int num[maxn];
int d[maxn];
vector<int> g[maxn];
vector<Edge> edges;
void init(int n) {
this->n=n;
rep(i,n) g[i].clear();
edges.clear();
}
int addEdge(int from,int to,int cap) {
edges.push_back( (Edge) {from,to,cap,0} );
edges.push_back( (Edge) {to,from,0,0} );
m=edges.size();
g[from].push_back(m-2);
g[to].push_back(m-1);
return m-2;
}
int augment() {
int x=t,a=inf;
while(x!=s) {
Edge &e=edges[p[x]];
a=min(a,e.cap-e.flow);
x=edges[p[x]].from;
}
x=t;
while(x!=s) {
edges[p[x]].flow+=a;
edges[p[x]^1].flow-=a;
x=edges[p[x]].from;
}
return a;
}
int maxFlow(int s,int t) {
int flow=0;
this->s=s; this->t=t;
clr(d,0); clr(num,0); clr(cur,0); clr(p,0);
rep(i,edges.size()) edges[i].flow=0;
rep(i,n) num[d[i]]++;
int x=s;
while(d[s]<n) {
if(x==t) { flow+=augment(); x=s; }
int ok=0;
Rep(i,cur[x],g[x].size()) {
Edge &e=edges[g[x][i]];
if(e.cap>e.flow && d[x]==d[e.to]+1) {
ok=1;
p[e.to]=g[x][i];
cur[x]=i;
x=e.to;
break;
}
}
if(!ok) {
int m=n-1;
rep(i,g[x].size()) {
Edge &e=edges[g[x][i]];
if(e.cap>e.flow) m=min(m,d[e.to]);
}
if(--num[d[x]]==0) break;
num[d[x]=m+1]++;
cur[x]=0;
if(x!=s) x=edges[p[x]].from;
}
}
return flow;
}
} isap;
int main()
{
freopen("telecow.in","r",stdin);
freopen("telecow.out","w",stdout);
clr(x,-1);
int n,m,s,t,a,b;
scanf("%d%d%d%d",&n,&m,&s,&t);
--s; --t;
isap.init(2*n);
rep(i,n) {
   x[i][0]=isap.addEdge(i,i+n,1);
   x[i][1]=isap.addEdge(i+n,i,1);
}
rep(i,m) {
scanf("%d%d",&a,&b);
--a; --b;
isap.addEdge(a+n,b,m+1);
isap.addEdge(b+n,a,m+1);
}
int maxFlow=isap.maxFlow(s+n,t);
vector<int> ans; ans.clear();
rep(i,n) if(i!=s && i!=t) {
isap.edges[x[i][0]].cap=0;
isap.edges[x[i][1]].cap=0;
   if(isap.maxFlow(s+n,t)==maxFlow-1) {
ans.push_back(i+1);
maxFlow--;
} else {
   isap.edges[x[i][0]].cap=1;
   isap.edges[x[i][1]].cap=1;
}
if(!maxFlow) break;
}
printf("%d\n",ans.size());
printf("%d",ans[0]);
Rep(i,1,ans.size()) printf(" %d",ans[i]);
printf("\n");
return 0;
}

--------------------------------------------------------------------------------------

Telecowmunication

Farmer John's cows like to keep in touch via email so they have created a network of cowputers so that they can intercowmunicate. These machines route email so that if there exists a sequence of c cowputers a1, a2, ..., a(c) such that a1 is connected to a2, a2 is connected to a3, and so on then a1 and a(c) can send email to one another.

Unfortunately, a cow will occasionally step on a cowputer or Farmer John will drive over it, and the machine will stop working. This means that the cowputer can no longer route email, so connections to and from that cowputer are no longer usable.

Two cows are pondering the minimum number of these accidents that can occur before they can no longer use their two favorite cowputers to send email to each other. Write a program to calculate this minimal value for them, and to calculate a set of machines that corresponds to this minimum.

For example the network:

               1*               /                3 - 2* 

shows 3 cowputers connected with 2 lines. We want to send messages between 1 with 2. Direct lines connect 1-3 and 2-3. If cowputer 3 is down, them there is no way to get a message from 1 to 2.

PROGRAM NAME: telecow

INPUT FORMAT

Line 1 Four space-separated integers: N, M, c1, and c2. N is the number of computers (1 <= N <= 100), which are numbered 1..N. M is the number of connections between pairs of cowputers (1 <= M <= 600). The last two numbers, c1 and c2, are the id numbers of the cowputers that the questioning cows are using. Each connection is unique and bidirectional (if c1 is connected to c2, then c2 is connected to c1). There can be at most one wire between any two given cowputers. Computer c1 and c2 will not have a direction connection.
Lines 2..M+1 The subsequent M lines contain pairs of cowputers id numbers that have connections between them.

SAMPLE INPUT (file telecow.in)

3 2 1 2 1 3 2 3 

OUTPUT FORMAT

Generate two lines of output. The first line is the minimum number of cowputers that can be down before terminals c1 & c2 are no longer connected. The second line is a minimal-length sorted list of cowputers that will cause c1 & c2 to no longer be connected. Note that neither c1 nor c2 can go down. In case of ties, the program should output the set of computers that, if interpreted as a base N number, is the smallest one.

SAMPLE OUTPUT (file telecow.out)

1 3

USACO Section 5.4 TeleCowmunication(最小割)的更多相关文章

  1. LG1345 「USACO5&period;4」Telecowmunication 最小割

    问题描述 LG1345 题解 点边转化,最小割,完事. \(\mathrm{Code}\) #include<bits/stdc++.h> using namespace std; tem ...

  2. &lbrack;USACO5&period;4&rsqb;奶牛的电信Telecowmunication 最小割

    题目描述 农夫约翰的奶牛们喜欢通过电邮保持联系,于是她们建立了一个奶牛电脑网络,以便互相交流.这些机器用如下的方式发送电邮:如果存在一个由c台电脑组成的序列a1,a2,...,a(c),且a1与a2相 ...

  3. &lbrack;Luogu P1345&rsqb; &lbrack;USACO5&period;4&rsqb;奶牛的电信Telecowmunication &lpar;最小割&rpar;

    题面 传送门:https://www.luogu.org/problemnew/show/P1345 ] Solution 这道题,需要一个小技巧了解决. 我相信很多像我这样接蒟蒻,看到这道题,不禁兴 ...

  4. &lbrack;USACO Section 4&period;4&rsqb;追查坏牛奶Pollutant Control &lpar;最小割&rpar;

    题目链接 Solution 一眼看过去就是最小割,但是要求割边最少的最小的割. 所以要用骚操作... 建边的时候每条边权 \(w = w * (E+1) + 1;\) 那么这样建图跑出来的 \(max ...

  5. 洛谷P1345 &lbrack;USACO5&period;4&rsqb;奶牛的电信Telecowmunication【最小割】分析&plus;题解代码

    洛谷P1345 [USACO5.4]奶牛的电信Telecowmunication[最小割]分析+题解代码 题目描述 农夫约翰的奶牛们喜欢通过电邮保持联系,于是她们建立了一个奶牛电脑网络,以便互相交流. ...

  6. USACO 4&period;4&period;2 追查坏牛奶 oj1341 网络流最小割问题

    描述 Description 你第一天接手三鹿牛奶公司就发生了一件倒霉的事情:公司不小心发送了一批有三聚氰胺的牛奶.很不幸,你发现这件事的时候,有三聚氰胺的牛奶已经进入了送货网.这个送货网很大,而且关 ...

  7. USACO 4&period;4 Pollutant Control (网络流求最小割割集)

    Pollutant ControlHal Burch It's your first day in Quality Control at Merry Milk Makers, and already ...

  8. 洛谷P1345 &lbrack;USACO5&period;4&rsqb;奶牛的电信Telecowmunication(最小割)

    题目描述 农夫约翰的奶牛们喜欢通过电邮保持联系,于是她们建立了一个奶牛电脑网络,以便互相交流.这些机器用如下的方式发送电邮:如果存在一个由c台电脑组成的序列a1,a2,...,a(c),且a1与a2相 ...

  9. P1345 &lbrack;USACO5&period;4&rsqb;奶牛的电信Telecowmunication【最小割】【最大流】

    题目描述 农夫约翰的奶牛们喜欢通过电邮保持联系,于是她们建立了一个奶牛电脑网络,以便互相交流.这些机器用如下的方式发送电邮:如果存在一个由c台电脑组成的序列a1,a2,...,a(c),且a1与a2相 ...

随机推荐

  1. 小身材大用途,用PrimusUI驾驭你的页面

    “PrimusUI”是自己在借鉴了如今网上很多开源的UI库,再经过自己整理加工的一个简单代码集合. 每个功能块的CSS代码都很少,力求简单易懂,低门槛,代码可根据自己实际情况轻易修改,改到符合自己场景 ...

  2. Inversions

    There are N integers (1<=N<=65537) A1, A2,.. AN (0<=Ai<=10^9). You need to find amount o ...

  3. 腾讯AlloyTeam正式发布Canvas魔幻线条 - curvejs

    [原文链接] ## 写在前面 curvejs 中文读["克js"],是腾讯AlloyTeam打造的一款魔幻线条框架,让线条成为一名优秀的舞者,让线条们成为优秀的舞团,HTML5 ...

  4. 不支持placeholder浏览器下对placeholder进行处理

    if(document.createElement('input').placeholder !== '') { $('[placeholder]').focus(function() { var i ...

  5. &lbrack;Gradle&rsqb; 在 Eclipse 下利用 gradle 构建系统

      转载自:http://www.ibm.com/developerworks/cn/opensource/os-cn-gradle/ 构建系统时候常常要用到 Ant, Maven 等工具,对于初学者 ...

  6. Python——Flask框架——电子邮件

    一.框架(Flask-Mail) 安装 : pip install flask-mail 二.SMTP服务器的配置 配置 默认值 说明 MAIL_SERVER locallhost 电子邮件服务器的主 ...

  7. C pointer again …

    记录一个比较基础的东东…… C 语言的指针,一直让人又爱又恨,爱它的人觉得它既灵活又强大,恨它的人觉得它太过于灵活太过于强大以至于容易将人绕晕.最早接触 C 语言,还是在刚进入大学的时候,算起来有好些 ...

  8. codeforces471B

    MUH and Important Things CodeForces - 471B It's time polar bears Menshykov and Uslada from the zoo o ...

  9. python基础之正则表达式 re模块

    内容梗概: 1. 正则表达式 2. re模块的使⽤ 3. 一堆练习正则表达式是对字符串串操作的一种逻辑公式. 我们一般使用正则表达式对字符串进行匹配和过滤.使用正则的优缺点: 优点: 灵活,功能性强, ...

  10. 【收集】Python 微优化

    1. 第二种方式可以节省寻找result的append属性的时间, 但会降低代码可读性和可维护性 # The way we're used to seeing it: result.append(&q ...