POJ - 1087 A Plug for UNIX(最大流)
生活随笔
收集整理的這篇文章主要介紹了
POJ - 1087 A Plug for UNIX(最大流)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:點擊查看
題目大意:給出n個互不相同的設備,m個插座以及k種適配器,每種適配器都有無限個,適配器可以互相搭配,問如何匹配可以讓盡可能多的設備用上電
題目分析:裸的最大流,就是加上了個字符串把操作復雜化以及有無用信息,比如對于每個設備而言,關鍵點是每個設備對應的插座類型,而不是設備名字,所以設備名字我們就并不需要儲存,這樣都轉換成了插座的類型就比較好想了,注意一定要保證思路清晰,不然寫亂了debug的會很頭疼,總的編號我們用一個cnt從1開始就好了,因為互不干擾,這樣直接建圖就好了:
按照上述規則建圖,然后跑最大流就好了,記得最后輸出的答案,是最小不能匹配的數量,也就是設備的個數減去最大流
代碼:
#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=310;struct Edge {int to,w,next; }edge[N*N];//邊數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;//反向邊邊權設置為0edge[cnt].next=head[v];head[v]=cnt++; }int d[N],now[N*N];//深度 當前弧優化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; }map<string,int>_equipment,_plug;map<pair<string,string>,int>_match;string to[110],plug[110],match1[110],match2[110];int main() { // freopen("input.txt","r",stdin); // ios::sync_with_stdio(false);int n,m,k;//插座、設備、適配器 while(scanf("%d",&n)!=EOF){init();_equipment.clear();_plug.clear();_match.clear();int cnt=0;for(int i=1;i<=n;i++)//讀入插座 {cin>>plug[i];if(!_plug[plug[i]])_plug[plug[i]]=++cnt;}scanf("%d",&m);for(int i=1;i<=m;i++)//讀入設備 {string temp;cin>>temp>>to[i];if(!_equipment[to[i]])_equipment[to[i]]=++cnt;}scanf("%d",&k);for(int i=1;i<=k;i++)//讀入適配器 {cin>>match1[i]>>match2[i];if(!_match[make_pair(match1[i],match2[i])])_match[make_pair(match1[i],match2[i])]=++cnt;}int st=N-1,ed=st-1;//1~num1 : 設備 num1+1~num1+num2 : 插座 num1+num2+1~最后 : 適配器 for(int i=1;i<=m;i++)//源點->設備 addedge(st,_equipment[to[i]],1);for(int i=1;i<=n;i++)//插座->匯點 addedge(_plug[plug[i]],ed,1);for(int i=1;i<=m;i++)//設備->插座 if(_plug[to[i]])addedge(_equipment[to[i]],_plug[to[i]],1);for(int i=1;i<=k;i++)//適配器 {for(int j=1;j<=n;j++)//適配器->插座if(match2[i]==plug[j])addedge(_match[make_pair(match1[i],match2[i])],_plug[plug[j]],inf);for(int j=1;j<=m;j++)//設備->適配器 if(match1[i]==to[j])addedge(_equipment[to[j]],_match[make_pair(match1[i],match2[i])],inf);for(int j=1;j<=k;j++)//適配器if(i!=j&&match2[i]==match1[j])addedge(_match[make_pair(match1[i],match2[i])],_match[make_pair(match1[j],match2[j])],inf);}printf("%d\n",m-solve(st,ed));}return 0; }?
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的POJ - 1087 A Plug for UNIX(最大流)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: POJ - 3436 ACM Compu
- 下一篇: POJ - 2516 Minimum C