UVaOJ 10305 - Ordering Tasks


——by A Code Rabbit


Description

有很多很多的任务,任务之间有优先性,意味着有时你想做某个任务,必须先完成它的前置任务。请你去安排完成任务的顺序。

输入任务的数量和任务之间的优先关系。

输出一种符合条件的完成任务的顺序。


Types

Date Structure :: Graphs


Analysis

把任务看成一个点,任务之间的关系看成边,构造一个有向图。

遍历这个图即可。

可以暴力枚举,也可以拓扑排序,其实时间复杂度是一样的。


Solution

1. 暴力

// UVaOJ 10305
// Ordering Tasks
// by A Code Rabbit

#include <cstdio>
#include <cstring>

const int LIMITS_N = 123;

int n, m;

bool graph[LIMITS_N][LIMITS_N];
/* the graph means direct precedence relations */

bool is_finished[LIMITS_N];

bool Check(int pos);

int main() {
    while (scanf("%d%d", &n, &m), n || m) {
        // INIT.
        memset(graph, false, sizeof(graph));
        memset(is_finished, false, sizeof(is_finished));
        // Inputs.
        for (int i = 0; i < m; ++i) {
            int u, v;
            scanf("%d%d", &u, &v);
            graph[u][v] = true;
        }      
        // Enumerate and outputs.
        bool is_first = true;
        int num_finished = 0;
        while (num_finished < n) {
            for (int i = 1; i <= n; ++i) {
                if (Check(i)) {
                    if (is_first) {
                        is_first = false;
                    } else {
                        printf(" ");
                    }
                    printf("%d", i);
                    is_finished[i] = true;
                    ++num_finished;
                }
            }
        }
        printf("\n");
    }

    return 0;
}

bool Check(int pos) {
    if (is_finished[pos]) {
        return false;
    }
    for (int i = 1; i <= n; ++i) {
        if (!is_finished[i] && graph[i][pos]) {
            return false;
        }
    }
    return true;
}

2. 拓扑排序

// UVaOJ 10305
// Ordering Tasks
// by A Code Rabbit

#include <cstdio>
#include <cstring>

const int LIMITS_N = 123;

int n, m;

bool graph[LIMITS_N][LIMITS_N];
int in_order[LIMITS_N];

bool is_finished[LIMITS_N];
int num_finished;

int main() {
    while (scanf("%d%d", &n, &m), n || m) {
        // INIT.
        memset(graph, false, sizeof(graph));
        memset(in_order, 0, sizeof(in_order));
        // Inputs.
        for (int i = 0; i < m; ++i) {
            int u, v;
            scanf("%d%d", &u, &v);
            graph[u][v] = true;
            ++in_order[v];
        }
        // Topo sort and outputs.
        memset(is_finished, false, sizeof(is_finished));
        num_finished = 0;
        bool is_first = true;
        while (num_finished < n) {
            for (int i = 1; i <= n; ++i) {
                if (!is_finished[i] && !in_order[i]) {
                    if (is_first) {
                        is_first = false;
                    } else {
                        printf(" ");
                    }
                    printf("%d", i);
                    is_finished[i] = true;
                    ++num_finished;
                    for (int j = 1; j <= n; ++j) {
                        if (graph[i][j]) {
                            --in_order[j];
                        }
                    }
                }
            }
        }
        printf("\n");
    }

    return 0;
}




下载PDF

参考资料:无


你可能感兴趣的:(UVaOJ 10305 - Ordering Tasks)