解题报告

二分图第一题。

题目描写叙述:

为了參加即将召开的会议,A国派出M位代表,B国派出N位代表,(N,M<=1000)

会议召开前,选出K队代表,每对代表必须一个是A国的,一个是B国的;

要求每个代表要与还有一方的一个代表联系,除了能够直接联系,也能够电话联系,求电话联系最少

思路:

电话联系最少就要使直接联系最大,又是一一匹配关系,就是二分图的最大匹配。

以下是匈牙利算法。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 1050
#define M 1050
using namespace std;
int mmap[M][N],vis[N],pre[N],n,m,k;
int dfs(int x)
{
for(int i=1;i<=n;i++)
{
if(!vis[i]&&mmap[x][i])
{
vis[i]=1;
if(pre[i]==-1||dfs(pre[i]))
{
pre[i]=x;
return 1;
}
}
}
return 0;
}
int main()
{
int i,j,u,v;
memset(pre,-1,sizeof(pre));
memset(vis,0,sizeof(vis));
scanf("%d%d%d",&m,&n,&k);
for(i=0;i<k;i++)
{
scanf("%d%d",&u,&v);
mmap[u][v]=1;
}
int ans=0;
for(i=1;i<=m;i++)
{
memset(vis,0,sizeof(vis));
ans+=dfs(i);
}
printf("%d\n",n+m-ans);
}

二分最大匹配也能够用最大流做,当试试模板

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define inf 99999999
#define N 1050
#define M 1050
using namespace std;
int mmap[M+N][N+M],vis[N],l[N+M],n,m,k;
int bfs()
{
queue<int>Q;
Q.push(0);
memset(l,-1,sizeof(l));
l[0]=0;
while(!Q.empty())
{
int u=Q.front();
Q.pop();
for(int i=0;i<=n+m+1;i++)
{
if(l[i]==-1&&mmap[u][i])
{
l[i]=l[u]+1;
Q.push(i);
}
}
}
if(l[n+m+1]>0)
return 1;
return 0;
}
int dfs(int x,int f)
{
int a,i;
if(x==n+m+1)
return f;
for(i=0;i<=n+m+1;i++)
{
if(mmap[x][i]&&l[i]==l[x]+1&&(a=dfs(i,min(f,mmap[x][i]))))
{
mmap[x][i]-=a;
mmap[i][x]+=a;
return a;
}
}
return 0;
}
int main()
{
int i,j,u,v;
memset(vis,0,sizeof(vis));
scanf("%d%d%d",&m,&n,&k);
for(i=1;i<=m;i++)
mmap[0][i]=1;
for(i=m+1;i<=m+n;i++)
mmap[i][m+n+1]=1;
for(i=0;i<k;i++)
{
scanf("%d%d",&u,&v);
mmap[u][v+m]=1;
}
int ans=0,a;
while(bfs())
while(a=dfs(0,inf))
ans+=a;
printf("%d\n",n+m-ans);
}

Conference

Time limit: 0.5 second

Memory limit: 64 MB
On the upcoming conference were sent M representatives of country A and N representatives of country B (M and N ≤ 1000). The representatives were
identified with 1, 2, …, M for country A and 1, 2, …, N for country B. Before the conference K pairs of representatives were chosen. Every such pair consists of one member of delegation A and one of delegation B.
If there exists a pair in which both member #i of A and member #j of B are included then #i and #j can negotiate. Everyone attending the conference was included in at least one pair. The CEO of the congress
center wants to build direct telephone connections between the rooms of the delegates, so that everyone is connected with at least one representative of the other side, and every connection is made between people that can negotiate. The CEO also wants to minimize
the amount of telephone connections. Write a program which given MNK and K pairs of representatives, finds the minimum number of needed connections.

Input

The first line of the input contains MN and K. The following K lines contain the choosen pairs in the form of two integers p1and p2, p1 is
member of A and p2 is member of B.

Output

The output should contain the minimum number of needed telephone connections.

Sample

input output
3 2 4
1 1
2 1
3 1
3 2
3
Problem Source: Bulgarian National Olympiad Day #1

Ural1109_Conference(二分图最大匹配/匈牙利算法/网络最大流)的更多相关文章

  1. HDU 1045 - Fire Net - [DFS][二分图最大匹配][匈牙利算法模板][最大流求二分图最大匹配]

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1045 Time Limit: 2000/1000 MS (Java/Others) Mem ...

  2. HDU - 1045 Fire Net (二分图最大匹配-匈牙利算法)

    (点击此处查看原题) 匈牙利算法简介 个人认为这个算法是一种贪心+暴力的算法,对于二分图的两部X和Y,记x为X部一点,y为Y部一点,我们枚举X的每个点x,如果Y部存在匹配的点y并且y没有被其他的x匹配 ...

  3. UESTC 919 SOUND OF DESTINY --二分图最大匹配+匈牙利算法

    二分图最大匹配的匈牙利算法模板题. 由题目易知,需求二分图的最大匹配数,采取匈牙利算法,并采用邻接表来存储边,用邻接矩阵会超时,因为邻接表复杂度O(nm),而邻接矩阵最坏情况下复杂度可达O(n^3). ...

  4. HDU1068 (二分图最大匹配匈牙利算法)

    Girls and Boys Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  5. poj - 3041 Asteroids (二分图最大匹配+匈牙利算法)

    http://poj.org/problem?id=3041 在n*n的网格中有K颗小行星,小行星i的位置是(Ri,Ci),现在有一个强有力的武器能够用一发光速将一整行或一整列的小行星轰为灰烬,想要利 ...

  6. 二分图最大匹配(匈牙利算法) POJ 3041 Asteroids

    题目传送门 /* 题意:每次能消灭一行或一列的障碍物,要求最少的次数. 匈牙利算法:把行和列看做两个集合,当有障碍物连接时连一条边,问题转换为最小点覆盖数==二分图最大匹配数 趣味入门:http:// ...

  7. 51Nod 2006 飞行员配对(二分图最大匹配)-匈牙利算法

    2006 飞行员配对(二分图最大匹配) 题目来源: 网络流24题 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 第二次世界大战时期,英国皇家空军从沦陷国 ...

  8. poj 3894 System Engineer (二分图最大匹配--匈牙利算法)

    System Engineer Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 507   Accepted: 217 Des ...

  9. POJ1274:The Perfect Stall(二分图最大匹配 匈牙利算法)

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17895   Accepted: 814 ...

随机推荐

  1. SpringBoot接口服务处理Whitelabel Error Page(转)

    To switch it off you can set server.error.whitelabel.enabled=false http://stackoverflow.com/question ...

  2. perl encode_utf8 和decode_utf8

    encode_utf8 等于 $octets = encode_utf8($string); 这个字符串 在$string 在Perl的内部格式,返回结果是作为一个顺序的字节. 因为所有的可能的字符串 ...

  3. boost库学习随记六:使用同步定时器、异步定时器、bind、成员函数回调处理、多线程的同步处理示例等

    一.使用同步定时器 这个示例程序通过展示如何在一个定时器执行一个阻塞等待. //makefile #-------------------------------------------------- ...

  4. BZOJ 1385: [Baltic2000]Division expression

    题目 1385: [Baltic2000]Division expression Time Limit: 5 Sec  Memory Limit: 64 MB Description 除法表达式有如下 ...

  5. 【课程分享】深入浅出嵌入式linux系统移植开发 (环境搭建、uboot的移植、嵌入式内核的配置与编译)

    深入浅出嵌入式linux系统移植开发 (环境搭建.uboot的移植.嵌入式内核的配置与编译) 亲爱的网友,我这里有套课程想和大家分享,假设对这个课程有兴趣的,能够加我的QQ2059055336和我联系 ...

  6. BZOJ 2435: [Noi2011]道路修建( dfs )

    NOI的水题...直接一遍DFS即可 ------------------------------------------------------------------------- #includ ...

  7. 2014 HDU多校弟九场I题 不会DP也能水出来的简单DP题

    听了ZWK大大的思路,就立马1A了 思路是这样的: 算最小GPA的时候,首先每个科目分配到69分(不足的话直接输出GPA 2),然后FOR循环下来使REMAIN POINT减少,每个科目的上限加到10 ...

  8. 关于ios下录音

    http://blog.csdn.net/silencetq/article/details/8447400 我是采用的AVAudioRecorder这个框架来进行录音 这个录音跟官方网站上的spea ...

  9. 转:Linus:利用二级指针删除单向链表

    感谢网友full_of_bull投递此文(注:此文最初发表在这个这里,我对原文后半段修改了许多,并加入了插图) Linus大婶在slashdot上回答一些编程爱好者的提问,其中一个人问他什么样的代码是 ...

  10. Hdu 1175 连连看(DFS)

    Problem地址:http://acm.hdu.edu.cn/showproblem.php?pid=1175 因为题目只问能不能搜到,没问最少要几个弯才能搜到,所以我采取了DFS. 因为与Hdu ...