题意:给定n个插座,m个插头,k个转换器(x,y),转换器可以让插头x转成插头y。问最少有多少个插头被剩下。

题解:

最大流或者二分图匹配。然而我不知道怎么打二分图匹配。。
打了最大流。这题字符串比较坑爹,我就先把所有字符串编号(去重),然后给每个点编两个号,一个代表它作为插头的编号,一个代表它作为插座的编号。
最坑的是uvaWA了好久最后发现结尾要有一个回车。。。。。

还有数据范围也是个坑点,点起码要开500。

代码如下:(poj上是单组数据,uva和hdu都是多组)

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<queue>
using namespace std; const int N=,L=,INF=(int)1e9;
char s[N][L],c1[L],c2[L];
int ss[N],sc[N],lc[N],rc[N],dis[N],first[N],len,n,m,k,l,num;
struct node{
int x,y,d,next;
}a[];
queue<int> q; int minn(int x,int y){return x<y ? x:y;} void ins(int x,int y,int d)
{
a[++len].x=x;a[len].y=y;a[len].d=d;
a[len].next=first[x];first[x]=len;
a[++len].y=x;a[len].x=y;a[len].d=;
a[len].next=first[y];first[y]=len;
} bool bfs(int st,int ed)
{
while(!q.empty()) q.pop();
memset(dis,-,sizeof(dis));
q.push(st);dis[st]=;
while(!q.empty())
{
int x=q.front();q.pop();
for(int i=first[x];i!=-;i=a[i].next)
{
int y=a[i].y;
if(dis[y]==- && a[i].d)
{
dis[y]=dis[x]+;
q.push(y);
}
}
}
return (dis[ed]!=-);
} int dfs(int x,int ed,int flow)
{
if(x==ed) return flow;
int r=;
for(int i=first[x];i!=-;i=a[i].next)
{
int y=a[i].y;
if(dis[y]==dis[x]+ && a[i].d)
{
int p=minn(flow-r,a[i].d);
p=dfs(y,ed,p);
r+=p;
a[i].d-=p;
a[i^].d+=p;
}
}
if(r==) dis[x]=-;
return r;
} int dinic(int st,int ed)
{
int ans=;
while(bfs(st,ed))
ans+=dfs(st,ed,INF);
return ans;
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int st,ed;
scanf("%d",&n);
l=;len=-;
memset(first,-,sizeof(first));
memset(sc,,sizeof(sc));
memset(ss,,sizeof(ss));
for(int i=;i<=n;i++)
{
scanf("%s",s[++l]);
bool bk=;
for(int j=;j<l;j++)
{
if(strcmp(s[l],s[j])==)
{
l--,ss[j]++;
bk=;break;
}
}
if(!bk) ss[l]++;
}
scanf("%d",&m);
for(int i=;i<=m;i++)
{
scanf("%s",s[++l]);
scanf("%s",s[l]);
bool bk=;
for(int j=;j<l;j++)
{
if(strcmp(s[j],s[l])==)
{
l--,sc[j]++;
bk=;break;
}
}
if(!bk) sc[l]++;
} st=;num=;
for(int i=;i<=l;i++)
{
rc[i]=++num;
lc[i]=++num;
}
scanf("%d",&k);
for(int i=;i<=k;i++)
{
scanf("%s%s",c1,c2);
int t1=,t2=;
for(int j=;j<=l;j++)
{
if(strcmp(c1,s[j])==) t1=j;
if(strcmp(c2,s[j])==) t2=j;
if(t1 && t2) break;
}
if(!t1) t1=++l,strcpy(s[l],c1),lc[l]=++num,rc[l]=++num;
if(!t2) t2=++l,strcpy(s[l],c2),lc[l]=++num,rc[l]=++num;
ins(lc[t1],lc[t2],INF);
}
ed=num+;
for(int i=;i<=l;i++)
{
if(ss[i]) ins(rc[i],ed,ss[i]);
if(sc[i]) ins(st,lc[i],sc[i]);
ins(lc[i],rc[i],INF);
}
printf("%d",m-dinic(st,ed));
if(T) printf("\n\n");
else printf("\n");
}
return ;
}

【uva753/poj1087/hdu1526-A Plug for UNIX】最大流的更多相关文章

  1. 【poj1087/uva753】A Plug for UNIX(最大流)

    A Plug for UNIX   Description You are in charge of setting up the press room for the inaugural meeti ...

  2. POJ1087:A Plug for UNIX(最大流)

    A Plug for UNIX 题目链接:https://vjudge.net/problem/POJ-1087 Description: You are in charge of setting u ...

  3. POJ1087 A Plug for UNIX —— 最大流

    题目链接:https://vjudge.net/problem/POJ-1087 A Plug for UNIX Time Limit: 1000MS   Memory Limit: 65536K T ...

  4. ZOJ1157, POJ1087,UVA 753 A Plug for UNIX (最大流)

    链接 : http://acm.hust.edu.cn/vjudge/problem/viewProblem.action? id=26746 题目意思有点儿难描写叙述 用一个别人描写叙述好的. 我的 ...

  5. TZOJ 1911 A Plug for UNIX(最大流)

    描述 You are in charge of setting up the press room for the inaugural meeting of the United Nations In ...

  6. POJ A Plug for UNIX (最大流 建图)

    Description You are in charge of setting up the press room for the inaugural meeting of the United N ...

  7. UVa 753 A Plug for UNIX (最大流)

    题意:给定 n 种插座,m种设备,和k个转换器,问你最少有几台设备不能匹配. 析:一个很裸的网络流,直接上模板就行,建立一个源点s和汇点t,源点和每个设备连一条边,每个插座和汇点连一条边,然后再连转换 ...

  8. hdu 1087 A Plug for UNIX 最大流

    题意:http://www.phpfans.net/article/htmls/201012/MzI1MDQw.html 1.在一个会议室里有n种插座,每种插座一个: 2.每个插座只能插一种以及一个电 ...

  9. uva753 A Plug for UNIX 网络流最大流

    C - A Plug for UNIX    You are in charge of setting up the press room for the inaugural meeting of t ...

  10. POJ1087 A Plug for UNIX(网络流)

                                       A Plug for UNIX Time Limit: 1000MS   Memory Limit: 65536K Total S ...

随机推荐

  1. 【EF Code First】CodeFirst初始配置

    1,在Nuget管理中下载EntityFramework 2,配置文件中添加数据库配置 <connectionStrings> <add name="DefaultConn ...

  2. How to write a windows service

    how to write a windows services susport microsoft This aritcle describe the detail step to setup a w ...

  3. 如何破解UltraEdit

    在断网的前提下,软件->帮助->注册->激活->脱机激活—>用户和密码随便输入->还有两个空着,就是该用注册机激活了. 打开注册机->输入ULtredit的自 ...

  4. 一、IRIG-B 概念

    参考:http://baike.baidu.com/view/3601618.htm http://wenku.baidu.com/view/7956cd29bd64783e09122bf1.html ...

  5. Ubuntu 12.04 Desktop使用XAMPP

    Ubuntu 12.04 Desktop安装XAMPP Ubuntu 12.04 Desktop配置XAMPP Ubuntu 12.04 Desktop使用XAMPP 1/打开GUI界面的管理工具 终 ...

  6. 数据类型 swift

    1整形 Int,Int8,Int16,Int32,Int64 UInt,UInt8,UInt16,UInt32,UInt64 其中Int,UInt始终和当前平台的原生字长相同(32位机,64位机) 查 ...

  7. JAVA类与对象(七)------继承

    理解:继承可以理解为一个对象获取属性的过程.如果类A是类B的父类,而类B是类C的父类,我们也称C是A的子类,类C是从类A继承而来.    在java中,类的继承是单一继承,也就是说,一个子类只能拥有一 ...

  8. iTween基础之iTweenPath(自定义路径移动)

    在游戏开发中经常会用到让一个游戏对象按照指定的路线移动,iTweenPath就提供了可视化的编辑路径功能. iTweenPath 下载地址: http://download.csdn.net/deta ...

  9. Ant学习---第三节:使用Ant实现一个最小的项目编译

    1.编译 .java 文件,生成 .jar 包,代码如下: <?xml version="1.0" encoding="UTF-8"?> <p ...

  10. 项目结队开发---NABC分析(成员)

    一.简介 项目名称:校园导航 特点:手机app,简便易用,适合对铁大地形不了解.路痴者使用. 二.NABC分析 N(need):对于新生报到,学生家长参观校园等想要了解校园路线者,本app软件将带给你 ...