Both Floyd and Dij can be used for this question, but it is very convenient to use Floyd. It is also the first time to use the Floyd algorithm. I didn't have this idea at first, so I could refer to other people's ideas ..~~~~ (>_< )~~~~
# Include <stdio. h> # include <iostream> # include <string. h> using namespace std; int main (void) {int ncases, n, I, j, k; int map [30] [30], mark [30] [30]; cin> ncases; for (int f = 1; f <= ncases; f ++) {memset (map, 0, sizeof (map); memset (mark, 0, sizeof (mark); int flag = 1; cin> n; char s [4]; while (n --) {scanf ("% s", s ); int a, B; char o; a = s [0]-'A' + 1; o = s [1]; B = s [2]-'A' + 1; if (o = '<') map [A] [B] = 1; else Map [B] [a] = 1;} memcpy (mark, map, sizeof (map); for (k = 1; k <= 28; k ++) for (I = 1; I <= 28; I ++) for (j = 1; j <= 28; j ++) map [I] [j] = map [I] [j] | (map [I] [k] & map [k] [j]); // here the Floyd is cleverly deformed .. At least I think this is a weak dish. Aha printf ("Case % d: \ n", f); for (I = 1; I <= 28; I ++) for (j = 1; j <= 28; j ++) if (map [I] [j]) &! Mark [I] [j]) {printf ("% c <% c \ n", I + 'a'-1, j + 'a'-1 ); flag = 0;} if (flag) printf ("NONE \ n") ;}return 0 ;}