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
| #include<bits/stdc++.h> using namespace std; inline int read(){ int res=0,f=1;char ch=getchar(); while(!isdigit(ch)) f=ch=='-'?-1:f,ch=getchar(); while(isdigit(ch)) res=(res<<3)+(res<<1)+(ch&15),ch=getchar(); return res*f; } inline void write(int x){ if(x<0) putchar('-'),x=-x; if(x<10) putchar(x+'0'); else write(x/10),putchar(x%10+'0'); } int n,m,c,fir[100010],nxt[500010+20*100010],son[500010+20*100010],w[500010+20*100010],tot,A,B,vis[1000010],dis[1000010]; inline void add(int x,int y,int z){++tot;nxt[tot]=fir[x];fir[x]=tot;son[tot]=y;w[tot]=z;} inline void build(){ for(int i=0;i<=n;i++) for(int j=1;j<=n;j<<=1) if((i^j)<=n) add(i,i^j,j*c); } priority_queue<pair<int,int> > q; inline void Dij(){ while(!q.empty()) q.pop(); q.push(make_pair(0,A)); memset(dis,63,sizeof(dis));dis[A]=0; memset(vis,0,sizeof(vis)); while(!q.empty()){ pair<int,int> u=q.top();q.pop(); while(vis[u.second]&&!q.empty()) u=q.top(),q.pop(); if(vis[u.second]) return ; vis[u.second]=1; for(int to,i=fir[u.second];i;i=nxt[i]){ to=son[i]; if(dis[to]>dis[u.second]+w[i]){ dis[to]=dis[u.second]+w[i]; q.push(make_pair(-dis[to],to)); } } } } int main(){ n=read(),m=read(),c=read(); build(); for(int x,y,z,i=1;i<=m;i++) x=read(),y=read(),z=read(),add(x,y,z); A=read(),B=read(); Dij(); return write(dis[B]),0; }
|