zoukankan      html  css  js  c++  java
  • poj 1094 Sorting It All Out (拓扑排序)

    效率太低了,反思~~~~

    1:题意理解错误

    where xxx is the number of relations processed at the time either a sorted sequence is determined or an inconsistency is found, whichever comes first, and yyy...y is the sorted, ascending sequence. 

    明明没说是按它列的顺序求解--关系的数量,

    2:对环的处理上没有搞好 

    #include<stdio.h>
    #include<string.h>
    #define N 30
    int map[N][N],sum[N],temp[N];
    char alp[N];
    int n,m;
    int topology()
    {
    int t=1,zero,k;
    memset(alp,'\0',sizeof(alp));
    memcpy(temp,sum,sizeof(temp));
    for(int i=0;i<n;i++)
    {
    zero=0;
    for(int j=0;j<n;j++)
    if(!temp[j])
    {
    zero++;k=j;
    }
    if(zero>1) t=-1;
    if(zero==0) return 0;
    temp[k]=-1,alp[i]='A'+k;
    for(int j=0;j<n;j++)
    {
    if(map[k][j])
    {
    temp[j]--;
    }
    }

    }
    return t;
    }
    int main()
    {
    while(scanf("%d%d%*c",&n,&m),n+m)
    {
    memset(map,0,sizeof(map));
    memset(sum,0,sizeof(sum));
    int ok=0;
    for(int i=1;i<=m;i++)
    {
    char st[4];gets(st);
    if(!map[st[0]-'A'][st[2]-'A'])
    {
    map[st[0]-'A'][st[2]-'A']=1;
    sum[st[2]-'A']++;
    }

    if(ok) continue;
    int t=topology();
    if(t==1)
    {
    ok=1;
    printf("Sorted sequence determined after %d relations: %s.\n",i,alp);
    }
    else if(!t)
    {
    ok=1;
    printf("Inconsistency found after %d relations.\n",i);
    }
    }
    if(!ok)
    printf("Sorted sequence cannot be determined.\n");

    }
    //system("pause");
    }



    Just a little, maybe change the world
  • 相关阅读:
    java学习笔记 (2) —— Struts2类型转换、数据验证重要知识点
    java学习笔记 (1) —— Strut2.3.24环境搭建
    数据不平衡问题的处理
    正则化与特征稀疏,过拟合
    leetcode144-先序遍历非递归实现
    解释器资料
    ROC,AUC
    假设检验——KS检验
    SVM理解
    高斯过程与核密度估计
  • 原文地址:https://www.cnblogs.com/skyming/p/2410908.html
Copyright © 2011-2022 走看看