I just want to say that the dictionary tree will release the memory
# Include <cstdio> # include <cstring> # include <algorithm> using namespace STD; struct node {node * Next [53]; int num; node () {for (INT I = 0; I <= 53; I ++) next [I] = NULL; num = 0 ;}} * root; int CNT; char S1 [33], S2 [33]; int DP [200] [200]; int insert (char * s) {int L = strlen (s ); node * P = root; For (INT I = 0; I <L; I ++) {int ID; if (s [I] <= 'Z' & S [I]> = 'A') id = s [I]-'A '; else id = s [I]-'A' + 26; If (p-> next [ID] = NULL) {node * q = new node; P-> next [ID] = Q;} p = p-> next [ID];} If (p-> num) return p-> num; else return p-> num = CNT ++;} void del (node * P) {for (INT I = 0; I <52; I ++) {If (p-> next [I]) del (p-> next [I]);} Delete P; P = NULL;} int N; char start [33], end [33]; int main () {While (scanf ("% d", & N) & n! =-1) {memset (DP, 0x3f3f3f, sizeof (DP); CNT = 1; int A, B, C; root = new node; scanf ("% S % s", start, end); int x = insert (start); int y = insert (end); While (n --) {scanf ("% S % d", S1, S2, & C); A = insert (S1); B = insert (S2 ); DP [a] [B] = DP [B] [a] = min (C, DP [a] [B]);} For (int K = 1; k <= CNT; k ++) for (INT I = 1; I <= CNT; I ++) for (Int J = 1; j <= CNT; j ++) DP [I] [J] = min (DP [I] [J], DP [I] [k] + dp [k] [J]); if (x = y) {printf ("0 \ n"); continue;} If (DP [x] [Y] = 0x3f3f3f3f) printf ("-1 \ n"); else printf ("% d \ n", DP [x] [Y]); del (Root ); // It is important to release the memory }}
HDU 2112 (Shortest Path + dictionary tree)