HDU - 6214 Smallest Minimum Cut(最小割最少边数)
生活随笔
收集整理的這篇文章主要介紹了
HDU - 6214 Smallest Minimum Cut(最小割最少边数)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
題目鏈接:點擊查看
題目大意:給出一張由n個點以及m條邊組成的有向圖,現(xiàn)在要求出最少割掉幾條邊使得整張圖不連通并且割掉邊的權(quán)值最小
題目分析:題目的意思也就是要求最小割的最少邊數(shù),這里有兩個方法:
既然是個模板題知道怎么處理就好了,有點尷尬的是沒寫出來第一種方法的代碼,一直WA,雖然寫出了第二種方法的代碼,但是不太明白其中的原理
代碼:
#include<iostream> #include<cstdlib> #include<string> #include<cstring> #include<cstdio> #include<algorithm> #include<climits> #include<cmath> #include<cctype> #include<stack> #include<queue> #include<list> #include<vector> #include<set> #include<map> #include<sstream> using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=210;struct Edge {int to,w,next; }edge[N*N];//邊數(shù)int head[N],cnt;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(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; }int main() { // freopen("input.txt","r",stdin); // ios::sync_with_stdio(false);int w;cin>>w;while(w--){init();int n,m;scanf("%d%d",&n,&m);int st,ed;scanf("%d%d",&st,&ed);while(m--){int u,v,w;scanf("%d%d%d",&u,&v,&w);addedge(u,v,w*1000+1);}printf("%d\n",solve(st,ed)%1000);}return 0; }?
總結(jié)
以上是生活随笔為你收集整理的HDU - 6214 Smallest Minimum Cut(最小割最少边数)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CodeForces - 1270D S
- 下一篇: HDU - 3987 Harry Pot