Poj 1087 A Plug For UNIX

来源:互联网 发布:c语言实训总结 编辑:程序博客网 时间:2024/06/06 08:48
2Y
1WA于以为插销只是一个字符
2Y 0ms
Zoo 的隐藏boss读的题
自己建的模,还不错
虚拟总源点,总汇点
然后离散化所有的插座代号
有的插座作为汇点
有的电器作为源点
转换器的边流量为INF
以上
#include<stdio.h>#include<queue>#include<vector>#include<algorithm>#include<string.h>using namespace std;#define N 510#define INF (int)1e9int G[N][N];#define FOR(i,a,b) for(i=a;i<=b;i++)int ft[N];bool visit[N];int p,q,r,n;bool EK(int src,int des){queue<int>Q;while(!Q.empty())Q.pop();int i;FOR(i,0,n+1)visit[i]=0;Q.push(src);visit[src]=0;while(!Q.empty()){int tp=Q.front();Q.pop();FOR(i,0,n+1){if(!visit[i]&&G[tp][i]){ft[i]=tp;Q.push(i);visit[i]=1;if(i==des)return 1;}}}return 0;}int FF(int src,int des){int res=0;while(1){if(!EK(src,des))break;int run=INF,tp=des;while(tp!=src){run=min(run,G[ft[tp]][tp]);tp=ft[tp];}tp=des;while(tp!=src){G[ft[tp]][tp]-=run;G[tp][ft[tp]]+=run;tp=ft[tp];}res+=run;}return res;}struct pak{char s[99];bool operator <  (const pak &tp)const{return strcmp(s,tp.s)<0;}bool operator == (const pak &tp)const{return strcmp(s,tp.s)==0;}}tp;vector<pak>X,Y,Z1,Z2,L;vector<pak>::iterator iter;int main(){while(scanf("%d",&p)!=EOF){int i,j,k,x,y,w;X.clear();Y.clear();Z1.clear();Z2.clear();L.clear();FOR(i,1,p){scanf("%s",tp.s);X.push_back(tp);L.push_back(tp);}scanf("%d",&q);FOR(i,1,q){scanf("%s",tp.s);scanf("%s",tp.s);Y.push_back(tp);L.push_back(tp);}scanf("%d",&r);FOR(i,1,r){scanf("%s",tp.s);Z1.push_back(tp);L.push_back(tp);scanf("%s",tp.s);Z2.push_back(tp);L.push_back(tp);}sort(L.begin(),L.end());L.erase(unique(L.begin(),L.end()),L.end());n=L.size();FOR(i,0,n+1)FOR(j,0,n+1)G[i][j]=0;FOR(i,0,X.size()-1){iter=lower_bound(L.begin(),L.end(),X[i]);x=iter-L.begin()+1;G[x][n+1]+=1;}FOR(i,0,Y.size()-1){iter=lower_bound(L.begin(),L.end(),Y[i]);y=iter-L.begin()+1;G[0][y]+=1;}FOR(i,0,Z1.size()-1){iter=lower_bound(L.begin(),L.end(),Z1[i]);x=iter-L.begin()+1;iter=lower_bound(L.begin(),L.end(),Z2[i]);y=iter-L.begin()+1;G[x][y]=INF;}printf("%d\n",q-FF(0,n+1));}return 0;}