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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| #include<iostream> #include<cstdio> #include<utility> #include<functional> #include<queue> #include<algorithm> #include<cstring> using namespace std; const int maxn=1e4,maxm=5e4,maxk=10,INF=~0U>>1; int n,m,k,start,end; struct edge{ int to,next,dis; }e[4*maxm*(maxk+1)+maxk+1]; int ecnt,head[maxn*(maxk+1)+1]; void addedge(int from,int to,int dis) { e[++ecnt]=(edge){to,head[from],dis};head[from]=ecnt; }
void read(int &x) { bool flag = false; char c; do c=getchar();while(c!='-' and (c < '0' || c > '9')); if(c=='-')flag=true; else x=c-'0'; c=getchar(); while(c>='0' and c<='9')x*=10,x+=c-'0',c=getchar(); } int read2() { int x; char c=getchar(); while(c<'0' or c>'9')c=getchar(); x=c-'0';c=getchar(); while(c>='0' and c<='9') x*=10,x+=c-'0',c=getchar(); return x; }
void readin() {
n=read2(),m=read2(),k=read2(),start=read2(),end=read2(); for(int i=0;i<m;i++) { int a,b,w;
a=read2(),b=read2(),w=read2(); for(int j=0;j<=k;j++) { addedge(a+n*j,b+n*j,w);addedge(b+n*j,a+n*j,w); if(j!=k) { addedge(a+n*j,b+n*(j+1),0);addedge(b+n*j,a+n*(j+1),0); } } } }
int dis[maxn*(maxk+1)]; bool vis[maxn*(maxk+1)]; typedef pair<int,int> pairr; void dijkstra() { priority_queue <pairr,vector<pairr >,greater<pairr > > q; q.push(make_pair(0,start)); memset(dis,0x3f3f3f3f,sizeof(dis)); dis[start]=0; while(!q.empty()) { int u=q.top().second;q.pop(); if(!vis[u]) { vis[u]=1; for(int i=head[u];i;i=e[i].next) { int to=e[i].to; if(dis[u]+e[i].dis<dis[to]) { dis[to]=dis[u]+e[i].dis; q.push(make_pair(dis[to],to)); } } } } } int main() { readin(); dijkstra(); for(int i=1;i<=k;++i) { addedge(end+(i-1)*n,end+i*n,0); } cout<<dis[end+k*n]; return 0; }
|