POJ 1094 Sorting It All Out

Sorting It All Outios

【題目連接】http://poj.org/problem?id=1094算法

【解題思路】Floyd判環+拓撲排序;當Floy[i][i] = true 的時候就說明圖中有環,拓撲排序在原圖中找到路徑,其實也沒什麼好說的,都是赤裸裸的圖算法的運用,坑點的就是不知道A<B B<A這種狀況是屬於那種輸出spa

【隨筆】這題雖然簡單,可是好久沒寫過拓撲,不敢肯定在提交WA以後不是算法出了錯誤,因此一直在找錯誤,後來的狀況確實是這樣,算法並無錯二十輸入的時候沒考慮到狀況,我是用了Floyd判環後得圖用做拓撲排序,統計節點的入度的時候重複計算了節點的入度,沒有考慮到的狀況是在輸入時的兩點連通的狀況在判環的時候可能已有統計,因此有重複的邊計算出來的入度比正確狀況下的大,因此拓撲不出來,WA了很多code

  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 }
相關文章
相關標籤/搜索