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