Ural1109_Conference(二分图最大匹配/匈牙利算法/网络最大流)
解题报告
二分图第一题。
题目描写叙述:
为了參加即将召开的会议,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
Memory limit: 64 MB
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 M, N, K and K pairs of representatives, finds the minimum number of needed connections.
Input
member of A and p2 is member of B.
Output
Sample
| input | output |
|---|---|
3 2 4 |
3 |
Ural1109_Conference(二分图最大匹配/匈牙利算法/网络最大流)的更多相关文章
- HDU 1045 - Fire Net - [DFS][二分图最大匹配][匈牙利算法模板][最大流求二分图最大匹配]
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1045 Time Limit: 2000/1000 MS (Java/Others) Mem ...
- HDU - 1045 Fire Net (二分图最大匹配-匈牙利算法)
(点击此处查看原题) 匈牙利算法简介 个人认为这个算法是一种贪心+暴力的算法,对于二分图的两部X和Y,记x为X部一点,y为Y部一点,我们枚举X的每个点x,如果Y部存在匹配的点y并且y没有被其他的x匹配 ...
- UESTC 919 SOUND OF DESTINY --二分图最大匹配+匈牙利算法
二分图最大匹配的匈牙利算法模板题. 由题目易知,需求二分图的最大匹配数,采取匈牙利算法,并采用邻接表来存储边,用邻接矩阵会超时,因为邻接表复杂度O(nm),而邻接矩阵最坏情况下复杂度可达O(n^3). ...
- HDU1068 (二分图最大匹配匈牙利算法)
Girls and Boys Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- poj - 3041 Asteroids (二分图最大匹配+匈牙利算法)
http://poj.org/problem?id=3041 在n*n的网格中有K颗小行星,小行星i的位置是(Ri,Ci),现在有一个强有力的武器能够用一发光速将一整行或一整列的小行星轰为灰烬,想要利 ...
- 二分图最大匹配(匈牙利算法) POJ 3041 Asteroids
题目传送门 /* 题意:每次能消灭一行或一列的障碍物,要求最少的次数. 匈牙利算法:把行和列看做两个集合,当有障碍物连接时连一条边,问题转换为最小点覆盖数==二分图最大匹配数 趣味入门:http:// ...
- 51Nod 2006 飞行员配对(二分图最大匹配)-匈牙利算法
2006 飞行员配对(二分图最大匹配) 题目来源: 网络流24题 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 第二次世界大战时期,英国皇家空军从沦陷国 ...
- poj 3894 System Engineer (二分图最大匹配--匈牙利算法)
System Engineer Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 507 Accepted: 217 Des ...
- POJ1274:The Perfect Stall(二分图最大匹配 匈牙利算法)
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 17895 Accepted: 814 ...
随机推荐
- Windows Phone 8初学者开发—第13部分:设置LongListSelector中磁贴的样式
原文 Windows Phone 8初学者开发—第13部分:设置LongListSelector中磁贴的样式 第13部分:设置LongListSelector中磁贴的样式 原文地址: http://c ...
- 宣布 Windows Azure 通过 PCI DSS 合规性验证并且 ISO 认证范围扩大,同时正式发布 Windows Azure Hyper-V 恢复管理器和其他更新功能
今天,我们高兴地宣布两个重大里程碑事件,客户将能借此提高基于 Windows Azure 构建安全且合规的应用程序的能力.此外,我们还宣布正式发布 Windows Azure Hyper-V 恢复管理 ...
- GDKOI2016
天若有情天亦老 月若无恨月常圆 Day1 score cardcaptor AAAAAAAATT protal WWWWWWWWWW treasurehunt AAAAWXXXXX map AAATT ...
- Redhat 5.8系统安装R语言作Arima模型预测
请见Github博客:http://wuxichen.github.io/Myblog/timeseries/2014/09/02/RJavaonLinux.html
- stm32之Systick(系统时钟)
Systick的两大作用: 1.可以产生精确延时: 2.可以提供给操作系统一个单独的心跳(时钟)节拍: 通常实现Delay(N)函数的方法为: for(i=0;i<x;i++) ; 对于STM3 ...
- css3属性——border-radius用法
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <style> ...
- UIBezierPath详解
使用UIBezierPath类可以创建基于矢量的路径,这个类在UIKit中.此类是Core Graphics框架关于path的一个封装.使用此类可以定义简单的形状,如椭圆或者矩形,或者有多个直线和曲线 ...
- BZOJ 3408: [Usaco2009 Oct]Heat Wave 热浪( 最短路 )
普通的最短路...dijkstra水过.. ------------------------------------------------------------------------------ ...
- 使用Qt设计师文件的3种方式
使用Qt设计师设计的.ui界面文件是XML文件,有3种方式在PyQt中使用,本文将通过实例进行讲解. 使用PyQt中如何结合Qt设计师进行开发中的例子.点击按钮修改标签的内容. 1. 直接使用ui文件 ...
- HDU2138 随机素数测试 Miller-Rabin算法
题目描述 Give you a lot of positive integers, just to find out how many prime numbers there are.. In eac ...