CSU-1531 Jewelry Exhibition —— 二分图匹配(最小覆盖点)
题目链接:https://vjudge.net/problem/CSU-1531

Input

Output

Sample Input
2
1 5 3
0.2 1.5
0.3 4.8
0.4 3.5
4 4 8
0.7 0.5
1.7 0.5
2.8 1.5
3.7 0.5
2.2 3.6
2.7 2.7
1.2 2.2
1.2 2.7 Sample Output
1
3
题解:
一开始想用DP做,后来发现不行,因为新加入的点会破坏前面的结果,且不知道前面的状态如何,所以不能用动态规划的思想去解题。
1.用最少的边去覆盖掉所有的点,顾名思义,可以用二分图的最小覆盖点去做,只是这题的“点”为二分图的边,这题的"边"为二分图的点。
2.把题目的点的x、y坐标看做二分图的点,把题目的点当做二分图的边,其两端是x、y坐标,这样就转化成了用最少的点去覆盖掉所有的边。
代码一(矩阵):
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define ms(a, b) memset((a), (b), sizeof(a))
#define eps 0.0000001
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int maxn = +; int G[maxn][maxn];
int vis[maxn], match[maxn];
int n,m,k; int find(int u)
{
for(int i = ; i<m; i++)
{
if(G[u][i] && !vis[i] )
{
vis[i] = ;
if(match[i]==- || find(match[i]))
{
match[i] = u;
return ;
}
}
}
return ;
} void solve()
{
double x,y;
scanf("%d%d%d",&n,&m,&k); ms(G,);
for(int i = ; i<k; i++)
{
scanf("%lf%lf",&x,&y);
G[(int)x][(int)y] = ;
} int ans = ;
ms(match,-);
for(int i = ; i<n; i++)
{
ms(vis,);
if(find(i))
ans++;
}
printf("%d\n",ans);
} int main()
{
int T;
scanf("%d",&T);
while(T--){
solve();
} return ;
}
代码二(vector):
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define ms(a, b) memset((a), (b), sizeof(a))
#define eps 0.0000001
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int maxn = +; vector<int> G[maxn];
int vis[maxn], match[maxn];
int n,m,k; int find(int u)
{
int Size = G[u].size();
for(int i = ; i<Size; i++)
{
int v = G[u][i];
if(!vis[v] )
{
vis[v] = ;
if(match[v]==- || find(match[v]))
{
match[v] = u;
return ;
}
}
}
return ;
} void solve()
{
double x,y;
scanf("%d%d%d",&n,&m,&k); for(int i = ; i<n; i++)
G[i].clear();
for(int i = ; i<k; i++)
{
scanf("%lf%lf",&x,&y);
G[(int)x].pb((int)y);
} int ans = ;
ms(match,-);
for(int i = ; i<n; i++)
{
ms(vis,);
if(find(i))
ans++;
}
printf("%d\n",ans);
} int main()
{
int T;
scanf("%d",&T);
while(T--){
solve();
} return ;
}
CSU-1531 Jewelry Exhibition —— 二分图匹配(最小覆盖点)的更多相关文章
- CSUOJ 1531 Jewelry Exhibition
Problem G Jewelry Exhibition To guard the art jewelry exhibition at night, the security agency has d ...
- csu 1552(米勒拉宾素数测试+二分图匹配)
1552: Friends Time Limit: 3 Sec Memory Limit: 256 MBSubmit: 723 Solved: 198[Submit][Status][Web Bo ...
- 【Learning】最小点覆盖(二分图匹配) 与Konig定理证明
(附一道例题) Time Limit: 1000 ms Memory Limit: 128 MB Description 最小点覆盖是指在二分图中,用最小的点集覆盖所有的边.当然,一个二分图的最小 ...
- 最短路&生成树&二分图匹配&费用流问题
最短路 题意理解,建图 https://vjudge.net/problem/UVALive-4128 飞机票+行程建图 https://vjudge.net/problem/UVALive-3561 ...
- UVA 12549 - 二分图匹配
题意:给定一个Y行X列的网格,网格种有重要位置和障碍物.要求用最少的机器人看守所有重要的位置,每个机器人放在一个格子里,面朝上下左右四个方向之一发出激光直到射到障碍物为止,沿途都是看守范围.机器人不会 ...
- POJ 1274 裸二分图匹配
题意:每头奶牛都只愿意在她们喜欢的那些牛栏中产奶,告诉每头奶牛愿意产奶的牛棚编号,求出最多能分配到的牛栏的数量. 分析:直接二分图匹配: #include<stdio.h> #includ ...
- BZOJ1433 ZJOI2009 假期的宿舍 二分图匹配
1433: [ZJOI2009]假期的宿舍 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2375 Solved: 1005[Submit][Sta ...
- HDU1281-棋盘游戏-二分图匹配
先跑一个二分图匹配,然后一一删去匹配上的边,看能不能达到最大匹配数,不能这条边就是重要边 /*----------------------------------------------------- ...
- HDU 1083 网络流之二分图匹配
http://acm.hdu.edu.cn/showproblem.php?pid=1083 二分图匹配用得很多 这道题只需要简化的二分匹配 #include<iostream> #inc ...
随机推荐
- linux jar 命令使用
原文链接:http://blog.chinaunix.net/uid-692788-id-2681136.html JAR包是Java中所特有一种压缩文档,其实大家就可以把它理解为.zip包.当然也是 ...
- 转:关于使用ImageMagick和Tesseract进行简单数字图像识别
据说Tesseract可是世界排名第三的OCR神器,2010年又更新了3.0版本.Tesseract原先是HP写的,现在Open Source了. 下面介绍怎么用Tesseract配合ImageMag ...
- mac下 JMeter 4.0 进行多用户接口压力测试
1.最近在做公司的内部系统,需要进行多用户压力测试,于是上网在官网下载了Jmeter 压缩包,并放在指定的目录解压,打开解压后文件夹到bin目录下: 执行sh jmeter Jmeter就启动起来了 ...
- 配置laravel的nginx站点
server{}配置 server{ #端口配置 listen 80; #域名配置 server_name laravel.cc; index index.php index.html index.h ...
- Oracle内部latch获取函数简介
标签: oracle call 函数 oracle statpack 转自: http://blog.51cto.com/458302/998775 Oracle的内部函数一直非常神秘,其 ...
- 什么叫PV,UV,PR值
1.PV PV(page view),即页面浏览量:用户每1次对网站中的每个网页访问均被记录1次.用户对同一页面的多次访问,访问量累计. 2.什么是UV uv(unique visitor),指访问某 ...
- 【spring cloud】spring cloud服务发现注解之@EnableDiscoveryClient与@EnableEurekaClient
spring cloud服务发现注解之@EnableDiscoveryClient与@EnableEurekaClient的区别
- CNN网络--VGGNet
Simonyan, Karen, and Andrew Zisserman. "Very deep convolutional networks for large-scale image ...
- [Algorithm] Write a Depth First Search Algorithm for Graphs in JavaScript
Depth first search is a graph search algorithm that starts at one node and uses recursion to travel ...
- HttpClient获取Cookie的两种方式
转载:http://blog.csdn.net/zhangbinu/article/details/72777620 一.旧版本的HttpClient获取Cookies p.s. 该方式官方已不推荐使 ...