TYVJ 1993 HASH+最短路
2012-10-05 15:29
148 查看
考试的时候偷懒,写的map,结果被卡了。。。20分。。我这弱菜、、
其实此题很水。。。
思路:
明显的最短路,就是写hash呗。。
表示从来没写过hash,一直用map水的。。
头一次写hash,还1A了~嘿嘿,细心点就好
PS:我的hash函数不取摸是完美的。
View Code
其实此题很水。。。
思路:
明显的最短路,就是写hash呗。。
表示从来没写过hash,一直用map水的。。
头一次写hash,还1A了~嘿嘿,细心点就好
PS:我的hash函数不取摸是完美的。
View Code
#include <cstdio> #include <cstring> #include <cstdlib> #include <string> #include <algorithm> #include <iostream> #define MOD 999991 #define N 50500 #define M 3000000 bool fg; using namespace std; struct HEAP { int x,d; }hp ; int fac[12],hto ,hnext ,hhead[1000000],next[M],head ,to[M],len[M],hcnt,cnt; int hash ,n,lcp[12],dis ,size; char tel [12],sst[12],stmp; inline bool cmp(const HEAP &a,const HEAP &b) { return a.d>b.d; } void prev()//check { fac[1]=1; for(int i=2;i<=10;i++) fac[i]=fac[i-1]*10; } void addhash(int u,int v) { hto[hcnt]=v; hnext[hcnt]=hhead[u]; hhead[u]=hcnt++; } void add(int u,int v,int w) { to[cnt]=v; len[cnt]=w; next[cnt]=head[u]; head[u]=cnt++; } void close(int x) { __int64 tmp=0,sk; for(int i=1;i<=10;i++) { sk=tel[x][i]-'0'; tmp+=sk*fac[i]; } hash[x]=tmp%MOD; addhash(hash[x],x); } void cpy(int x) { for(int i=1;i<=11;i++) sst[i]=tel[x][i]; } int gethash() { __int64 rt=0,tmp; for(int i=1;i<=10;i++) { tmp=(sst[i]-'0'); rt+=tmp*fac[i]; } rt%=MOD; for(int i=hhead[rt];~i;i=hnext[i]) if(strcmp(&sst[1],&tel[hto[i]][1])==0) return hto[i]; return 0; } void create() { for(int i=1;i<=n;i++) { cpy(i); for(int j=1;j<=10;j++) for(int k=j+1;k<=10;k++) if(sst[j]!=sst[k]) { swap(sst[j],sst[k]); if(i==1&&j==4&&k==10) fg=true; int tmp=gethash(); if(tmp) { add(i,tmp,lcp[j-1]); add(tmp,i,lcp[j-1]); } swap(sst[j],sst[k]); } for(int j=1;j<=10;j++) for(int k=0;k<=9;k++) if(sst[j]!=k+'0') { stmp=sst[j]; sst[j]=k+'0'; int tmp=gethash(); if(tmp) { add(i,tmp,lcp[j-1]); add(tmp,i,lcp[j-1]); } sst[j]=stmp; } } } void read() { memset(head,-1,sizeof head);cnt=1; memset(hhead,-1,sizeof hhead);hcnt=1; scanf("%d",&n); for(int i=0;i<10;i++) scanf("%d",&lcp[i]); for(int i=1;i<=n;i++) { scanf("%s",&tel[i][1]); close(i); } } void dijkstra() { memset(dis,0x3f,sizeof dis); size=1; hp[1].x=1; hp[1].d=0; dis[1]=0; while(size) { int sta=hp[1].x; int k=hp[1].d; pop_heap(hp+1,hp+1+size,cmp); size--; if(dis[sta]<k) continue; for(int i=head[sta];~i;i=next[i]) if(dis[to[i]]>dis[sta]+len[i]) { dis[to[i]]=dis[sta]+len[i]; size++; hp[size].x=to[i]; hp[size].d=dis[to[i]]; push_heap(hp+1,hp+1+size,cmp); } } if(dis ==0x3f3f3f3f) dis =-1; cout<<dis <<endl; } int main() { prev(); read(); create(); dijkstra(); return 0; }
相关文章推荐
- 【tyvj1293】小v的红豆(次短路---一条路只能经过一次)
- tyvj 1031 热浪(最短路)
- 【tyvj1068】STR(KMP||hash)
- 【BZOJ1922】【Tyvj1736】【codevs2129】大陆争霸,无语最短路
- TYVJ-1031 最短路
- TYVJ1993 ACM/ICPC 2001 括号序列 解题报告
- [最短路 主席树 Hash] Codeforces 464E #265 (Div. 1) E. The Classic Problem
- [Hash] [最短路] [Vijos P1184] CoVH之华丽的IP伪装 (ConanIP)
- codeforces464E The Classic Problem -- 最短路+主席树+Hash
- [最短路 && 主席树维护HASH] 51nod1863 Travel
- 【基础练习】【最短路堆优dij】tyvj1376 魔域之战题解
- [最短路 && 主席树维护HASH]Codeforces 464E. The Classic Problem
- TYVJ1232最短路(SPFA+连接表)
- |Tyvj|图论最短路|P1083 分糖果
- |Tyvj|图论最短路|P1031 热浪
- [tyvj1064]新三国争霸(最短路+dp)
- tyvj 1031 热浪 最短路
- [最短路 主席树 Hash] 51Nod 算法马拉松26 E Travel
- tyvj 1031 热浪 最短路
- Hashslider – 带有 Hash 标签功能的 jQuery 内容滑块