【uva753/poj1087/hdu1526-A Plug for UNIX】最大流
题意:给定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】最大流的更多相关文章
- 【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 ...
- POJ1087:A Plug for UNIX(最大流)
A Plug for UNIX 题目链接:https://vjudge.net/problem/POJ-1087 Description: You are in charge of setting u ...
- POJ1087 A Plug for UNIX —— 最大流
题目链接:https://vjudge.net/problem/POJ-1087 A Plug for UNIX Time Limit: 1000MS Memory Limit: 65536K T ...
- ZOJ1157, POJ1087,UVA 753 A Plug for UNIX (最大流)
链接 : http://acm.hust.edu.cn/vjudge/problem/viewProblem.action? id=26746 题目意思有点儿难描写叙述 用一个别人描写叙述好的. 我的 ...
- 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 ...
- POJ A Plug for UNIX (最大流 建图)
Description You are in charge of setting up the press room for the inaugural meeting of the United N ...
- UVa 753 A Plug for UNIX (最大流)
题意:给定 n 种插座,m种设备,和k个转换器,问你最少有几台设备不能匹配. 析:一个很裸的网络流,直接上模板就行,建立一个源点s和汇点t,源点和每个设备连一条边,每个插座和汇点连一条边,然后再连转换 ...
- hdu 1087 A Plug for UNIX 最大流
题意:http://www.phpfans.net/article/htmls/201012/MzI1MDQw.html 1.在一个会议室里有n种插座,每种插座一个: 2.每个插座只能插一种以及一个电 ...
- 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 ...
- POJ1087 A Plug for UNIX(网络流)
A Plug for UNIX Time Limit: 1000MS Memory Limit: 65536K Total S ...
随机推荐
- iTween基础之iTweenPath(自定义路径移动)
在游戏开发中经常会用到让一个游戏对象按照指定的路线移动,iTweenPath就提供了可视化的编辑路径功能. iTweenPath 下载地址: http://download.csdn.net/deta ...
- 字符串流sstream[part2/使用同一个字符串流反复读写数据]
stringstream构造函数会特别消耗内存,似乎不打算主动释放内存(或许是为了提高效率),如果你要在程序中使用同一个流反复读写大量数据,将会造成大量的内部消耗,因此建议: 1:调用clear ...
- svg绘制蓝色星空,月亮,旋转灯塔
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- android 实现2张图片层叠效果
如图: 代码: <RelativeLayout android:layout_width="match_parent" android:layout_height=" ...
- adb出现unkown host advices 错误
今日在Windows DOS窗口中输入adb命令,如adb devices,adb shell等后,会出现如下错误: adb server is out of date. killing... AD ...
- 使用Java Service Wrapper在Linux下配置Tomcat应用
前言 Java Service Wrapper是Tanuki Software的一个产品,可以将Java应用注册成Windows或Linux服务,使其可以随系统开机启动,同时可以监控Java应用的状态 ...
- Spring MVC 学习笔记 data binding
最近在实验Spring的时候遇到了一个问题: SEVERE: Servlet.service() for servlet [DispatcherServlet] in context with ...
- 【POJ】【1160】Post Office
DP/四边形不等式 邮局,经典的四边形不等式例题! 关于四边形不等式的学习请看 赵爽论文<动态规划加速原理之四边形不等式> 题目总结&题解:http://blog.csdn.net ...
- NF3 里面的z cull reverse reload
nf3 siggraph2011的 分享 里面有谈对csm的优化. 1.mask white red 2. HI Z 这俩我都懂 3.reverse depth buffer这实在不明白, 为什么会有 ...
- C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V110\Microsoft.CppCommon.targets(611,5): error MSB
project options, linker, manifest, Generate Manifest-> NO. 项目->属性->链接器->清单文件->生成清单 改 ...