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
时间: 2024-10-15 03:51:12

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

USACO 4.4.2 追查坏牛奶 oj1341 网络流最小割问题

描述 Description 你第一天接手三鹿牛奶公司就发生了一件倒霉的事情:公司不小心发送了一批有三聚氰胺的牛奶.很不幸,你发现这件事的时候,有三聚氰胺的牛奶已经进入了送货网.这个送货网很大,而且关系复杂.你知道这批牛奶要发给哪个零售商,但是要把这批牛奶送到他手中有许多种途径.送货网由一些仓库和运输卡车组成,每辆卡车都在各自固定的两个仓库之间单向运输牛奶.在追查这些有三聚氰胺的牛奶的时候,有必要保证它不被送到零售商手里,所以必须使某些运输卡车停止运输,但是停止每辆卡车都会有一定的经济损失.你的

USACO 4.4 Pollutant Control (网络流求最小割割集)

Pollutant ControlHal Burch It's your first day in Quality Control at Merry Milk Makers, and already there's been a catastrophe: a shipment of bad milk has been sent out. Unfortunately, you didn't discover this until the milk was already into your del

USACO 5.4.3 点的最小割

这道题的难点在于将点拆分, 无向图变为有向图, 对于 i点我们可以将这个点拆分成2*i-1 -> 2*i,权值为1 对于i - j, 我们可以将点拆分以后再添加2*j -> 2*i-1 2*i -> 2*j-1,权值为inf,  然后求解最大流即为要去掉的顶点的个数, 求解具体的边的时候我们可以枚举要删除的边, 假设删除前的最大流为f,边权为1, 删除边后的最大流为ff, 若ff+1 == f的话那么这条边就在最小割集中.然后更新最大流,删掉最这条边重复上述操作即可, 代码如下: /*

【jzyzoj】【p1320 patrol】 巡逻(网络流最小割例题)

描述 Description FJ有个农场,其中有n块土地,由m条边连起来.FJ的养牛场在土地1,在土地n有个新开张的雪糕店.Bessie经常偷偷溜到雪糕店,当Bessie去的时候,FJ就要跟上她.但是Bessie很聪明,她在从雪糕店返回时不会经过去雪糕店时经过的农场,因此FJ总是抓不住Bessie.为了防止Bessie生病,FJ决定把一些诚实的狗放在一些土地(1和n除外)上,使Bessie无法在满足每块土地最多只经过一次的条件的情况下,从养牛场溜到雪糕店然后又溜回养牛场.求出FJ最少要放多少只

uva 1212 Duopoly (最小割最大流)

uva 1212 Duopoly Description The mobile network market in country XYZ used to be dominated by two large corporations, XYZ Telecom and XYZ Mobile. The central government recently has realized that radio frequency spectrum is a scarce resource and want

【UVALive - 3487】 Duopoly(网络流-最小割)

Description The mobile network market in country XYZ used to be dominated by two large corporations, XYZTelecom and XYZ Mobile. The central government recently has realized that radio frequency spectrumis a scarce resource and wants to regulate its u

HDU 3917 Road constructions (最小割---最大权闭包)经典

Road constructions Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1475    Accepted Submission(s): 489 Problem Description N cities are required to connect with each other by a new transportati

【BZOJ2039】【2009国家集训队】人员雇佣 [最小割]

人员雇佣 Time Limit: 20 Sec  Memory Limit: 259 MB[Submit][Status][Discuss] Description 作为一个富有经营头脑的富翁,小L决定从本国最优秀的经理中雇佣一些来经营自己的公司.这些经理相互之间合作有一个贡献指数,(我们用Ei,j表示i经理对j经理的了解程度),即当经理i和经理j同时被雇佣时,经理i会对经理j做出贡献,使得所赚得的利润增加Ei,j.当然,雇佣每一个经理都需要花费一定的金钱Ai,对于一些经理可能他做出的贡献不值得

hdoj 4289 Control 【拆点 求最小割】

Control Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2295    Accepted Submission(s): 961 Problem Description You, the head of Department of Security, recently received a top-secret informati