题目链接: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 —— 二分图匹配(最小覆盖点)的更多相关文章

  1. CSUOJ 1531 Jewelry Exhibition

    Problem G Jewelry Exhibition To guard the art jewelry exhibition at night, the security agency has d ...

  2. csu 1552(米勒拉宾素数测试+二分图匹配)

    1552: Friends Time Limit: 3 Sec  Memory Limit: 256 MBSubmit: 723  Solved: 198[Submit][Status][Web Bo ...

  3. 【Learning】最小点覆盖(二分图匹配) 与Konig定理证明

    (附一道例题) Time Limit: 1000 ms   Memory Limit: 128 MB Description 最小点覆盖是指在二分图中,用最小的点集覆盖所有的边.当然,一个二分图的最小 ...

  4. 最短路&生成树&二分图匹配&费用流问题

    最短路 题意理解,建图 https://vjudge.net/problem/UVALive-4128 飞机票+行程建图 https://vjudge.net/problem/UVALive-3561 ...

  5. UVA 12549 - 二分图匹配

    题意:给定一个Y行X列的网格,网格种有重要位置和障碍物.要求用最少的机器人看守所有重要的位置,每个机器人放在一个格子里,面朝上下左右四个方向之一发出激光直到射到障碍物为止,沿途都是看守范围.机器人不会 ...

  6. POJ 1274 裸二分图匹配

    题意:每头奶牛都只愿意在她们喜欢的那些牛栏中产奶,告诉每头奶牛愿意产奶的牛棚编号,求出最多能分配到的牛栏的数量. 分析:直接二分图匹配: #include<stdio.h> #includ ...

  7. BZOJ1433 ZJOI2009 假期的宿舍 二分图匹配

    1433: [ZJOI2009]假期的宿舍 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2375  Solved: 1005[Submit][Sta ...

  8. HDU1281-棋盘游戏-二分图匹配

    先跑一个二分图匹配,然后一一删去匹配上的边,看能不能达到最大匹配数,不能这条边就是重要边 /*----------------------------------------------------- ...

  9. HDU 1083 网络流之二分图匹配

    http://acm.hdu.edu.cn/showproblem.php?pid=1083 二分图匹配用得很多 这道题只需要简化的二分匹配 #include<iostream> #inc ...

随机推荐

  1. 简单的makefile模板

    makefile不是总用到,每次用到的时候总要重新找资料,有点麻烦(怪自己基础知识不扎实,汗).留一个通用模板放这,方便以后使用 CC = gcc CXX = g++ LINK = g++ CFLAG ...

  2. SVG动画实践篇-字母切换

    git: https://github.com/rainnaZR/svg-animations/tree/master/src/pages/step2/letter.change 说明 这个页面实现了 ...

  3. 【Exception】查看异常出现在具体的文件名/类名/方法名/具体行号

    今天在处理异常日志保存过程中,想要获取到异常抛出在具体在那个文件,哪个类下的哪个方法中的具体第几行,所以具体实现如下 try{ Integer adminID = Integer.parseInt(a ...

  4. linux下添加自动启动项,linux 开机自动启动脚本方法

    #service servicename status是当前状态#chkconfig --list servicename是查看启动状态,也就是是否开机自动启动 首先写好脚本,如 mysql,把它放到 ...

  5. SilverLight:基础控件使用(1)

    ylbtech-SilverLight-Basic-Control:基础控件使用(1) 本文详解控件有: Label, TextBox, PasswordBox, Image, Button , Ra ...

  6. linux下的环境文件设置说明

    工作环境设置文件 环境设置文件有两种:系统环境设置文件 和 个人环境设置文件   1.系统中的用户工作环境设置文件:   登录环境设置文件:/etc/profile        非登录环境设置文件: ...

  7. Twitter网站架构分析介绍

    http://www.kaiyuanba.cn/html/1/131/147/7539.htm作为140个字的缔造者,twitter太简单了,又太复杂了,简单是因为仅仅用140个字居然使有几次世界性事 ...

  8. 手把手教你将本地项目文件上传至github

    相信大家都听过Git(分布式版本号控制系统)和github吧.没听过也没关系(Google一下),反正以后要去公司肯定会听过. 我是在今年年初才接触Git.之后就一发不可收拾.仅仅要有比較好的项目就G ...

  9. 车牌识别--OMAP4430处理器上測试

    OMAP4430(ME865) arm-linux-gcc 4.5.1(FriendlyARM) 软浮点执行结果: root@lj:/workspace/carid# arm-linux-gcc ca ...

  10. SQL Cursor生命周期

      阅读导航 1 Cursor Step 1.1 Create cursor 1.2 Parse statement 1.3 descript and define 1.4 Bind variable ...