标签:io for ar art amp sp new size on
只想说 字典树必释放内存
#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,0x3f3f3f3f,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%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); //释放内存很重要 } }
标签:io for ar art amp sp new size on
原文地址:http://blog.csdn.net/u013076044/article/details/38819817