题意:给定一些插座和一些插头,还有一些单向接头,比如A->B

接头可以串联A->B->C->D

使得插座和插头匹配数目最大

题解:

首先接头可以用Floyd处理

这题可以转化为二分图的模型,就是直接连边,不做处理

 #include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<map>
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#define MAXN 505
#define INF 0x7f7f7f7f
#define ll long long
#define P 203
using namespace std;
int n,m,e,N;
int V1[MAXN],V2[MAXN];
int G[MAXN<<][MAXN<<];
map<ll,int> mp;
ll Hash(string s){
ll ret=;
for(string::iterator it=s.begin();it!=s.end();it++){
ret=ret*P+(*it);
}
return ret;
}
struct Edge{
int from,to,cap,flow;
Edge(int u=,int v=,int c=,int f=){
from=u,to=v,cap=c,flow=f;
}
};
struct Dinic{
int n,m,s,t;
vector<Edge> edges;
vector<int> G[MAXN];
int b[MAXN];
int d[MAXN];
int cur[MAXN];
void init(int n,int s,int t){
this->n=n;
this->s=s,this->t=t;
edges.clear();
for(int i=;i<=n;i++){
G[i].clear();
}
}
void AddEdge(int x,int y,int cap){
edges.push_back(Edge(x,y,cap,));
edges.push_back(Edge(y,x,,));
m=edges.size();
G[x].push_back(m-);
G[y].push_back(m-);
}
bool BFS(){
memset(b,,sizeof(b));
queue<int> q;
d[s]=;
q.push(s);
b[s]=;
while(!q.empty()){
int x=q.front();q.pop();
for(int i=;i<G[x].size();i++){
Edge& e=edges[G[x][i]];
if(e.cap>e.flow&&!b[e.to]){
d[e.to]=d[x]+;
q.push(e.to);
b[e.to]=;
}
}
}
return b[t];
}
int dfs(int x,int a){
if(x==t||!a)return a;
int flow=,f;
for(int& i=cur[x];i<G[x].size();i++){
Edge& e=edges[G[x][i]];
if(d[e.to]==d[x]+&&(f=dfs(e.to,min(a,e.cap-e.flow)))>){
edges[G[x][i]].flow+=f;
edges[G[x][i]^].flow-=f;
flow+=f;
a-=f;
if(!a){
break;
}
}
}
return flow;
}
int Maxflow(){
int flow=;
while(BFS()){
memset(cur,,sizeof(cur));
flow+=dfs(s,INF);
}
return flow;
}
}D;
void init(){
memset(G,,sizeof(G));
mp.clear();
N=;
ll h;
string t1,t2;
scanf("%d",&n);
for(int i=;i<=n;i++){
cin>>t1;
h=Hash(t1);
if(mp.count(h)){
V1[i]=mp[h];
}
else{
N++;
mp[h]=N;
V1[i]=N;
}
}
scanf("%d",&m);
for(int i=;i<=m;i++){
cin>>t2>>t1;
h=Hash(t1);
if(mp.count(h)){
V2[i]=mp[h];
}
else{
N++;
mp[h]=N;
V2[i]=N;
}
}
scanf("%d",&e);
for(int i=;i<=e;i++){
cin>>t1>>t2;
int c1,c2;
h=Hash(t1);
if(mp.count(h)){
c1=mp[h];
}
else{
N++;
mp[h]=N;
c1=N;
}
h=Hash(t2);
if(mp.count(h)){
c2=mp[h];
}
else{
N++;
mp[h]=N;
c2=N;
}
G[c1][c2]=;
}
for(int i=;i<=n;i++){
G[i][i]=;
}
for(int k=;k<=N;k++){
for(int i=;i<=N;i++){
for(int j=;j<=N;j++){
G[i][j]|=(G[i][k]&G[k][j]);
}
}
}
}
void solve(){
D.init(n,,n+m+);
for(int i=;i<=m;i++){
for(int j=;j<=n;j++){
if(G[V2[i]][V1[j]]){
D.AddEdge(i,j+m,);
}
}
}
for(int i=;i<=m;i++){
D.AddEdge(,i,);
}
for(int j=;j<=n;j++){
D.AddEdge(j+m,n+m+,);
}
printf("%d\n",m-D.Maxflow());
}
int main()
{
// freopen("data.in","r",stdin);
// freopen("my.out","w",stdout);
int T;
scanf("%d",&T);
while(T--){
init();
solve();
if(T){
printf("\n");
}
}
return ;
}

二分图匹配

也可以把相同类型的压在一起处理,

源点到某类型插头的容量就是该类型的数目,汇点同理

如果相连的话建一条长度为INF的边,

然后跑一遍最大流即可

 #include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<map>
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#define MAXN 505
#define INF 0x7f7f7f7f
#define ll long long
#define P 203
using namespace std;
int n,m,e,N;
int V1[MAXN],V2[MAXN];
int b1[MAXN],b2[MAXN];
int cnt1,cnt2;
int G[MAXN<<][MAXN<<];
map<string,int> mp;
struct Edge{
int from,to,cap,flow;
Edge(int u=,int v=,int c=,int f=){
from=u,to=v,cap=c,flow=f;
}
};
struct Dinic{
int n,m,s,t;
vector<Edge> edges;
vector<int> G[MAXN];
int b[MAXN];
int d[MAXN];
int cur[MAXN];
void init(int n,int s,int t){
this->n=n;
this->s=s,this->t=t;
edges.clear();
for(int i=;i<=n;i++){
G[i].clear();
}
}
void AddEdge(int x,int y,int cap){
edges.push_back(Edge(x,y,cap,));
edges.push_back(Edge(y,x,,));
m=edges.size();
G[x].push_back(m-);
G[y].push_back(m-);
}
bool BFS(){
memset(b,,sizeof(b));
queue<int> q;
d[s]=;
q.push(s);
b[s]=;
while(!q.empty()){
int x=q.front();q.pop();
for(int i=;i<G[x].size();i++){
Edge& e=edges[G[x][i]];
if(e.cap>e.flow&&!b[e.to]){
d[e.to]=d[x]+;
q.push(e.to);
b[e.to]=;
}
}
}
return b[t];
}
int dfs(int x,int a){
if(x==t||!a)return a;
int flow=,f;
for(int& i=cur[x];i<G[x].size();i++){
Edge& e=edges[G[x][i]];
if(d[e.to]==d[x]+&&(f=dfs(e.to,min(a,e.cap-e.flow)))>){
edges[G[x][i]].flow+=f;
edges[G[x][i]^].flow-=f;
flow+=f;
a-=f;
if(!a){
break;
}
}
}
return flow;
}
int Maxflow(){
int flow=;
while(BFS()){
memset(cur,,sizeof(cur));
flow+=dfs(s,INF);
}
return flow;
}
}D;
void init(){
memset(G,,sizeof(G));
memset(b1,,sizeof(b1));
memset(b2,,sizeof(b2));
cnt1=cnt2=;
mp.clear();
N=;
string t1,t2;
scanf("%d",&n);
for(int i=;i<=n;i++){
cin>>t1;
if(mp.count(t1)){
V1[i]=mp[t1];
}
else{
N++;
mp[t1]=N;
V1[i]=N;
}
}
scanf("%d",&m);
for(int i=;i<=m;i++){
cin>>t2>>t1;
if(mp.count(t1)){
V2[i]=mp[t1];
}
else{
N++;
mp[t1]=N;
V2[i]=N;
}
}
scanf("%d",&e);
for(int i=;i<=e;i++){
cin>>t1>>t2;
int c1,c2;
if(mp.count(t1)){
c1=mp[t1];
}
else{
N++;
mp[t1]=N;
c1=N;
}
if(mp.count(t2)){
c2=mp[t2];
}
else{
N++;
mp[t2]=N;
c2=N;
}
G[c1][c2]=;
}
for(int i=;i<=n;i++){
G[i][i]=;
}
for(int k=;k<=N;k++){
for(int i=;i<=N;i++){
for(int j=;j<=N;j++){
G[i][j]|=(G[i][k]&G[k][j]);
}
}
}
for(int i=;i<=n;i++){
if(!b1[V1[i]]){
cnt1++;
}
b1[V1[i]]++;
}
for(int i=;i<=m;i++){
if(!b2[V2[i]]){
cnt2++;
}
b2[V2[i]]++;
}
}
void solve(){
D.init(n,,cnt1+cnt2+);
int t1=,t2=cnt1;
for(int i=;i<=N;i++){
if(b2[i]) t2++;
else continue;
t1=;
for(int j=;j<=N;j++){
if(b1[j]){
t1++;
if(G[i][j]){
D.AddEdge(t1,t2,INF);
}
}
}
}
t1=;
for(int i=;i<=N;i++){
if(b1[i]){
t1++;
D.AddEdge(,t1,b1[i]);
}
}
t2=;
for(int j=;j<=N;j++){
if(b2[j]){
t2++;
D.AddEdge(t2+cnt1,cnt1+cnt2+,b2[j]);
}
}
printf("%d\n",m-D.Maxflow());
}
int main()
{
// freopen("data.in","r",stdin);
// freopen("my.out","w",stdout);
int T;
scanf("%d",&T);
while(T--){
init();
solve();
if(T){
printf("\n");
}
}
return ;
}

压点最大流

UVA753: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. 【uva753/poj1087/hdu1526-A Plug for UNIX】最大流

    题意:给定n个插座,m个插头,k个转换器(x,y),转换器可以让插头x转成插头y.问最少有多少个插头被剩下. 题解: 最大流或者二分图匹配.然而我不知道怎么打二分图匹配..打了最大流.这题字符串比较坑 ...

  3. 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 ...

  4. A Plug for UNIX 分类: POJ 图论 函数 2015-08-10 14:18 2人阅读 评论(0) 收藏

    A Plug for UNIX Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14786 Accepted: 4994 Desc ...

  5. UVa753/POJ1087_A Plug for UNIX(网络流最大流)(小白书图论专题)

    解题报告 题意: n个插头m个设备k种转换器.求有多少设备无法插入. 思路: 定义源点和汇点,源点和设备相连,容量为1. 汇点和插头相连,容量也为1. 插头和设备相连,容量也为1. 可转换插头相连,容 ...

  6. poj 1087 C - A Plug for UNIX 网络流最大流

    C - A Plug for UNIXTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contes ...

  7. UVA 753 - A Plug for UNIX(网络流)

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

  8. POJ1087 A Plug for UNIX 【最大流】

    A Plug for UNIX Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13855   Accepted: 4635 ...

  9. POJ 1087 A Plug for UNIX / HDU 1526 A Plug for UNIX / ZOJ 1157 A Plug for UNIX / UVA 753 A Plug for UNIX / UVAlive 5418 A Plug for UNIX / SCU 1671 A Plug for UNIX (网络流)

    POJ 1087 A Plug for UNIX / HDU 1526 A Plug for UNIX / ZOJ 1157 A Plug for UNIX / UVA 753 A Plug for ...

随机推荐

  1. 201621123057 《Java程序设计》第4周学习总结

    1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 答: (普通方法 / 构造函数)重载. static . final.继承与多态.extends.object类.abstrac ...

  2. http post/get 2种使用方式

     public class HttpUtil { //HttpPost public static String executePost(String url, List<NameValue ...

  3. 动手写IL到Lua的翻译器——准备

    文章里的代码粘过来的时候格式有点问题,原因是一开始文章是在订阅号上写的(gamedev101,文末有二维码),不知道为啥贴过来就没了格式,还要手动删行号,就没搞了. 介绍下问题背景: 小说君正在参与的 ...

  4. 数据恢复案例分享:MSSQL 2000 错误823

    一.故障描述 MSSQL Server 2000 附加数据库错误823,附加数据库失败.数据库没有备份,不能通过备份恢复数据库,急需恢复数据库中的数据. 二.故障分析SQL Server数据库 823 ...

  5. Echarts柱状图实现不同颜色渐变色

    第一次写文,只是想记录一下自己平时发现的小功能,这篇主要是实现echarts柱状图,每个柱子实现不同颜色的渐变色,也是第一次接触echarts,后台使用ssm,前台是extjs,直接上效果图 直接上j ...

  6. 关于网页设计的css+html相对定位和决定定位的理解

    css中有很多定位,其中最重要的是相对定位和绝对定位: 定位很重要,不搞好,网页就会很乱,显示的完全不是自己想要的效果,自己必须掌握: 首先说一个重要的结论:绝对定位,是不占位置的,总是相对离自己最近 ...

  7. 查找git ignore的追踪

    前言 版本控制说简单也简单,说复杂也困难的多.作为开发者,最基础的版本管理和团队协作的功能必须掌握.而其他一些相关的信息也可以了解下.比如,这次就有同事遇到了问题. 遇到的问题 在windows下,往 ...

  8. SpringCloud的应用发布(一)SpringCloud的样例工程

    前言 这个综合例子创建了 6个微服务应用 一个服务注册中心 SvcReg(EurekaServer),生产中要考虑高可用 一个配置中心 CfgMgr + git目录存储配置(ConfigServer, ...

  9. loadrunner录制时web时,安全证书问题

    测试环境:win7+LoadRunner11+ie9 遇到的问题:用LoadRunner录制时,打开百度,总是报安全证书问题,如图所示 解决方法:Tools——Recording Options——p ...

  10. mysql的账户管理

    mysql中账户管理:1 查看所有用户: 所有用户及权限信息都存储在mysql数据库中的user表中 查看user表的结构 desc user\G; 主要字段: host: 表示允许访问的主机 use ...