Hdu oj 1350 taxi cab Scheme [minimum path Overwrite for Bipartite Graph Matching]

Source: Internet
Author: User

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);    }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.