poj2594Treasure Exploration【最小路徑覆蓋+floyd傳遞閉包】

MissZhou要努力發表於2016-04-19

Description

Have you ever read any book about treasure exploration? Have you ever see any film about treasure exploration? Have you ever explored treasure? If you never have such experiences, you would never know what fun treasure exploring brings to you.
Recently, a company named EUC (Exploring the Unknown Company) plan to explore an unknown place on Mars, which is considered full of treasure. For fast development of technology and bad environment for human beings, EUC sends some robots to explore the treasure.
To make it easy, we use a graph, which is formed by N points (these N points are numbered from 1 to N), to represent the places to be explored. And some points are connected by one-way road, which means that, through the road, a robot can only move from one end to the other end, but cannot move back. For some unknown reasons, there is no circle in this graph. The robots can be sent to any point from Earth by rockets. After landing, the robot can visit some points through the roads, and it can choose some points, which are on its roads, to explore. You should notice that the roads of two different robots may contain some same point.
For financial reason, EUC wants to use minimal number of robots to explore all the points on Mars.
As an ICPCer, who has excellent programming skill, can your help EUC?

Input

The input will consist of several test cases. For each test case, two integers N (1 <= N <= 500) and M (0 <= M <= 5000) are given in the first line, indicating the number of points and the number of one-way roads in the graph respectively. Each of the following M lines contains two different integers A and B, indicating there is a one-way from A to B (0 < A, B <= N). The input is terminated by a single line with two zeros.

Output

For each test of the input, print a line containing the least robots needed.

Sample Input

1 0
2 1
1 2
2 0
0 0

Sample Output

1
1
2

看到這個題想到是最小路徑覆蓋了,一頓亂套模板,也想到是單向加邊了,但是floyd閉包頭一次聽說並且使用(讓我想起來JS的閉包了==)就是i->j  j->k  求得i->k

    #include<cstdio>
    #include<cstring>
    using namespace std;
    /* **************************************************************************
    //二分圖匹配(匈牙利演算法的DFS實現)
    //初始化:g[][]兩邊頂點的劃分情況
    //建立g[i][j]表示i->j的有向邊就可以了,是左邊向右邊的匹配
    //g沒有邊相連則初始化為0
    //uN是匹配左邊的頂點數,vN是匹配右邊的頂點數
    //呼叫:res=hungary();輸出最大匹配數
    //優點:適用於稠密圖,DFS找增廣路,實現簡潔易於理解
    //時間複雜度:O(VE)
    //***************************************************************************/
    //頂點編號從0開始的
    const int MAXN=509;
    int n;//u,v數目
    int g[MAXN][MAXN];
    int linker[MAXN];
    bool used[MAXN];
  //  int k,m,n,u,v;
    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)
                {
                    if(i != j && g[i][k] && g[k][j])
                        g[i][j] = 1;
                }
            }
        }
    }
    bool dfs(int u)//從左邊開始找增廣路徑
    {
        int v;
        for(v=1;v<=n;v++)//這個頂點編號從0開始,若要從1開始需要修改
          if(g[u][v]&&!used[v])
          {
              used[v]=true;
              if(linker[v]==-1||dfs(linker[v]))
              {//找增廣路,反向
                  linker[v]=u;
                  return true;
              }
          }
        return false;//這個不要忘了,經常忘記這句
    }
    int hungary()
    {
        int res=0;
        int u;
        memset(linker,-1,sizeof(linker));
        for(u=1;u<=n;u++)
        {
            memset(used,0,sizeof(used));
            if(dfs(u)) res++;
        }
        return res;
    }
    //******************************************************************************/

    int main()
    {
        //freopen("cin.txt","r",stdin);
        int m;
        while(~scanf("%d%d",&n,&m))
        {
            if(n==0&&m==0) break;
         //   printf("n=%d,m=%d\n",n,m);
            if(m==0) {printf("%d\n",n);continue;}
            memset(g,0,sizeof(g));
            while(m--)
            {
                int u,v;
                scanf("%d%d",&u,&v);
                g[u][v]=1;
            }
            Floyd(n);
            printf("%d\n",n-hungary());
        }
        return 0;
    }


相關文章