題目連接:點(diǎn)擊查看
 
題目大意:給出一個(gè) p?個(gè)點(diǎn) c?條邊的無(wú)向圖,設(shè)置點(diǎn) 1 為基地,現(xiàn)在有 n 個(gè)點(diǎn)表示自己沒(méi)有被摧毀,但無(wú)法與基地相連,現(xiàn)在問(wèn)最少摧毀多少個(gè)點(diǎn)可以使得滿足條件
 
題目分析:最小割,割的是點(diǎn),所以考慮拆點(diǎn)限流:
 
源點(diǎn) -> 點(diǎn) 1,流量為 inf不能被摧毀的點(diǎn):入點(diǎn) -> 出點(diǎn),流量為 inf可以被摧毀的點(diǎn):入點(diǎn) -> 出點(diǎn),流量為 1n 個(gè)點(diǎn)的出點(diǎn) -> 匯點(diǎn),流量為 inf
跑最大流最小割就是答案了
 
需要注意的就是,p 是圖中節(jié)點(diǎn)的個(gè)數(shù)吧,寫成了 n 調(diào)了有一會(huì)。。
 
代碼:
 ?
 
//#pragma GCC optimize(2)
//#pragma GCC optimize("Ofast","inline","-ffast-math")
//#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=1e6+100;struct Edge
{int to,w,next;
}edge[N];//邊數(shù)int head[N],cnt,p,c,n,id1[3100],id2[3100],vis[3100];char s[N];void addedge(int u,int v,int w)
{edge[cnt].to=v;edge[cnt].w=w;edge[cnt].next=head[u];head[u]=cnt++;edge[cnt].to=u;edge[cnt].w=0;//反向邊邊權(quán)設(shè)置為0edge[cnt].next=head[v];head[v]=cnt++;
}int d[N],now[N];//深度 當(dāng)前弧優(yōu)化bool bfs(int s,int t)//尋找增廣路
{memset(d,0,sizeof(d));queue<int>q;q.push(s);now[s]=head[s];d[s]=1;while(!q.empty()){int u=q.front();q.pop();for(int i=head[u];i!=-1;i=edge[i].next){int v=edge[i].to;int w=edge[i].w;if(d[v])continue;if(!w)continue;d[v]=d[u]+1;now[v]=head[v];q.push(v);if(v==t)return true;}}return false;
}int dinic(int x,int t,int flow)//更新答案
{if(x==t)return flow;int rest=flow,i;for(i=now[x];i!=-1&&rest;i=edge[i].next){int v=edge[i].to;int w=edge[i].w;if(w&&d[v]==d[x]+1){int k=dinic(v,t,min(rest,w));if(!k)d[v]=0;edge[i].w-=k;edge[i^1].w+=k;rest-=k;}}now[x]=i;return flow-rest;
}void init()
{memset(now,0,sizeof(now));memset(head,-1,sizeof(head));cnt=0;
}int solve(int st,int ed)
{int ans=0,flow;while(bfs(st,ed))while(flow=dinic(st,ed,inf))ans+=flow;return ans;
}void get_id()
{int id=0;for(int i=1;i<=p;i++){id1[i]=++id;id2[i]=++id;}
}int main()
{
#ifndef ONLINE_JUDGE
//  freopen("data.in.txt","r",stdin);
//  freopen("data.out.txt","w",stdout);
#endif
//  ios::sync_with_stdio(false);init();int st=N-1,ed=st-1;scanf("%d%d%d",&p,&c,&n);get_id();while(c--){int x,y;scanf("%d%d",&x,&y);addedge(id2[x],id1[y],inf);addedge(id2[y],id1[x],inf);}while(n--){int x;scanf("%d",&x);vis[x]=true;//不能割的點(diǎn)addedge(id2[x],ed,inf); }vis[1]=true;for(int i=1;i<=p;i++){if(vis[i])addedge(id1[i],id2[i],inf);elseaddedge(id1[i],id2[i],1);}addedge(st,id1[1],inf);printf("%d\n",solve(st,ed));return 0;
} 
?
超強(qiáng)干貨來(lái)襲 云風(fēng)專訪:近40年碼齡,通宵達(dá)旦的技術(shù)人生
                            
總結(jié)
                            
                                以上是生活随笔為你收集整理的洛谷 - P2944 [USACO09MAR]Earthquake Damage 2 G(最小割)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
                            
                            
                                如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。