D - Plane 航空管制2

 HYSBZ - 2535 

世博期间,上海的航空客运量大大超过了平时,随之而来的航空管制也频频发生。最近,小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

 

思路:

对于第一个问题,如果想要获得合法的拓扑排序,仅仅直接根据限制1进行排序,然后根据入度为0的 点进行拓扑排序就行,但是我们这里不这么做,我们用下面的方法

对于第2 个问题,需要给出每一个点的可以出发的最早时间,这里的思路是建立反向边,然后对于限制 最晚在x 号出发改为

最早 为n-x 号出发,然后我们再对每个节点进行排序,每次都选择最早出发时间最小的。

要求的每个节点的最早位置,可以通过将其忽视,在拓扑排序的时候看看能够拓扑出多少个点,最终的结果就是N-x 为这个点的最早出发位置

详细看代码,主要用了STL,提高代码的可读性

 

#include<iostream>
#include<queue>
#include<cstring>
#include<vector>
#include<cstdio>
using namespace std;
const int MAXN=30000;

//节点结构体, 
struct node{
	int v;//节点号 
	int limit;//限制 
	node(){}; 
	node(int v,int limit):v(v),limit(limit){};
	bool operator <(const node & b)const{ //按越早的越好的顺序 
		return this->limit>b.limit;
	}
};

//定义总优先队列,用于后续拷贝使用 
priority_queue<node> p_que;
int N,M;
//邻接表,存放图的拓扑关系 
vector<int> map[MAXN];
//入度 
int deg[MAXN];
//暂时存放的入度 
int temp_deg[MAXN];
//合法序列的存放地点 
vector <int > ans_list;
//对应的 节点映射 
node arr[MAXN];
int slove(int x){
	if(!x)
		ans_list.clear();
	memcpy(temp_deg,deg,sizeof(deg));
	priority_queue<node> temp_que = p_que;

	priority_queue<node> que_;
	
	//先找到入度为0 的,并按照最早靠前的顺序进行排序 
	while(!temp_que.empty()){
		node temp_node = temp_que.top(); temp_que.pop();
//		if(temp_node.limit >count)continue;

		int u = temp_node.v;
		//屏蔽掉点x 
		if(u == x) continue;
		if(!temp_deg[u]) 
			que_.push(arr[u]);
	}
	//当前顺序的限制,就是最早的顺序一定比当前顺序小 
	int count=0;
	//能够拓扑的点的数量,那么答案就是 N-ans  就是这个飞机最早的位置 
	int ans = 0;
	while(!que_.empty()){
		node temp_node = que_.top(); que_.pop();
		if(temp_node.limit >count)continue; count++;

		int vv = temp_node.v;
		if(!x)
			ans_list.push_back(vv); 
		ans ++;
		for(int i=0;i<map[vv].size();i++){
			int v = map[vv][i];
			if(v == x) continue;
			temp_deg[v]--;
			if(!temp_deg[v])
				que_.push(arr[v]);				
		}
	}
	return ans;
}

int main(){
	scanf("%d%d",&N,&M);
	memset(deg,0,sizeof(deg));
	for(int i=1;i<=N;i++){
		int limit;
		scanf("%d",&limit);
		node temp_node = node(i,N-limit);
		arr[i] = temp_node;
		p_que.push(temp_node);
	}
	
	for(int i=1;i<=M;i++){
		int a,b;
		scanf("%d%d",&a,&b);
		map[b].push_back(a);
		deg[a]++;
	}
	
	slove(0);
	for(int i=ans_list.size()-1;i>=0;i--)
		printf(i==ans_list.size()-1?"%d":" %d",ans_list[i]);
	printf("\n");
	
	for(int i=1;i<=N;i++)
		printf(i==1?"%d":" %d",N-slove(i));
	printf("\n");
	
	return 0;
}