Original question connection: Click to open the link
Question :......
Idea: The minimum path coverage for binary matching. In a directed graph without loops, find the minimum path to overwrite all nodes, and each node can only overwrite once.
Overwrite all vertices of directed acyclic graph (DAG) g with a few non-Intersecting simple paths.
To solve this problem, you can create a bipartite graph model. Split all vertex I into two: I in the x node set and I in the Y node set. If edge I-> J exists, edge I-> J is introduced in the bipartite graph ', if the maximum matching value of a bipartite graph is m, the result is n-M.
Remember an important conclusion: the least path overwrite count of the DAC graph = number of nodes (N)-maximum match count
Code:
#include<cstdio>#include<cstring>#include<queue>#include<vector>#include<cmath>#include<iostream>#include<algorithm>using namespace std;vector<int>V[1000];int link[1000],use[1000];void init(int n){ int i; for(i=0;i<=n;i++) V[i].clear();}bool Dfs(int v){ int i,j,k; for(i=0;i<V[v].size();i++) { k=V[v][i]; if(!use[k]) { use[k]=1; if(!link[k]||Dfs(link[k])) { link[k]=v; return true; } } } return false;}struct hh{ int x; int y; int x1,x2,y1,y2;}map[1000];int MaxMatch(int n){ int i,j,ans=0; memset(link,0,sizeof(link)); for(i=1;i<=n;i++) { memset(use,0,sizeof(use)); if(Dfs(i)) ans++; } return ans;}int Find(int i,int j,int x,int y){ return abs(i-j)+abs(x-y);}int main(){ int i,j,n,m,k,t; scanf("%d",&t); while(t--) { int x1,y1,x2,y2; scanf("%d",&n); init(n); for(i=1;i<=n;i++) { scanf("%d:%d %d%d%d%d",&m,&k,&map[i].x1,&map[i].y1,&map[i].x2,&map[i].y2); map[i].x=m*60+k; map[i].y=map[i].x+Find(map[i].x1,map[i].x2,map[i].y1,map[i].y2); } for(i=1;i<n;i++) { for(j=i+1;j<=n;j++) if(map[i].y+Find(map[i].x2,map[j].x1,map[i].y2,map[j].y1)<map[j].x) V[i].push_back(j); } int ans=MaxMatch(n); printf("%d\n",n-ans); }}