1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| #include<cstdio> #include<cstring> #include<iostream> #define reg register #define uni unsigned #define ll long long using namespace std; const int N=10010,FSIZE=1<<26,W=4,INF=0x3f3f3f3f; int n,m,k,r,a[N],b[N+N][3],last,g[N][2048],h,t,d[N<<3],map[N],ans=INF; bool leg[N]; char BuF[FSIZE],*InF=BuF; int read(){ reg int x=0; for(;47>*InF||*InF>58;++InF); for(;47<*InF&&*InF<58;x=x*10+(*InF++^48)); return(x); } void add(reg int x,reg int y,reg int w){ b[++last][0]=a[x]; b[a[x]=last][1]=y; b[last][2]=w; } void change(reg int &x,reg int &y){ swap(map[x],map[y]); swap(x,y); } void push(reg int nxt,reg int s){ if(!map[nxt]){ d[map[nxt]=++t]=nxt; if(t>h+1&&g[d[t-1]][s]<g[d[t]][s]){ change(d[t],d[t-1]); } }else if(g[nxt][s]<g[d[t]][s]){ change(d[map[nxt]],d[t]); } } void spfa_PMF(int s){ for(;h<=t;++h){ reg int &hd=d[h]; for(reg int i=0,*zh=d+h+1,*tl=d+t;i<W&&zh<tl;++i,++zh,--tl){ if(g[hd][s]>g[*zh][s]){ change(hd,*zh); } if(g[hd][s]>g[*tl][s]){ change(hd,*tl); } } for(reg int i=a[hd];i;i=b[i][0]){ reg int nxt=b[i][1],p=g[hd][s]+b[i][2]; if(g[nxt][s]>p){ g[nxt][s]=p; push(nxt,s); } } map[hd]=0; } } int main(){ fread(BuF,1,FSIZE,stdin); n=read();m=read(); for(r=1<<(k=read());m;--m){ reg int x=read(),y=read(),w=read(); add(x,y,w); add(y,x,w); } memset(g,63,sizeof(g)); for(reg int i=0;i<k;g[read()][1<<i++]=0); for(reg int i=1;i<r;++i){ h=0;t=-1; for(reg int j=1;j<=n;++j){ for(reg int k=i&(i-1);k;k=i&(k-1)){ g[j][i]=min(g[j][i],g[j][k]+g[j][i^k]); } if(g[j][i]<INF){ push(j,i); } } spfa_pmf(i); } for(reg int i=0;i<n;ans=min(ans,g[++i][r-1])); printf("%d",ans); return(0); }
|