zoukankan      html  css  js  c++  java
  • Ordering Tasks 拓扑排序

     John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task is
    only possible if other tasks have already been executed.
    Input
    The input will consist of several instances of the problem. Each instance begins with a line containing
    two integers, 1 n 100 and m. n is the number of tasks (numbered from 1 to n) and m is the
    number of direct precedence relations between tasks. After this, there will be m lines with two integers
    i and j, representing the fact that task i must be executed before task j.
    An instance with n = m = 0 will finish the input.
    Output
    For each instance, print a line with n integers representing the tasks in a possible order of execution.
    Sample Input
    5 4
    1 2
    2 3
    1 3
    1 5
    0 0
    Sample Output
    1 4 2 5 3

     

    拓扑排序:

     

    代码1:

    #include <iostream>
    #include <cstdio>
    #include <queue>
    #include <map>
    #include <algorithm>
    using namespace std;
    ///拓扑排序学习题
    int main()
    {
        int n,m,a,b,ans[100];
        while(scanf("%d%d",&n,&m))
        {
            if(!(n+m)^0)break;
            int mp[101][101]={0},vis[101]={0},mark[101]={0};///mark用来记录是否有比自己优先的 如果有就
            for(int i=0;i<m;i++)            /// 加1 可能有多个比自己优先的,如果没有比自己优先的 直接输出
            {                          ///mp记录是否有排在自己后面的 如果有就把mark减一 表示我已经输出了
                scanf("%d%d",&a,&b);           /// 对你没限制了 至于其他人对你有没有限制我不管了
                mark[b]++;                         ///vis看是否被记录 ,记录了变为1
                mp[a][b]=1;
            }
            int j=0,temp;
            while(j<n)
            {
                temp=-1;
                for(int i=1;i<=n;i++)
                    if(!vis[i]&&!mark[i])
                    {
                        ans[j++]=i;
                        temp=i;
                        break;
                    }
                if(temp<0)break;///不存在没记录的元素了 就停止
                vis[temp]=1; ///mark一下
                for(int i=1;i<=n;i++)
                    if(mp[temp][i]==1)mark[i]--;
            }
            for(int i=0;i<n;i++)
                printf("%d ",ans[i]);
            putchar('
    ');
        }
    }

    代码2:

    #include <iostream>
    #include <cstdio>
    #include <queue>
    #include <map>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    ///拓扑排序学习题  递归形式dfs 选中一个未排序的元素进行dfs把在他之后的装进ans(倒着装)并标记。

    int n,m,a,b,ans[101],mp[101][101]={0},vis[101]={0},k; bool dfs(int last) { vis[last]=-1; for(int i=1;i<=n;i++) if(mp[last][i]) { if(vis[i]==0)dfs(i); else if(vis[i]==-1)return false;//形成了回路 无法继续排序 -1表示i比last先入栈。 } ans[--k]=last; vis[last]=1; return true; } int main() { while(scanf("%d%d",&n,&m)) { if(!(n+m))break; k=n; memset(vis,0,sizeof(vis)); memset(mp,0,sizeof(mp)); for(int i=0;i<m;i++) { scanf("%d%d",&a,&b); mp[a][b]=1; } for(int i=1;i<=n;i++) { if(!vis[i]) { if(!dfs(i))break; } } for(int i=k;i<n;i++) printf("%d ",ans[i]); putchar(' '); } }
  • 相关阅读:
    System Idle Process SYSTEM占用CPU
    apache和nginx的rewrite的区别
    解决file_get_contents failed to open stream: HTTP request failed! 错误
    个人总结大型高并发高负载网站的系统架构(转)
    代码的抽象三原则
    mysqldump导入某库某表的数据
    mysql中insert into和replace into以及insert ignore用法区别
    【原创】学习日记4:nginx负载均衡(二)2012.01.08
    【原创】学习日记1:redis安装以及lnmp环境搭建2012.01.06
    mysql优化 mysql.ini优化
  • 原文地址:https://www.cnblogs.com/8023spz/p/7231122.html
Copyright © 2011-2022 走看看