[USACO5.4]奶牛的电信Telecowmunication

题目描述

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

很不幸,有时候奶牛会不小心踩到电脑上,农夫约翰的车也可能碾过电脑,这台倒霉的电脑就会坏掉。这意味着这台电脑不能再发送电邮了,于是与这台电脑相关的连接也就不可用了。

有两头奶牛就想:如果我们两个不能互发电邮,至少需要坏掉多少台电脑呢?请编写一个程序为她们计算这个最小值。

以如下网络为例:

1*

/ 3 - 2*

这张图画的是有2条连接的3台电脑。我们想要在电脑1和2之间传送信息。电脑1与3、2与3直接连通。如果电脑3坏了,电脑1与2便不能互发信息了。

输入输出格式

输入格式:

第一行 四个由空格分隔的整数: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不能互相通信需要坏掉的电脑数目的最小值。

输入输出样例

输入样例#1:
3 2 1 2
1 3
2 3
输出样例#1:
1
思路:构图+最小割
 1 #include<cstdio>
 2 #include<cstring>
 3 const int inf=1e8;
 4 const int maxn=110;
 5 const int maxm=2000;
 6 int n,m,s,t,tw;
 7 int a,b,c;
 8 inline int min_(int x,int y){return x<y?x:y;}
 9 int h[maxn],hs=1;
10 struct edge{int s,n,w;}e[maxm];
11 void add(int q,int z){
12     e[++hs]=(edge){b,h[a],1},h[a]=hs;
13     e[++hs]=(edge){a,h[b],1},h[b]=hs;
14 }
15 int d[maxn],q[maxn],head,tail;
16 void bfs(){
17     memset(d,0,sizeof(d));
18     head=tail=0;
19     q[head++]=s,d[s]=1;
20     while(head>tail){
21         a=q[tail++];
22         for(int i=h[a];i;i=e[i].n)
23         if(!d[e[i].s]&&e[i].w){
24             d[e[i].s]=d[a]+1;
25             if(e[i].s==t) return;
26             q[head++]=e[i].s;
27         }
28     }
29 }
30 int ap(int k,int w){
31     if(k==t) return w;
32     int uw=w;
33     for(int i=h[k];i&&uw;i=e[i].n)
34     if(d[e[i].s]==d[k]+1&&e[i].w){
35         int nw=ap(e[i].s,min_(uw,e[i].w));
36         if(nw) e[i].w-=nw,e[i^1].w+=nw,uw-=nw;
37         else d[e[i].s]=0;
38     }
39     return w-uw;
40 }
41 void Dinic(){while(bfs(),d[t]) tw+=ap(s,inf);}
42 int main(){
43     scanf("%d%d%d%d",&n,&m,&s,&t);
44     for(int i=1;i<=m;i++){
45         scanf("%d%d",&a,&b);
46         add(a,b);
47     }
48     Dinic();
49     printf("%d
",tw);
50     return 0;
51 }
建图又错了(80分)。

每台电脑只需要破坏一次(啊,思维混乱了,看懂就行)。

因此,每台电脑要拆点(使得点对图的连通性产生影响)。
代码实现:
 1 #include<cstdio>
 2 #include<cstring>
 3 const int inf=1e8;
 4 const int maxn=300;
 5 const int maxm=20000;
 6 int n,m,s,t,tw;
 7 int a,b,c;
 8 inline int min_(int x,int y){return x<y?x:y;}
 9 int h[maxn],hs=1;
10 struct edge{int s,n,w;}e[maxm];
11 void add(int q,int z,int w){
12     e[++hs]=(edge){z,h[q],w},h[q]=hs;
13     e[++hs]=(edge){q,h[z]},h[z]=hs;
14 }
15 int d[maxn],q[maxn],head,tail;
16 void bfs(){
17     memset(d,0,sizeof(d));
18     head=tail=0;
19     q[head++]=s,d[s]=1;
20     while(head>tail){
21         a=q[tail++];
22         for(int i=h[a];i;i=e[i].n)
23         if(!d[e[i].s]&&e[i].w){
24             d[e[i].s]=d[a]+1;
25             if(e[i].s==t) return;
26             q[head++]=e[i].s;
27         }
28     }
29 }
30 int ap(int k,int w){
31     if(k==t) return w;
32     int uw=w;
33     for(int i=h[k];i&&uw;i=e[i].n)
34     if(d[e[i].s]==d[k]+1&&e[i].w){
35         int nw=ap(e[i].s,min_(uw,e[i].w));
36         if(nw) e[i].w-=nw,e[i^1].w+=nw,uw-=nw;
37         else d[e[i].s]=0;
38     }
39     return w-uw;
40 }
41 void Dinic(){while(bfs(),d[t]) tw+=ap(s,inf);}
42 int main(){
43     scanf("%d%d%d%d",&n,&m,&s,&t);
44     s+=n;
45     for(int i=1;i<=n;i++) add(i,i+n,1);
46     for(int i=1;i<=m;i++){
47         scanf("%d%d",&a,&b);
48         add(a+n,b,inf),add(b+n,a,inf);
49     }
50     Dinic();
51     printf("%d
",tw);
52     return 0;
53 }

题目来源:洛谷

原文地址:https://www.cnblogs.com/J-william/p/6602325.html