Ural1109_Conference(二分图最大匹配/匈牙利算法/网络最大流)
2024-10-15 02:41:29
解题报告
二分图第一题。
题目描写叙述:
为了參加即将召开的会议,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
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 M, N, K and K pairs of representatives, finds the minimum number of needed connections.
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
The first line of the input contains M, N 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.
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 |
3 |
Problem Source: Bulgarian National Olympiad Day #1
最新文章
- 第 31 章 项目实战-PC 端固定布局[5]
- Linux查询网址
- javascript DOM对象
- js 操作cookie
- UVA 110 Meta-Loopless Sorts(输出挺麻烦的。。。)
- ASP.NET Core之跨平台的实时性能监控(2.健康检查)
- arcgis地图服务之 identify 服务
- 【XSY2469】graph 分治 并查集
- java 命令行JDBC连接Mysql
- sql server 运维时CPU,内存,操作系统等信息查询(用sql语句)
- How can I add a site title refiner
- [AWS] Amazon Cognito
- 写出一个程序,接受一个由字母和数字组成的字符串,和一个字符,然后输出输入字符串中含有该字符的个数。不区分大小写。java算法
- iOS 多页面跳转同一页面时数据处理
- 2668: [cqoi2012]交换棋子
- 统计时间段内周分类SQL语句
- c#上一周下一周代码
- WPF入门教程系列三
- HDU 3466 Proud Merchants【贪心 + 01背包】
- 关于Lisp和函数式编程 &; 各种语言对比 &; TIOBE