[POJ2594 Treasure Exploration DAG covering toward the minimum path may intersect] FIG.

Topic links: http://poj.org/problem?id=2594

Treasure Exploration
Time Limit: 6000MS   Memory Limit: 65536K
Total Submissions:10480   Accepted: 4250

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.
Subject to the effect: given a directed graph, the minimum required number of robots can go through all the points.
Ideas:
1. Because it is reusable passing point, the path is intersected. Seeking the minimum number of vertices path coverage = picture - largest number of matched new FIG.
2. For the minimum path may intersect the coverage problem, at first with floyd seek transitive closure. Conversion coating is to find the shortest path not intersect.
code show as below:
 1 #include<stdio.h>
 2 #include<string.h>
 3 #define mem(a, b) memset(a, b, sizeof(a))
 4 const int MAXN = 550;
 5 const int MAXM = 5100;
 6 
 7 int n, m; //n个点 m条有向边 
 8 int line[MAXN][MAXN], used[MAXN], master[MAXN];
 9 
10 void floyd()
11 {
12     for(int i = 1; i <= n; i ++)
13         for(int j = 1; j <= n; j ++)
14             for(int k = 1; k <= n; k ++)
15                 if(line[i][k] && line[k][j])
16                     line[i][j] = 1;
17 }
18 
19 int find(int x)
20 {
21     for(int i = 1; i <= n; i ++) //y部的点 
22     {
23         if(line[x][i] && used[i] == -1)
24         {
25             used[i] = 1;
26             if(master[i] == -1 || find(master[i]))
27             {
28                 master[i] = x;
29                 return 1;
30             }
31         }
32     }
33     return 0;
34 }
35 
36 int main()
37 {
38     while(scanf("%d%d", &n, &m) != EOF)
39     {
40         if(n == 0 && m == 0)
41             break;
42         int cnt = 0;
43         mem(line, 0), mem(master, -1);
44         for(int i = 1; i <= m; i ++)
45         {
46             int a, b;
47             scanf("%d%d", &a, &b);
48             line[a][b] = 1;
49          }
 50          Floyd ();
 51 is          for ( int I = . 1 ; I <= n-; I ++) // point x portion 
52 is          {
 53 is              MEM (Used, - . 1 );
 54 is              IF (Find (I))  
 55                  ++ CNT; // maximum number of matches 
56 is          }
 57 is          the printf ( " % D \ n- " , n-- CNT);
 58      }
 59      return  0 ;
 60 }
View Code

 

Guess you like

Origin www.cnblogs.com/yuanweidao/p/11481946.html