POJ - 1087 A Plug for UNIX解题报告(网络流建图+map容器使用)
2017-05-13 21:42
330 查看
题目大意:
大概就是说,首先给你100个插座,这些插座有不同的类型,然后再给你100个用电器,告诉你每个用电器的对应插座类型,再之后,就告诉你100个转换器每个转换器告诉你它能把什么类型转换成什么类型转换器无限个。问你最少有多少个用电器没办法用。网络流建图:
一个超级源点连接所有类型的插座,容量为1,各种类型的转换器代表容量无限大的有向边,再创建一个超级汇点,每个类型的用电器所需的插座类型对应连到该汇点,容量为1。 求超级源点到超级汇点的最大流。ps:原谅我又用了有道翻译,说真的这个翻译真是智能整篇复制上去,然后翻译出来毫无违和感。
代码:
#include<iostream> #include<stdio.h> #include<math.h> #include<queue> #include<vector> #include<string.h> #include<map> using namespace std; #define maxn 1000 #define inf 1000000 struct Edge { int from,to,cap,flow; Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){} }; int n;int m; vector<Edge>edge; vector<int>G[maxn]; int a[maxn]; int p[maxn]; void init(int n) { for(int i=0;i<n;i++)G[i].clear(); edge.clear(); } void add_edge(int from,int to,int cap) { edge.push_back(Edge(from,to,cap,0)); edge.push_back(Edge(to,from,0,0)); int lm=edge.size(); G[from].push_back(lm-2); G[to].push_back(lm-1); } int maxflow(int s,int t) { int flow=0; while(1) { memset(a,0,sizeof(a)); queue<int>Q; Q.push(s); a[s]=inf; while(!Q.empty()) { int x=Q.front();Q.pop(); for(int i=0;i<G[x].size();i++) { Edge& e=edge[G[x][i]]; if(!a[e.to]&&e.cap>e.flow) { p[e.to]=G[x][i]; a[e.to]=min(a[x],e.cap-e.flow); Q.push(e.to); } } if(a[t])break; } if(!a[t])break; for(int u=t;u!=s;u=edge[p[u]].from) { edge[p[u]].flow+=a[t]; edge[p[u]^1].flow-=a[t]; } flow+=a[t]; } return flow; } char ls[30]={0}; char ls2[30]={0}; int l[maxn]={0}; map<string,int>xcx; int main() { int t; while(cin>>t) { init(maxn); xcx.clear(); memset(p,0,sizeof(p)); n=2; memset(l,0,sizeof(l)); for(int i=1;i<=t;i++) { scanf("%s",ls); if(xcx.count(ls)==0)//没出现过 { xcx[ls]=n; n++; } l[xcx[ls]]++; } for(int i=2;i<n;i++) { if(l[i]!=0) { add_edge(0,i,l[i]); } } cin>>t; m=t; memset(l,0,sizeof(l)); //xcx.clear(); //n=2; for(int i=1;i<=t;i++) { scanf("%s",ls2); scanf("%s",ls); if(xcx.count(ls)==0)//没出现过 { xcx[ls]=n; n++; } if(xcx.count(ls2)==0) { xcx[ls2]=n; n++; } add_edge(xcx[ls2],1,1); add_edge(xcx[ls],xcx[ls2],1); } /*for(int i=2;i<n;i++) { if(l[i]!=0) { add_edge(i,1,l[i]); } } */ cin>>t; for(int i=1;i<=t;i++) { scanf("%s",ls); scanf("%s",ls2); if(xcx.count(ls)==0) { xcx[ls]=n; n++; } if(xcx.count(ls2)==0) { xcx[ls2]=n; n++; } add_edge(xcx[ls2],xcx[ls],inf); } /*for(int i=1;i<n;i++) { for(int j=1;j<n;j++) { if(map[i][j]!=0) { add_edge(i,j,map[i][j]); } } }*/ //if(first==1)printf("\n"); printf("%d\n",m-maxflow(0,1)); //first=1; } }
相关文章推荐
- 解题报告 之 POJ1087 A Plug for UNIX
- pku 1087 A Plug for UNIX 网络流 解题报告
- POJ--1087--A Plug for UNIX【Dinic】网络最大流
- poj 1087 A Plug for UNIX(最大流)
- POJ 1087:A Plug for UNIX(最大流)
- poj 1087 A Plug for UNIX
- poj 1087 A Plug for UNIX
- POJ 1087 A Plug for UNIX
- A Plug for UNIX--POJ 1087
- poj 1087 a plug for unix
- POJ 1087 A Plug for UNIX
- POJ 1087 A Plug for UNIX【二分图最大匹配】
- Poj 1087 A Plug For UNIX
- zoj 1157 || poj 1087 A Plug for UNIX
- POJ1087 A Plug for UNIX 二分图最大匹配
- POJ 1087 A Plug for UNIX(网络流—最大流(最大二分匹配))
- ZOJ 1157 A Plug for UNIX (POJ 1087)
- poj-1087 A Plug for UNIX ***
- poj 1087 A Plug for UNIX
- POJ 1087 A Plug for UNIX(最大二分匹配)