题意:给定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. Asp.Net MVC如何返回401响应码

    需求:     在默认创建的Asp.Net MVC项目中(这里使用VS2013),需要手动返回一个401响应码给浏览器.我们的代码可能是下面这样子的.   public ActionResult Un ...

  2. centos下安装nagios

    摘要Nagios是一款开源的免费网络监视工具,能有效监控Windows.Linux和Unix的主机状态,交换机路由器等网络设置,打印机等. Nagios是一款开源的免费网络监视工具,能有效监控Wind ...

  3. ios8中的UIScreen

    let orientation: UIInterfaceOrientation = UIApplication.sharedApplication().statusBarOrientation pri ...

  4. [转]UDP/TCP穿越NAT的P2P通信方法研究(UDP/TCP打洞 Hole Punching)

     [转]UDP/TCP穿越NAT的P2P通信方法研究(UDP/TCP打洞 Hole Punching) http://www.360doc.com/content/12/0428/17/6187784 ...

  5. [读行者][学习LinqExpression和Reflection(Emit)]阅读TypeBuilderSample之ExampleFromTheArticle

    前言 关于”读行者“ 俗语有云:"读万卷书,行万里路“.多读一些优秀代码,不仅可以锻炼我们读代码的能力(便于维护或相互交流),还可以吸取很多我们成长所需的知识点.多读,才能开阔我们的眼界,才 ...

  6. modelsim仿真错误解决办法

    编译不成功可能是因为: 1.本身程序有问题. 2.没有设置顶层文件 3.modelsim 出现错误是不要只是看错误的地方,也要看前面的一部分

  7. iOS开发HTTPS实现之信任SSL证书和自签名证书

    iOS开发HTTPS实现之信任SSL证书和自签名证书 转自:http://www.jianshu.com/p/6b9c8bd5005a/comments/5539345 (收录一下供自己学习用的) 字 ...

  8. 如何ping通两台计算机

    如何ping通两台计算机 因为ping是基于IP协议的,所以,先要保证两台计算机在同一个子网中,这里涉及到vlan和子网的概念 若两台主机不在同一个子网中则无法ping通 若两台主机在同一个子网中却p ...

  9. java 的 (PO,VO,TO,BO,DAO,POJO) 解释

    最近在给Android写接口,其中服务器数据需要定义VO(Value Object)对象进行封装传输 对于VO ,PO , BO , QO,DAO,POJO概念还是比较模糊,所以在这里记录一下: O/ ...

  10. Windows+Apache+MySQL+PHP(WAMP)环境搭建

    运行操作系统:Windows Server 2008 R2 Apache版本:Apache 2.2 MySQL版本:MySQL 5.5 PHP版本:PHP 5.6.14(当前最新版) 更新日期:201 ...