POJ 1094 Sorting It All Out

Sorting It All Out

【题目链接】http://poj.org/problem?id=1094

【解题思路】Floyd判环+拓扑排序;当Floy[i][i] = true 的时候就说明图中有环,拓扑排序在原图中找到路径,其实也没什么好说的,都是赤裸裸的图算法的运用,坑点的就是不知道A<B B<A这种情况是属于那种输出

【随笔】这题虽然简单,但是很久没写过拓扑,不敢确定在提交WA之后不是算法出了错误,所以一直在找错误,后来的情况确实是这样,算法并没有错二十输入的时候没考虑到情况,我是用了Floyd判环后得图用作拓扑排序,统计节点的入度的时候重复计算了节点的入度,没有考虑到的情况是在输入时的两点连通的情况在判环的时候可能已有统计,所以有重复的边计算出来的入度比正确情况下的大,所以拓扑不出来,WA了不少

  1 #include <iostream>
  2 #include <cstring>
  3 #include <cstdio>
  4 #define SIZE 27
  5 using namespace std;
  6 
  7 bool alpha[SIZE][SIZE];
  8 int num[SIZE], copynum[SIZE];
  9 int path[SIZE], visit[SIZE];
 10 int nv, ne;
 11 
 12 bool Floyd()
 13 {
 14     for(int k=0; k<nv; ++k)
 15     for(int i=0; i<nv; ++i)
 16     for(int j=0; j<nv; ++j)
 17     {
 18         if(alpha[i][j] || (alpha[i][k] && alpha[k][j]))
 19         {
 20             if(!alpha[i][j] && i != j) num[j]++;
 21             if(i == j) return true;
 22             alpha[i][j] = true;
 23         }
 24     }
 25     return false;
 26 }
 27 
 28 bool DFS(int cur, int sum)
 29 {
 30     if(cur == -1)
 31     {
 32         int cnt = 0;
 33         for(int i=0; i<nv; ++i) if(num[i] == 0)
 34         {
 35             if(cnt == 0) cnt++;
 36             else return false;
 37             cur = i;
 38         }
 39         if(cnt == 0) return false;
 40        memcpy(copynum, num, sizeof(num));
 41     }
 42     path[sum] = cur;
 43     if(sum  == nv - 1) return true;
 44     for(int i=0; i<nv; ++i) if(alpha[cur][i] && cur != i) copynum[i]--;
 45     int cnt = 0, next = -1 ;
 46     for(int i=0; i<nv; ++i) if(alpha[cur][i] && cur != i && copynum[i] == 0)
 47     {
 48         if(cnt == 0) cnt++;
 49         else return false;
 50         next = i;
 51     }
 52     if(next != -1 && DFS(next, sum+1)) return true;
 53     return false;
 54 
 55 }
 56 
 57 int main()
 58 {
 59     #ifndef ONLINE_JUDGE
 60     freopen("F:\test\input.txt", "r", stdin);
 61     #endif
 62     while(cin >> nv >> ne, nv + ne)
 63     {
 64         for(int i=0; i<nv; ++i)
 65         for(int j=i; j<nv; ++j)
 66         {
 67             num[i] = 0;
 68             alpha[i][j] = alpha[j][i] = false;
 69         }
 70         int flag = -1, where = -1;
 71         char u, v, op;
 72         for(int i=0; i<ne; ++i)
 73         {
 74             cin >> u >> op >> v;
 75             if(flag != -1) continue;
 76             if(alpha[u-'A'][v-'A'] != true) num[v-'A']++;
 77             alpha[u-'A'][v-'A'] = true;
 78 
 79             if(Floyd()) flag = 0, where = i+1;
 80             else if(DFS(-1, 0)) flag = 1, where = i+1;
 81         }
 82         if(flag == -1)
 83             cout << "Sorted sequence cannot be determined." << endl;
 84         else if(flag == 0)
 85             cout << "Inconsistency found after "<< where << " relations." << endl;
 86         else
 87         {
 88             cout << "Sorted sequence determined after " << where << " relations: ";
 89             for(int i=0; i<nv; ++i)
 90             {
 91                 char temp = path[i] + 'A';
 92                 cout << temp;
 93             }
 94             cout << "." << endl;
 95         }
 96 
 97 
 98 
 99     }
100     return 0;
101 }
原文地址:https://www.cnblogs.com/liaoguifa/p/3233245.html