zoukankan      html  css  js  c++  java
  • 确定比赛名次(拓扑排序 +有限对列)

    /**
    优先队列加拓扑排序
    */

    include<stdio.h>

    include

    include

    include<string.h>

    using namespace std;
    vectoredge[500*500];
    int du[550];
    void creatLinJieBiao(int x,int y)
    {

    edge[x].push_back(y);
    du[y]++;
    

    }
    struct myComp///不是结构体时 定义优先队列的方法
    {
    bool operator ()(int a,int b)///小的优先出队
    {
    return a>b;
    }
    };
    void topSort(int n)
    {
    priority_queue<int,vector,myComp>q;///不是结构体的优先队列
    for(int i=1; i<=n; i++)
    {
    if(du[i]==0)
    q.push(i);
    }
    int op=0;
    while(!q.empty())
    {

        int a=q.top();
        q.pop();
        if(op==0)
            printf("%d",a);
        else printf(" %d",a);
        op=1;
        for(int i=0; i<edge[a].size(); i++)
        {
            du[edge[a][i]]--;
            if(du[edge[a][i]]==0)
            {
                q.push(edge[a][i]);
            }
        }
    }
    printf("
    ");
    

    }
    int main()
    {
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
    memset(du,0,sizeof(du));
    memset(edge,0,sizeof(edge));///注意初始化
    for(int i=1; i<=m; i++)
    {
    int x,y;
    scanf("%d%d",&x,&y);
    creatLinJieBiao(x,y);
    }
    topSort(n);
    }
    return 0;
    }

    /**
    5 5

    3 4
    1 5 2 3
    5 4 1 2
    */

    梦里不知身是客,一晌贪欢。
  • 相关阅读:
    BZOJ4569: [Scoi2016]萌萌哒
    BZOJ4566: [Haoi2016]找相同字符
    BZOJ4556: [Tjoi2016&Heoi2016]字符串
    BZOJ4545: DQS的trie
    BZOJ4458: GTY的OJ
    Codeforces Beta Round #19E. Fairy
    不确定性推理
    朴素贝叶斯
    对抗搜索
    struct
  • 原文地址:https://www.cnblogs.com/dccmmtop/p/5502815.html
Copyright © 2011-2022 走看看