POJ–3921[Destroying the bus stations] 最小費用流+拆點

來源:互聯網
上載者:User

題目大意:
一個有向圖有N(N<=50)個點,M(M<=4000)條邊。現在要求刪掉最少的點,使得不存在從1號點到N號點的長度<=K(K<1000)的路徑。當然,不能直接刪掉1號點或N號點。輸出最少需要刪掉的點數(刪掉一個點的時候會將與之相連的邊也刪掉)。

PS.不存在一條邊直接連1-N。

 

思路:
最小費用流+拆點

 

構圖:
(1):把[2..N-1]個點拆成i->i+N連一條容量為1費用為0的邊<1,0>;//保證每個點被破壞一次
(2):把1和N拆點,連一條<INF,0>;//保證不會破壞
(3):如果存在一條邊(u->v)怎連一條u+N->v容量為INF費用為1的邊<INF,1>;//確定每條邊的費用
(4):最後從1點到2*N點不停地找最短路增廣,知道最短路>K為止

PS.也可以用深搜+枚舉刪點的方案:

CODE:

 

/*最小費用流+拆點*//*AC代碼:32ms*/#include <iostream>#include <cstdio>#include <memory.h>#include <cstdio>#include <cstdlib>#include <queue>#define MAXN 200#define INF 1e8#define min(a,b) (a<b?a:b)using namespace std;struct edge{int u,v,w,c,next; }E[20000];int head[MAXN],ecnt;int N,M,K,scr,sink,vn;bool vis[MAXN];int dis[MAXN],pre[MAXN];void Insert(int u,int v,int w,int c){E[ecnt].u=u;E[ecnt].v=v;E[ecnt].w=w;E[ecnt].c=c;E[ecnt].next=head[u];head[u]=ecnt++;E[ecnt].u=v;E[ecnt].v=u;E[ecnt].w=0;E[ecnt].c=-c;E[ecnt].next=head[v];head[v]=ecnt++;}void Init(){int i,u,v;memset(head,-1,sizeof(head));ecnt=0;scr=1;sink=2*N;vn=sink;for(i=2;i<=N-1;i++)Insert(i,i+N,1,0);Insert(1,1+N,INF,0);Insert(N,N+N,INF,0);for(i=1;i<=M;i++){scanf("%d%d",&u,&v);Insert(u+N,v,INF,1);}}queue<int>Q;bool SPFA(int s,int t,int n){int i,u,v;int c;while(!Q.empty()) Q.pop();memset(vis,false,sizeof(vis));for(i=0;i<=n;i++)//找最長路dis[i]=INF;Q.push(s);pre[s]=-1;dis[s]=0;vis[s]=true;while(!Q.empty()){u=Q.front();Q.pop();vis[u]=false;for(i=head[u];i!=-1;i=E[i].next){v=E[i].v;c=E[i].c;if(E[i].w>0&&dis[v]>dis[u]+c){dis[v]=dis[u]+c;pre[v]=i;if(!vis[v]){vis[v]=true;Q.push(v);}}}}if(dis[t]!=INF) return true;return false;}void Solve(){int i,ans=0;while(SPFA(scr,sink,vn)){if(dis[sink]>K) break;ans++;for(i=pre[sink];i!=-1;i=pre[E[i].u])//更新{E[i].w-=1;E[i^1].w+=1; } }printf("%d\n",ans);}int main(){while(scanf("%d%d%d",&N,&M,&K)!=EOF){if(N==0&&M==0&&K==0) break;Init();Solve();}return 0;}

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.