floyd傳遞閉包

2021-08-15 15:19:51 字數 2389 閱讀 5321

傳遞閉包(我第一反應是凸包emmm),就是把具有傳遞性的關係傳遞開。一般我們用一鄰接矩陣儲存。比如許多的並查集解決的問題,如果需要細緻**(效率o(n³)),可以用傳遞閉包去做。

看一道題吧,poj1094,不等式的傳遞性。這道題在處理方面,d(i, j)為1時表示i1、矛盾與不確定,優先矛盾。

2、若加入了幾個,發現有唯一解,輸出這個即可,不要管後面會不會矛盾。

上**:

#include #include #include using namespace std;

struct node opt[1010], sum[30];

inline bool cmp1(node a, node b)

int d[30][30], n, m;

inline void send()

inline int check()

if(d[i][j] == d[j][i] && d[i][j] == 0)

} if(incon) return -1;

if(cannot) return 0;

return 1;

}int main()

opt[i].x = ch[0] - 'a' + 1; opt[i].y = ch[2] - 'a' + 1;

d[ch[0] - 'a' + 1][ch[2] - 'a' + 1] = 1;

} send();

int signal = check();

if(signal == 0)

int l = 1, r = m, mid, ans;

if(signal == 1)

printf("sorted sequence determined after %d relations: ", ans);

for(int i = 1; i <= n; ++i)

sort(sum+1, sum+n+1, cmp1);

for(int i = 1; i <= n; ++i) printf("%c", 'a' - 1 + sum[i].x);

printf(".\n"); continue;

} if(signal == -1)

else l = mid + 1;

}if(!atp) printf("inconsistency found after %d relations.\n", ans);

else

sort(sum+1, sum+n+1, cmp1);

for(int i = 1; i <= n; ++i) printf("%c", 'a' - 1 + sum[i].x);

printf(".\n"); continue;

}} }

return 0;

}

膜poj標準output:

sorted sequence determined after 6 relations: dcab.

sorted sequence determined after 29 relations: cfdaebhgji.

sorted sequence cannot be determined.

inconsistency found after 48 relations.

sorted sequence determined after 318 relations: gmnvkchfobrjdzlpxayseiwqtu.

sorted sequence determined after 25 relations: abcdefghijklmnopqrstuvwxyz.

sorted sequence determined after 7 relations: badce.

sorted sequence determined after 158 relations: hnamgipctbjfrleodksq.

inconsistency found after 35 relations.

sorted sequence cannot be determined.

sorted sequence determined after 7 relations: abdec.

sorted sequence cannot be determined.

sorted sequence determined after 11 relations: acdbfe.

sorted sequence cannot be determined.

sorted sequence cannot be determined.

sorted sequence determined after 179 relations: chqnamdlrfpgisbjoket.

floyd求傳遞閉包

n頭牛比賽,m種比賽結果,最後問你一共有多少頭牛的排名被確定了,其中如果a戰勝b,b戰勝c,則也可以說a戰勝c,即可以傳遞勝負。求能確定排名的牛的數目。思路 如果一頭牛被x頭牛打敗,打敗y頭牛,且x y n 1,則我們容易知道這頭牛的排名就被確定了,所以我們只要將任何兩頭牛的勝負關係確定了,在遍歷所...

floyd 排序(傳遞閉包)

給定 n 個變數和 m 個不等式。其中 n 小於等於26,變數分別用前 n 的大寫英文本母表示。不等式之間具有傳遞性,即若 a b 且 b c 則 a c。請從前往後遍歷每對關係,每次遍歷時判斷 如果能夠確定全部關係且無矛盾,則結束迴圈,輸出確定的次序 如果發生矛盾,則結束迴圈,輸出有矛盾 如果迴圈...

Floyd演算法 求傳遞閉包

題目讓確定有幾個人的位置是確定的,如果乙個點有x個點能到達此點,從該點出發能到達y個點,若x y n 1 x y n 1 x y n 1,則該點的位置是確定的。用floyd演算法算出每兩個點之間的距離,最後統計時,若dis a b dis a b dis a b 之間無路且dis b a dis b...