B4010 菜肴制作 拓扑排序(附随机跳题代码)

今天写了一个自己的随机跳题小程序,第一次试发现跳的全是不可做题,但是在周围我一眼看见了这个题,不能说一眼看出来,但是也是比较有思路,所以就做他了!

做得比较顺利,做完之后美滋滋,突然发现样例第三组过不了。。。然后发现自己算法有问题。。。GG,又想了一个超复杂的算法,刚开始写就放弃了,根本没法写。

于是看题解(本来以为自己能A),就看了一行就明白了,只要倒着存边再倒着输出就行了!!!QAQ!!

跳题代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
#define duke(i,a,n) for(int i = a;i <= n;i++)
#define lv(i,a,n) for(int i = a;i >= n;i--)
#define clean(a) memset(a,0,sizeof(a))
const int INF = 1 << 30;
typedef long long ll;
typedef double db;
template <class T>
void read(T &x)
{
    char c;
    bool op = 0;
    while(c = getchar(), c < '0' || c > '9')
        if(c == '-') op = 1;
    x = c - '0';
    while(c = getchar(), c >= '0' && c <= '9')
        x = x * 10 + c - '0';
    if(op) x = -x;
}
template <class T>
void write(T x)
{
    if(x < 0) putchar('-'), x = -x;
    if(x >= 10) write(x / 10);
    putchar('0' + x % 10);
}
int main()
{
    int m,d;
    time_t t;
    read(m);read(d);
    srand((unsigned int)time(NULL));
    int f =  m * d * rand() % 5010;
    if(f > 1000)
    printf("%d
",f);
    else
    printf("%d
",f + 1000);
    return 0;
}

题干:

Description

知名美食家小 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>,所以只有制作完 34 后才能制作 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!” (不含引号,
首字母大写,其余字母小写) 
Input

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

 输出文件仅包含 D 行,每行 N 个整数,表示最优的菜肴制作顺序,或
者”Impossible!”表示无解(不含引号)。 
Sample Input
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 
Sample Output
1 5 3 4 2 

Impossible! 

1 5 2 4 3 

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<queue>
#include<algorithm>
#include<cstring>
#include<vector> 
using namespace std;
#define duke(i,a,n) for(int i = a;i <= n;i++)
#define lv(i,a,n) for(int i = a;i >= n;i--)
#define clean(a) memset(a,0,sizeof(a))
const int INF = 1 << 30;
typedef long long ll;
typedef double db;
template <class T>
void read(T &x)
{
    char c;
    bool op = 0;
    while(c = getchar(), c < '0' || c > '9')
        if(c == '-') op = 1;
    x = c - '0';
    while(c = getchar(), c >= '0' && c <= '9')
        x = x * 10 + c - '0';
    if(op) x = -x;
}
template <class T>
void write(T x)
{
    if(x < 0) putchar('-'), x = -x;
    if(x >= 10) write(x / 10);
    putchar('0' + x % 10);
}
int len = 0,ru[100010],chu[100010],lst[100010];
int n,m,k,x,y,w,ans[100010],num = 0;
priority_queue <int> qu;
//vector <int> ve[100010];
bool vis[100010];
struct node
{
    int l,r,nxt;
}a[200010];
void add(int l,int r)
{
    a[++len].l = l;
    a[len].r = r;
    a[len].nxt = lst[l];
    lst[l] = len;
    chu[l]++;
    ru[r]++;
}
void clean_queue()
{
    priority_queue <int> p;
    swap(p,qu);
}
int ok = 1,emm = 1;
int main()
{
    read(k);
    while(k--)
    {
        clean(vis);
        clean(lst);
        clean(ru);
        clean(chu);
        clean(ans);
        clean_queue();
        len = 0;
        ok = 1;
        emm = 1;
        num = 0;
        read(n);read(m);
        duke(i,1,m)
        {
            read(x);read(y);
            add(y,x);
        }
        duke(i,1,n)
        {
            if(ru[i] == 0)
            qu.push(i),emm = 0;
        }
        if(emm != 0)
        {
            printf("Impossible!
");
            continue;
        }
        while(!qu.empty())
        {
            x = qu.top();
            qu.pop();
            vis[x] = 1;
            for(int p = lst[x];p;p = a[p].nxt)
            {
                int r = a[p].r;
                ru[r]--;
                if(ru[r] == 0)
                qu.push(r);
            }
            ans[++num] = x;
        } 
        if(ans[n] != 0)
        {
            lv(i,n,1)
                printf("%d ",ans[i]);
            printf("
");
        }
        else
        {
            printf("Impossible!
");
            continue;
        }
    }
    return 0;
} 
/*
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 
*/
原文地址:https://www.cnblogs.com/DukeLv/p/9499099.html