POJ 2594--treasure Exploration —————— "Minimum path overlay, focus, Floyd delivery Closure"

Source: Internet
Author: User

Treasure ExplorationTime limit:6000MS Memory Limit:65536KB 64bit IO Format:%i64d &%i64 U SubmitStatusPracticePOJ 2594

Description

Has a ever read any book about treasure exploration? Has the ever see any film about treasure exploration? Have you ever explored treasure? If you never has 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 Considere D Full of treasure. For fast development of technology and ill environment for human beings, EUC sends some robots to explore the treasure.
To make it easy, we use a graph, which was formed by N points (these n points be numbered from 1 to N), to represent the P Laces to be explored. And some points is connected by one-way road, which means., through the road, a robot can only move from one end to T He and end, but cannot move back. For some unknown reasons, there are no circle in this graph. The robots can 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 is on its roads, T o Explore. You should notice so the roads of the different robots may contain some point.
For financial reason, EUC wants to use minimal number of robots to explore all the points on Mars.
As an icpcer, who have excellent programming skill, can your help EUC?

Input

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

Output

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

Sample Input

1 02 11 22 00 0

Sample Output

112
Main topic: Multiple sets of data. Each group gives n,m. Indicates that there are n points, and m bars have a forward edge. Let the robot access N points, each robot cannot backtrack, but different robots can walk through the points of other robots. Ask how many robots you need at least. Problem solving: Minimum path coverage. First, the closure is delivered with Floyd. Then run the maximum match again. Example: 5 4 1 2 3 2 2 4 2 5 in accordance with the general minimum path coverage, then 3 robots are required, but after Floyd delivery closure, in fact only 2 can be.
#include <stdio.h> #include <string.h> #include <math.h> #include <queue> #include <vector > #include <algorithm>using namespace std;const int maxn = 600;const int INF = 0X3F3F3F3F;VECTOR&LT;INT&GT;G[MAXN ];int MX[MAXN], MY[MAXN], DX[MAXN], DY[MAXN], USED[MAXN], dis;int map[maxn][maxn];bool searchp (int _n) {Queue<int&gt ;    Q;    memset (dx,-1,sizeof (DX));    memset (dy,-1,sizeof (dy));    int dis = INF;            for (int i = 1; I <= _n; i++) {if (mx[i] = = 1) {Dx[i] = 0;        Q.push (i);    }} int v; while (! Q.empty ()) {int u = q.front ();        Q.pop ();        if (Dx[u] > dis) break;            for (int i = 0; i < g[u].size (); i++) {v = g[u][i];                if (dy[v] = = 1) {Dy[v] = Dx[u] + 1;                if (my[v] = =-1) {dis = dy[v];                    }else{Dx[my[v]] = Dy[v] + 1;                Q.push (My[v]); }}}} return dis! = INF;}    int dfs (int u) {int V;        for (int i = 0; i < g[u].size (); i++) {v = g[u][i];            if (!used[v] && dy[v] = = Dx[u] + 1) {used[v] = 1;            if (my[v]! =-1 && dy[v] = = dis) {continue;                } if (my[v] = = 1 | | DFS (MY[V])) {Mx[u] = V;                MY[V] = u;            return true; }}} return false;}    int maxmatch (int ln,int rn) {int ret = 0;    memset (mx,-1,sizeof (Mx));    memset (my,-1,sizeof (My));        while (SEARCHP (LN)) {memset (used,0,sizeof (used));            for (int i = 1; i <= ln; i++) {if (mx[i] = = 1 && dfs (i)) {ret++; }}} return ret;}    int main () {int T, cas = 0, n, m, N, M, K;        while (scanf ("%d%d", &n,&m)!=eof&& (n+m)) {for (int i = 0; I <= N; i++) {g[i].clear ();        } int A, b;        memset (map,0,sizeof (MAP)); for (int i = 1; I <= M            i++) {scanf ("%d%d", &a,&b);        MAP[A][B] = 1; } for (int k = 1, k <= N; k++) {for (int i = 1, i <= N; i++) {for (int j = 1; J <= N J + +) {Map[i][j] = map[i][j]| |                map[i][k]&&map[k][j]; }}} for (int i = 1, i <= N; i++) {for (int j = 1; J <= N; j + +) {I                F (Map[i][j]) {g[i].push_back (j);        }}} n = m = n;        int res = Maxmatch (n,m);    printf ("%d\n", n-res); } return 0;}

  

POJ 2594--treasure Exploration —————— "Minimum path overlay, focus, Floyd delivery Closure"

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.