zoukankan      html  css  js  c++  java
  • POJ2594 最小路径覆盖

    题意:
          题意就是给你个有向无环图,问你最少放多少个机器人能把图全部遍历,机器人不能走回头路线。


    思路:

         如果直接建图,跑一遍二分匹配输出n - 最大匹配数会跪,原因是这个题目和以往见到的题目不一样的,区别就在,之前很多题目给的都是全边,就是假如 a->b b->c ,那么他一定会给你一条 a->c,因为a->c也是有指向关系的,而这个题目就没有给a->c,这就需要我们自己去找到所有可达边,一遍Floyd或者深搜都行,深搜是O(n^2)的,会快一点。给你在网上找的例子。



    #include<stdio.h>
    #include<string.h>
    
    #define N_node 510
    #define N_edge 300000
    #define INF 100000000
    
    typedef struct
    {
       int to ,next;
    }STAR;
    
    STAR E[N_edge];
    int list[N_node] ,tot;
    int mk_dfs[N_node] ,mk_gx[N_node];
    int map[510][510];
    
    void add(int a ,int b)
    {
       E[++tot].to = b;
       E[tot].next = list[a];
       list[a] = tot;
    }
    
    int DFS_XYL(int x)
    {
       for(int k = list[x] ;k ;k = E[k].next)
       {
          int to = E[k].to;
          if(mk_dfs[to]) continue;
          mk_dfs[to] = 1;
          if(mk_gx[to] == -1 || DFS_XYL(mk_gx[to]))
          {
             mk_gx[to] = x;
             return 1;
          }
       }
       return 0;
    }
    
    int minn(int x ,int y)
    {
       return x < y ? x : y;
    }
    
    void Floyd(int n)
    {
       for(int k = 1 ;k <= n ;k ++)
       for(int i = 1 ;i <= n ;i ++)
       for(int j = 1 ;j <= n ;j ++)
       map[i][j] = minn(map[i][j] ,map[i][k] + map[k][j]);
    }
    
    int main ()
    {
       int n ,m ,i ,j;
       int a ,b;
       while(~scanf("%d %d" ,&n ,&m) && n + m)
       {
          for(i = 1 ;i <= n ;i ++)
          for(j = 1 ;j <= n ;j ++)
          if(i == j) map[i][j] = 0;
          else map[i][j] = INF;
          for(i = 1 ;i <= m ;i ++)
          {
             scanf("%d %d" ,&a ,&b);
             map[a][b] = 1;
          }
          Floyd(n);
          memset(list ,0 ,sizeof(list));
          tot = 1;
          for(i = 1 ;i <= n ;i ++)
          for(j = 1 ;j <= n ;j ++)
          {
             if(i == j || map[i][j] == INF) continue;
             add(i ,j);
          }
          int sum = 0;
          memset(mk_gx ,255 ,sizeof(mk_gx));
          for(i = 1 ;i <= n ;i ++)
          {
             memset(mk_dfs ,0 ,sizeof(mk_dfs));
             sum += DFS_XYL(i);
          }
          printf("%d
    " ,n - sum);
       }
       return 0;
    }


  • 相关阅读:
    cnblogs blogStats All In One
    ESLint & vue template indent validate All In One
    vue & elementui 表单验证 bug All In One
    vue 表单验证 rule.message bug All In One
    vue 表单验证 rule message bug All In One
    Node.js & TypeScript error All In One
    VS2010如何调试IIS上的网站
    用LINQ查询XML并绑定给GridView显示
    SQLServer2008评估期已过解决方法
    ASP.NET给用户控件(.ascx)增加属性
  • 原文地址:https://www.cnblogs.com/csnd/p/12063067.html
Copyright © 2011-2022 走看看