zoukankan      html  css  js  c++  java
  • hdu 4160(最小路径覆盖)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4160

    思路:最小路径覆盖,如果满足条件:wi < wj , li < lj , and hi < hj,那么i->j连边,然后就是求最大匹配。

    最小路径覆盖=顶点数-最大匹配。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 using namespace std;
     6 struct Node{
     7    int wi,hi,li;
     8 }node[555];
     9 bool map[555][555];
    10 bool mark[555];
    11 int ly[555];
    12 int n;
    13 
    14 int cmp(const Node &p,const Node &q){
    15    if(p.wi!=q.wi)return p.wi<q.wi;
    16    else if(p.li!=q.li)return p.li<q.li;
    17    return p.hi<q.hi;
    18 }
    19 
    20 int dfs(int u)
    21 {
    22    for(int v=0;v<n;v++){
    23       if(map[u][v]&&!mark[v]){
    24          mark[v]=true;
    25          if(ly[v]==-1||dfs(ly[v])){
    26             ly[v]=u;
    27             return 1;
    28          }
    29       }
    30    }
    31    return 0;
    32 }
    33 
    34 int MaxMatch()
    35 {
    36    int res=0;
    37    memset(ly,-1,sizeof(ly));
    38    for(int i=0;i<n;i++){
    39       memset(mark,false,sizeof(mark));
    40       res+=dfs(i);
    41    }
    42    return res;
    43 }
    44 
    45 int main(){
    46  //  freopen("1.txt","r",stdin);
    47    while(scanf("%d",&n),n){
    48       for(int i=0;i<n;i++){
    49          scanf("%d%d%d",&node[i].wi,&node[i].li,&node[i].hi);
    50       }
    51       sort(node,node+n,cmp);
    52       memset(map,false,sizeof(map));
    53       for(int i=0;i<n;i++){
    54          for(int j=i+1;j<n;j++){
    55             if(node[i].wi<node[j].wi&&node[i].li<node[j].li&&node[i].hi<node[j].hi)
    56                map[i][j]=true;
    57          }
    58       }
    59       int ans=MaxMatch();
    60       printf("%d\n",n-ans);
    61    }
    62    return 0;
    63 }
    View Code
  • 相关阅读:
    第三章-套接字编程
    unix网络编程第四章----基于TCP套接字编程
    unix网络编程第一章demo
    unix网络编程第2章
    论epoll的实现
    Select函数实现
    函数式编程语言
    Git
    python库-urllib
    PIL处理图片信息
  • 原文地址:https://www.cnblogs.com/wally/p/3122921.html
Copyright © 2011-2022 走看看