poj1548(最小路径覆盖问题)

news/2024/7/7 19:06:50

题意:相当于给出N个坐标点,因为机器人只能向下或者向右走,所以如果能到达其他点,则连接这两个点,即line[i][j]=1
最小路径覆盖数:
对于一个DAG(有向无环图),选取最少条路径,使得每个 顶点属于且仅属于一条路径。路径长度可以为零;(有向图中找一些路径,使之覆盖了图中的所有顶点,就是任意一个顶点都跟那些路径中的某一条关联,且任何一个顶点有且只有一个与之关联)
最小路径覆盖数(最少边覆盖)=顶点数-最大匹配数
思路:把每个点都拆成两个点,分为入点和出点,如果 u 到 v 有边,那么我们就让 u 的入点连向 v 的出点 , 匈牙利 算出最大匹配。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdio>
using namespace std;
const int maxx=1005;
const int inf=0x3f3f3f3f;
int used[maxx];
int line[maxx][maxx];
int vis[maxx];
int n,m;
int e[maxx][maxx];
int nxt[maxx];
struct node{int x,y;
}num[maxx];
void init(){memset(used,0,sizeof(used));memset(vis,0,sizeof(vis));memset(line,0,sizeof(line));memset(nxt,-1,sizeof(nxt));for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){if(i==j){e[i][j]=0;}else{e[i][j]=inf;}}	}
}
void bfs(int n){for(int i=1;i<=n;i++){for(int j=i+1;j<=n;j++){if((num[j].x>=num[i].x)&&(num[j].y>=num[i].y)){line[i][j]=1;}}}
}
int Find(int x,int n){for(int i=1;i<=n;i++){if(used[i]==0&&line[x][i]==1){used[i]=1;if(nxt[i]==-1||Find(nxt[i],n)){nxt[i]=x;return true;}}}return false;
}
int match(int n){int sum=0;for(int i=1;i<=n;i++){memset(used,0,sizeof(used));if(Find(i,n)){sum++;}}return sum;
}
int main(){int t;while(scanf("%d %d",&n,&m)){if(n==-1&&m==-1)break;init();int i=1;num[i].x=n;num[i].y=m;while(scanf("%d %d",&n,&m)){if(n==0&&m==0)break;i++;num[i].x=n;num[i].y=m;}bfs(i);int ans=match(i);cout<<i-ans<<endl;} return 0;
}

http://lihuaxi.xjx100.cn/news/286072.html

相关文章

Prime Path(bfs)广度优先搜索

题目描述 The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on their offices. — It is a matter of security to change such things every now and the…

一些改进模型速度/精度的工程方法

点击上方“视学算法”&#xff0c;选择加"星标"或“置顶”重磅干货&#xff0c;第一时间送达作者&#xff1a;Captain Jackhttps://zhuanlan.zhihu.com/p/92025012本文已由作者授权&#xff0c;未经允许&#xff0c;不得二次转载一些自己的工作经验总结&#xff0c;用…

HDU4160(最小路径覆盖问题)

题意&#xff1a;当满足条件wi<wj,hi<hl和li<lj时&#xff0c;求解通过优化嵌套给定的娃娃可以获得的最外层洋娃娃的最小数量。 思路&#xff1a;如果嵌套的娃娃越多&#xff0c;则剩下的娃娃就越少&#xff0c;意味着单独出来的娃娃越少&#xff0c;转换为求解最小路…

线段树分治 ---- CF1217F - Forced Online Queries Problem(假离线 可撤销并查集 + 线段树分治)详解

题目链接 题目大意 解题思路&#xff1a; 我一开始想到可以用可撤销并查集去维护这种删边加边的操作&#xff0c;但是有个缺点是每次撤销都有把后面的边全部撤销复度是O(n2)O(n^2)O(n2) 首先我们考虑这种动态加边删边的问题&#xff0c;如果是离线的话&#xff0c;那就是线段…

秘籍 | 机器学习数据集网址大全

作者 | Will Badr译者 | Linstancy整理 | Jane出品 | AI科技大本营&#xff08;ID:rgznai100&#xff09;要找到一定特定的数据集可以解决各种机器学习问题&#xff0c;是一件很难的事情。越来越多企业或研究机构将自己的数据集公开&#xff0c;已经成为全球的趋势&#xff0c;…

第一个Servlet和Jsp

为什么80%的码农都做不了架构师&#xff1f;>>> 第一个Servlet和Jsp 开发Servlet有3种方法1.Servlet接口2.继承GenericServlet3.继承HttpServlet Tomcat 9部署Servlet 1.Tomcat的安装目录的webapps目录&#xff0c;可以看到ROOT&#xff0c;examples, tomcat-docs之…

Dungeon Master(bfs)广度优先搜索

描述 You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonal…

你的代码将会被GitHub埋在北极,保存1000年!

点击上方“视学算法”&#xff0c;选择加"星标"或“置顶”重磅干货&#xff0c;第一时间送达晓查 发自 凹非寺 本文转载自&#xff1a;量子位&#xff08;QbitAI&#xff09;你写的代码将被会被GitHub保存1000年。GitHub是不是疯了&#xff1f;有网友吐槽&#xff1a…