题目链接: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. codeforces437C

    The Child and Toy CodeForces - 437C On Children's Day, the child got a toy from Delayyy as a present ...

  2. BZOJ2214[Poi2011]Shift——模拟

    题目描述 Byteasar bought his son Bytie a set of blocks numbered from to and arranged them in a row in a ...

  3. jQuery文档处理总结

    <!DOCTYPE html> <html lang="cn"> <head> <meta charset="UTF-8&quo ...

  4. 洛谷 P2058 海港 解题报告

    P2058 海港 题目描述 小K是一个海港的海关工作人员,每天都有许多船只到达海港,船上通常有很多来自不同国家的乘客. 小K对这些到达海港的船只非常感兴趣,他按照时间记录下了到达海港的每一艘船只情况: ...

  5. pyEcharts

    例子: from pyecharts import Line line = Line("我的第一个图表", "这里是副标题",width="800px ...

  6. CF1114B Yet Another Array Partitioning Task(贪心,构造题)

    我至今不敢相信我被这么一道简单的题卡了这么久……看来还是太弱了…… 题目链接:CF原网 题目大意:定义一个序列的“美丽度”为这个序列前 $m$ 大的数的和.现在有一个长度为 $n$ 的序列,你需要把它 ...

  7. 在Android中通过Intent使用Bundle传递对象

    IntentBundle传递对象SerializableParcelable Android开发中有时需要在应用中或进程间传递对象,下面详细介绍Intent使用Bundle传递对象的方法.被传递的对象 ...

  8. ffmpeg 在ubuntu上编译环境搭建和开发

    步骤如下: 1. 下载 官网永远是王道,呵呵:http://ffmpeg.org/download.html 或者 svn checkout svn://svn.mplayerhq.hu/ffmpeg ...

  9. JAVA:当数据库重启后连接池没有自动识别的解决办法

    今天发现服务器上的一个服务程序出现问题,软件抛出:Connection reset by peer: socket write error 无法正常提供服务,找了一下原因,原来是因为数据库服务器重启, ...

  10. django 403问题

    C:\Users\x\pyp1>python manage.py runserverPerforming system checks... System check identified no ...