zoukankan      html  css  js  c++  java
  • BZOJ4010 [HNOI2015]菜肴制作 【拓扑排序 + 贪心】

    题目

    知名美食家小 A被邀请至ATM 大酒店,为其品评菜肴。

    ATM 酒店为小 A 准备了 N 道菜肴,酒店按照为菜肴预估的质量从高到低给予
    1到N的顺序编号,预估质量最高的菜肴编号为1。由于菜肴之间口味搭配的问题,
    某些菜肴必须在另一些菜肴之前制作,具体的,一共有 M 条形如“i 号菜肴‘必须’
    先于 j 号菜肴制作”的限制,我们将这样的限制简写为<i,j>。现在,酒店希望能求
    出一个最优的菜肴的制作顺序,使得小 A能尽量先吃到质量高的菜肴:也就是说,
    (1)在满足所有限制的前提下,1 号菜肴“尽量”优先制作;(2)在满足所有限制,1
    号菜肴“尽量”优先制作的前提下,2号菜肴“尽量”优先制作;(3)在满足所有限
    制,1号和2号菜肴“尽量”优先的前提下,3号菜肴“尽量”优先制作;(4)在满
    足所有限制,1 号和 2 号和 3 号菜肴“尽量”优先的前提下,4 号菜肴“尽量”优
    先制作;(5)以此类推。
    例1:共4 道菜肴,两条限制<3,1>、<4,1>,那么制作顺序是 3,4,1,2。例2:共
    5道菜肴,两条限制<5,2>、 <4,3>,那么制作顺序是 1,5,2,4,3。例1里,首先考虑 1,
    因为有限制<3,1>和<4,1>,所以只有制作完 3 和 4 后才能制作 1,而根据(3),3 号
    又应“尽量”比 4 号优先,所以当前可确定前三道菜的制作顺序是 3,4,1;接下来
    考虑2,确定最终的制作顺序是 3,4,1,2。例 2里,首先制作 1是不违背限制的;接
    下来考虑 2 时有<5,2>的限制,所以接下来先制作 5 再制作 2;接下来考虑 3 时有
    <4,3>的限制,所以接下来先制作 4再制作 3,从而最终的顺序是 1,5,2,4,3。
    现在你需要求出这个最优的菜肴制作顺序。无解输出“Impossible!” (不含引号,
    首字母大写,其余字母小写)

    输入格式

    第一行是一个正整数D,表示数据组数。

    接下来是D组数据。
    对于每组数据:
    第一行两个用空格分开的正整数N和M,分别表示菜肴数目和制作顺序限
    制的条目数。
    接下来M行,每行两个正整数x,y,表示“x号菜肴必须先于y号菜肴制作”
    的限制。(注意:M条限制中可能存在完全相同的限制)

    输出格式

    输出文件仅包含 D 行,每行 N 个整数,表示最优的菜肴制作顺序,或

    者”Impossible!”表示无解(不含引号)。

    输入样例

    3

    5 4

    5 4

    5 3

    4 2

    3 2

    3 3

    1 2

    2 3

    3 1

    5 2

    5 2

    4 3

    输出样例

    1 5 3 4 2

    Impossible!

    1 5 2 4 3

    提示

    第二组数据同时要求菜肴1先于菜肴2制作,菜肴2先于菜肴3制作,菜肴3先于

    菜肴1制作,而这是无论如何也不可能满足的,从而导致无解。

    100%的数据满足N,M<=100000,D<=3。

    题解

    容易想到正拓扑序 + 堆的贪心
    但是这样不能保证编号小的尽量前

    但是我们逆拓扑序,尽量先选编号大的,再把顺序反过来就行了

    因为大的编号放后面,就以为着把更前的位置留给了编号更小的点,交换过来一定不优

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<queue>
    #include<vector>
    #include<cstring>
    #include<algorithm>
    #define LL long long int
    #define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
    #define REP(i,n) for (int i = 1; i <= (n); i++)
    #define BUG(s,n) for (int i = 1; i <= (n); i++) cout<<s[i]<<' '; puts("");
    using namespace std;
    const int maxn = 100005,maxm = 100005,INF = 1000000000;
    inline int read(){
    	int out = 0,flag = 1; char c = getchar();
    	while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
    	while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
    	return out * flag;
    }
    int h[maxn],ne = 1;
    struct EDGE{int to,nxt;}ed[maxn];
    int n,m,ans[maxn],ansi,vis[maxn],inde[maxn];
    priority_queue<int> q;
    inline void build(int u,int v){
    	ed[ne] = (EDGE){v,h[u]}; h[u] = ne++;
    	inde[v]++;
    }
    int dfn[maxn],low[maxn],Scc[maxn],st[maxn],top,cnt,scci,flag;
    void dfs(int u){
    	dfn[u] = low[u] = ++cnt;
    	st[++top] = u;
    	Redge(u){
    		if (!dfn[to = ed[k].to]){
    			dfs(to);
    			if (!flag) return;
    			low[u] = min(low[to],low[u]);
    		}
    		else if (!Scc[to]) low[u] = min(low[u],dfn[to]);
    	}
    	if (dfn[u] == low[u]){
    		scci++;
    		int tot = 0;
    		do {
    			Scc[st[top]] = scci; tot++;
    		}while (st[top--] != u);
    		if (tot > 1){
    			flag = false;
    			return;
    		}
    	}
    }
    bool solve(){
    	memset(dfn,0,sizeof(dfn));
    	memset(Scc,0,sizeof(Scc));
    	top = cnt = scci = 0; flag = true;
    	REP(i,n) if (!dfn[i]){
    		dfs(i);
    		if (!flag) return false;
    	}
    	REP(i,n) if (!inde[i]) q.push(i);
    	int u; ansi = 0;
    	while (!q.empty()){
    		u = q.top(); q.pop();
    		ans[++ansi] = u;
    		Redge(u) {
    			inde[to = ed[k].to]--;
    			if (!inde[to]) q.push(to);
    		}
    	}
    	return true;
    }
    int main(){
    	int T = read();
    	while (T--){
    		ne = 1;
    		memset(h,0,sizeof(h));
    		memset(inde,0,sizeof(inde));
    		n = read(); m = read();
    		int a,b;
    		while (m--){
    			a = read(); b = read();
    			build(b,a);
    		}
    		if (!solve()) puts("Impossible!");
    		else{
    			for (int i = n; i; i--) printf("%d ",ans[i]); puts("");
    		}
    	}
    	return 0;
    }
    
    
  • 相关阅读:
    自定义导航全屏滑动返回上一页
    快速tab应用
    一种透明效果的view
    10.11 pod 安装
    GIT本地操作
    Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/usr/lib/dyld_sim is not owned by root.
    bt5r3开启远程登录
    神奇的脚本1
    pt-table-checksum和pt-table-sync
    http://blog.sae.sina.com.cn/archives/881
  • 原文地址:https://www.cnblogs.com/Mychael/p/8944313.html
Copyright © 2011-2022 走看看