题目链接:https://cn.vjudge.net/contest/245468#problem/F

大意:给你插座和电器的对应关系,有多个电器对应一个插座的情况,但是一个插座只能供一个电器使用,现在有一个转换头,能够将一个插座改成三个插头,问你最多能匹配多少个电器。

思路: HK(洪凯)的思路。先按照没有转换头的思路跑一下匈牙利算法。然后,遍历有多个电器对应一个插座的这种情况,再“加”上两个插座,不过加上的这两个插座,对应关系和你当前正在遍历的这个插座与灯泡的对应关系相同,然后再看一下加入的这两个插座能不能匹配到灯泡,然后求出最大匹配量。

代码:

#include<iostream>
#include<string>
#include<cstring>
#include<iomanip>
#include<map>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
using namespace std;
#define maxn 1500+10
int m,n,k;
int vis[maxn];
int net[maxn];
int e[maxn];
int a[maxn];
vector<int >q[maxn];
vector<int >wa[maxn];
bool Find(int t)
{
int len=q[t].size();
for(int i=0; i<len; i++)
{
int temp=q[t][i];
if(vis[temp]==0)
{
vis[temp]=1;
if(net[temp]==0||Find(net[temp]))
{
net[temp]=t;
return true;
}
}
}
return false;
}
int match()
{
int ans=0;
for(int i=1; i<=m; i++)
{
memset(vis,0,sizeof(vis));
if(Find(i))ans++;
}
return ans;
}
int main()
{
memset(a,0,sizeof(a));
memset(net,0,sizeof(net));
cin>>m>>n>>k;
for(int i=1; i<=k; i++)
{
int u,v;
cin>>u>>v;
q[u].push_back(v);//建立插座与灯泡的对应关系,方便后面的遍历。
a[u]++;//记录插座对应的灯泡的个数
}
int t=match();
// cout<<t<<endl;
int maxx=0;
for(int i=1; i<=n; i++)
{
e[i]=net[i];
}
for(int i=1; i<=m; i++)
{
if(a[i]>1)
{
int s=0;
int len=q[i].size();
for(int k=1; k<=2; k++)
{
for(int j=0; j<len; j++)
{
int temp=q[i][j];
q[m+k].push_back(temp);
}
}//加上两个插座
memset(vis,0,sizeof(vis));
if(Find(m+1))s++;
memset(vis,0,sizeof(vis));//注意每次对vis数组进行清空。
if(Find(m+2))s++;
maxx=max(s,maxx);
q[m+1].clear();
q[m+2].clear();//清除新加入的两个的插座的对应关系
for(int l=1; l<=n; l++)
{
net[l]=e[l];
}//在匹配新加的两个插座的时候,原来的灯泡与插座的对应关系有可能会改变,所以需要恢复到原来的对应关系
if(t==n)break;
}
}
cout<<t+maxx<<endl;
return 0;
}

Problem F Plug It In!的更多相关文章

  1. 实验12:Problem F: 求平均年龄

    Home Web Board ProblemSet Standing Status Statistics   Problem F: 求平均年龄 Problem F: 求平均年龄 Time Limit: ...

  2. The Ninth Hunan Collegiate Programming Contest (2013) Problem F

    Problem F Funny Car Racing There is a funny car racing in a city with n junctions and m directed roa ...

  3. Codeforces Gym 100500F Problem F. Door Lock 二分

    Problem F. Door LockTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/at ...

  4. Codeforces Gym 100002 Problem F "Folding" 区间DP

    Problem F "Folding" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/ ...

  5. Codeforces Gym 100286F Problem F. Fibonacci System 数位DP

    Problem F. Fibonacci SystemTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudg ...

  6. Problem F: Exponentiation

    Problem F: ExponentiationTime Limit: 1 Sec Memory Limit: 128 MBSubmit: 4 Solved: 2[Submit][Status][W ...

  7. Problem F: 合唱比赛开始了!

    Problem F: 合唱比赛开始了! Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 440  Solved: 201[Submit][Status][ ...

  8. 几何入门合集 gym101968 problem F. Mirror + gym102082 Problem F Fair Chocolate-Cutting + gym101915 problem B. Ali and Wi-Fi

    abstract: V const & a 加速 F. Mirror 题意 链接 问题: 有n个人在y=0的平面上(及xoz平面).z=0平面上有一面镜子(边平行于坐标轴).z=a平面上有q个 ...

  9. 2013-2014 ACM-ICPC, NEERC, Southern Subregional Contest Problem F. Judging Time Prediction 优先队列

    Problem F. Judging Time Prediction 题目连接: http://www.codeforces.com/gym/100253 Description It is not ...

随机推荐

  1. BZOJ1195[HNOI2006]最短母串——AC自动机+BFS+状态压缩

    题目描述 给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,„,Sn)都是T的子串. 输入 第一行是一个正整数n(n<=12),表示给定的字符串的 ...

  2. The Chinese Postman Problem HIT - 2739(有向图中国邮路问题)

    无向图的问题,如果每个点的度数为偶数,则就是欧拉回路,而对于一个点只有两种情况,奇数和偶数,那么就把都为奇数的一对点  连一条  边权为原图中这两点最短路的值  的边  是不是就好了 无向图中国邮路问 ...

  3. BUPT2017 wintertraining(15) #2 题解

    这场有点难,QAQ.补了好久(。• ︿•̀。) ,总算能写题解了(つд⊂) A. Beautiful numbers CodeForces - 55D 题意 ​ 求\([l,r](1\le l_i\l ...

  4. Shell基础知识(五)

    shell中同样有数组的概念,获取数组中的元素要使用下标[],并且下标的值必须大于等于0.数据的各项特性见下例: #!/bin/bash array1=(1 2 3 999) echo ${array ...

  5. 【转】Context Switches上下文切换性能详解

    http://blog.csdn.net/aiai5251/article/details/50015745 Context Switches 上下文切换,有时也被称为进程切换(process swi ...

  6. bzoj1003/luogu1772 物流运输 (dijkstra+dp)

    先求出某一段时间[i,j]一直用同一个路径的最短路,乘上天数,记作cost[i,j] 那就可以设f[i]是前i天的最小代价,f[i]=f[j]+cost[j+1,i]+K #include<bi ...

  7. POSIX 线程取消点的 Linux 实现

    http://blog.csdn.net/stevenliyong/article/details/4364039 原文链接:http://blog.solrex.cn/articles/linux- ...

  8. Memcached在Windows下的配置和使用

    Memcached学习笔记---- 安装和配置 首先,下载Memcached相关文件. 打开控制台,进入Memcached主程序目录,输入: memcached.exe -d install //安装 ...

  9. Linux中禁用命令历史记录

    关闭history记录功能 set +o history 打开history记录功能 set -o history 清空记录 history -c 记录被清空,重新登录后恢复. rm -f $HOME ...

  10. A1060. Are They Equal

    If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered ...