HDU 2112 (Shortest Path + dictionary tree)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.