zoukankan      html  css  js  c++  java
  • [NOI 2010]航空管制

    Description

    世博期间,上海的航空客运量大大超过了平时,随之而来的航空管制也频频发生。最近,小X就因为航空管制,连续两次在机场被延误超过了两小时。对此,小X表示很不满意。 在这次来烟台的路上,小 X不幸又一次碰上了航空管制。于是小 X开始思考关于航空管制的问题。 假设目前被延误航班共有 n个,编号为 1至n。机场只有一条起飞跑道,所有的航班需按某个顺序依次起飞(称这个顺序为起飞序列)。定义一个航班的起飞序号为该航班在起飞序列中的位置,即是第几个起飞的航班。 起飞序列还存在两类限制条件:  第一类(最晚起飞时间限制):编号为 i的航班起飞序号不得超过 ki;  第二类(相对起飞顺序限制):存在一些相对起飞顺序限制(a, b),表示航班 a的起飞时间必须早于航班 b,即航班 a的起飞序号必须小于航班 b 的起飞序号。 小X 思考的第一个问题是,若给定以上两类限制条件,是否可以计算出一个可行的起飞序列。第二个问题则是,在考虑两类限制条件的情况下,如何求出每个航班在所有可行的起飞序列中的最小起飞序号。

    Input

    第一行包含两个正整数 n和m,n表示航班数目,m表示第二类限制条件(相对起飞顺序限制)的数目。 第二行包含 n个正整数 k1, k2, „, kn。 接下来 m行,每行两个正整数 a和b,表示一对相对起飞顺序限制(a, b),其中1≤a,b≤n, 表示航班 a必须先于航班 b起飞。

    Output

    由两行组成。 
    第一行包含 n个整数,表示一个可行的起飞序列,相邻两个整数用空格分隔。

    输入数据保证至少存在一个可行的起飞序列。如果存在多个可行的方案,输出任意一个即可。 

    第二行包含 n个整数 t1, t2, „, tn,其中 ti表示航班i可能的最小起飞序号,相邻两个整数用空格分隔。

    Sample Input

    5 5
    4 5 2 5 4
    1 2
    3 2
    5 1
    3 4
    3 1

    Sample Output

    3 5 1 4 2
    3 4 1 2 1

    题解(转载)

    ->原文地址<-

    正解:贪心+堆。

    第一问很简单,反向拓扑序+大根堆,然后从后往前依次填序号就行。

    第二问其实也不难。和第一问一样,只要我们把当前这个点卡住,不对它进行任何操作,当我们发现堆中取出的点没有办法再标号时,那这个标号就是询问点的最小标号。

    (luogu大牛分站开O2可以过)

     1 //It is made by Awson on 2017.11.1
     2 #include <map>
     3 #include <set>
     4 #include <cmath>
     5 #include <ctime>
     6 #include <queue>
     7 #include <stack>
     8 #include <cstdio>
     9 #include <string>
    10 #include <vector>
    11 #include <cstdlib>
    12 #include <cstring>
    13 #include <iostream>
    14 #include <algorithm>
    15 #define LL long long
    16 #define Max(a, b) ((a) > (b) ? (a) : (b))
    17 #define Min(a, b) ((a) < (b) ? (a) : (b))
    18 #define Abs(a) ((a) < 0 ? (-(a)) : (a))
    19 using namespace std;
    20 const int N = 2000;
    21 const int M = 10000;
    22 
    23 int n, m, u, v;
    24 int k[N+5], kp[N+5];
    25 struct tt {
    26     int to, next;
    27 }edge[M+5];
    28 int path[N+5], top;
    29 int ans[N+5];
    30 struct node {
    31     int x, k;
    32     node() {
    33     }
    34     node (int _x, int _k) {
    35     x = _x; k = _k;
    36     }
    37     bool operator < (const node &b) const {
    38     return k < b.k;
    39     }
    40 }; priority_queue<node>Q;
    41 int in[N+5];
    42 
    43 void add(int u, int v) {
    44     edge[++top].to = v;
    45     edge[top].next = path[u];
    46     path[u] = top;
    47 }
    48 void topsort(int lim) {
    49     while (!Q.empty()) Q.pop();
    50     for (int i = 1; i <= n; i++) {
    51     in[i] = kp[i];
    52     if (kp[i] == 0 && i != lim) Q.push(node(i, k[i]));
    53     }
    54     for (int cnt = n; cnt >= 1; cnt--) {
    55     if (Q.empty()) {
    56         printf("%d ", cnt); return;
    57     }
    58     if (Q.top().k < cnt) {
    59         printf("%d ", cnt); return;
    60     }
    61     ans[cnt] = Q.top().x; Q.pop();
    62     for (int i = path[ans[cnt]]; i; i= edge[i].next) {
    63         in[edge[i].to]--;
    64         if (in[edge[i].to] == 0) {
    65         if (edge[i].to != lim) Q.push(node(edge[i].to, k[edge[i].to]));
    66         }
    67     }
    68     }
    69 }
    70 void work() {
    71     scanf("%d%d", &n, &m);
    72     for (int i = 1; i <= n; i++) scanf("%d", &k[i]);
    73     for (int i = 1; i <= m; i++) {
    74     scanf("%d%d", &u, &v); add(v, u); kp[u]++;
    75     }
    76     topsort(0);
    77     for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
    78     printf("
    ");
    79     for (int i = 1; i <= n; i++) topsort(i);
    80 }
    81 int main() {
    82     work();
    83     return 0;
    84 }
  • 相关阅读:
    字符串的长度 -- js
    导入drupal中文语言包
    ubuntu修改iP地址
    人生需要苦难和敌人
    Drupal.theme未解之谜
    如何定义带下标的js数组
    smtp admin email 似乎可以考虑在
    js中的apply 和 call
    js 点号 中括号
    代码调试
  • 原文地址:https://www.cnblogs.com/NaVi-Awson/p/7768151.html
Copyright © 2011-2022 走看看