zoukankan      html  css  js  c++  java
  • BZOJ2535 [Noi2010]Plane 航空管制2

    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
     
     
     
     
    正解:拓扑排序+贪心
    解题报告:
      跟刚才那道题一样的,移步BZOJ2109
     
      1 //It is made by jump~
      2 #include <iostream>
      3 #include <cstdlib>
      4 #include <cstring>
      5 #include <cstdio>
      6 #include <cmath>
      7 #include <algorithm>
      8 #include <ctime>
      9 #include <vector>
     10 #include <queue>
     11 #include <map>
     12 #include <set>
     13 #ifdef WIN32   
     14 #define OT "%I64d"
     15 #else
     16 #define OT "%lld"
     17 #endif
     18 using namespace std;
     19 typedef long long LL;
     20 const int MAXN = 2011;
     21 const int MAXM = 10011;
     22 int n,m;
     23 int w[MAXN];
     24 int first[MAXN],next[MAXM],to[MAXM];
     25 int ecnt;
     26 bool vis[MAXN];
     27 int top;
     28 int dui[MAXN];
     29 int mp[MAXN][MAXN];
     30 
     31 struct ljh{
     32     int val,jilu;
     33 }a[MAXN];
     34 
     35 inline int getint()
     36 {
     37        int w=0,q=0;
     38        char c=getchar();
     39        while((c<'0' || c>'9') && c!='-') c=getchar();
     40        if (c=='-')  q=1, c=getchar();
     41        while (c>='0' && c<='9') w=w*10+c-'0', c=getchar();
     42        return q ? -w : w;
     43 }
     44 
     45 inline void link(int x,int y){ next[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=y; }
     46 
     47 inline void topo_sort(int x){ 
     48     vis[x]=1;
     49     for(int i=first[x];i;i=next[i]) {
     50     int v=to[i];
     51     if(!vis[v]) {
     52         //w[v]=min(w[v],w[x]-1);
     53         topo_sort(v);
     54     }
     55     }  
     56     dui[++top]=x;
     57 } 
     58 
     59 inline bool cmp(ljh q,ljh qq){ return q.val<qq.val; }
     60 
     61 inline void make(){
     62     for(int i=n;i>=1;i--) {
     63     int u=dui[i];
     64     for(int j=first[u];j;j=next[j]) {
     65         w[to[j]]=min(w[u]-1,w[to[j]]);
     66     }
     67     }
     68 
     69     for(int i=1;i<=n;i++) a[i].val=w[i],a[i].jilu=i;
     70     sort(a+1,a+n+1,cmp);
     71     for(int i=1;i<=n;i++) {
     72     printf("%d ",a[i].jilu);
     73     }
     74     printf("
    ");
     75 }
     76 
     77 inline void dfs(int x,int now){
     78     vis[now]=1;
     79     mp[x][now]=1;
     80     for(int i=first[now];i;i=next[i]) {
     81     int v=to[i];
     82     if(!vis[v]) {        
     83         dfs(x,v);
     84     }
     85     }
     86 }
     87 
     88 inline void go(int x){
     89     int j=n;
     90     for(int i=n;i>=1;i--){
     91     int v=a[i].jilu;
     92     if(mp[x][v]==0 && w[v]>=j) j--;
     93     else if(w[v]<j) break;
     94     }
     95     printf("%d ",j);
     96 }
     97 
     98 inline void work(){
     99     n=getint(); m=getint();
    100     for(int i=1;i<=n;i++) w[i]=getint();
    101     int x,y;
    102     for(int i=1;i<=m;i++) {
    103     x=getint(); y=getint();
    104     link(y,x);
    105     }
    106     for(int i=1;i<=n;i++) if(!vis[i]) topo_sort(i);
    107     make();
    108     for(int i=1;i<=n;i++) memset(vis,0,sizeof(vis)),dfs(i,i);
    109     for(int i=1;i<=n;i++) go(i);
    110 }
    111 
    112 int main()
    113 {
    114   work();
    115   return 0;
    116 }
     
  • 相关阅读:
    Python tutorial阅读之基本数据结构
    Leetcode:Merge Sorted Array
    Python tutorial阅读之函数的定义与使用
    LeetCode:3Sum
    Python tutorial阅读之Python基本运算与基本变量
    Python tutorial阅读之使用 Python 解释器
    LeetCode:Longest Substring Without Repeating Characters
    如何过好幸福且富裕的一生----查理芒格
    创业学习---《调研黑客上:锁定调研目标》--D-2.调研模块---HHR计划---以太一堂
    创业学习---《如何展开竞争情报调研》--D-1.调研模块---HHR计划---以太一堂
  • 原文地址:https://www.cnblogs.com/ljh2000-jump/p/5663072.html
Copyright © 2011-2022 走看看