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
| #include<iostream> #include<cstdio> #include<algorithm> #include<queue> #include<vector> #include<cstring> #define ll long long using namespace std;
const ll N=1e4,K=10,M=5e4;
ll n,m,k,S,T,u,v,w,tot,ans;
ll ver[M*2+5],nxt[M*2+5],wt[M*2+5],head[N+5],f[N+5][K+5];
bool vis[N+5][K+5];
struct node{ ll d,v,l; bool operator > (const node& rhs) const { return v>rhs.v; } }h,t;
priority_queue<node,vector<node>,greater<node> > q;
void dij() { memset(vis,0,sizeof(vis)); memset(f,0x3f,sizeof(f)); f[S][0]=0;t.d=S;t.l=0;t.v=f[S][0]; q.push(t); while(!q.empty()) { h=q.top();q.pop(); if(vis[h.d][h.l]) continue;vis[h.d][h.l]=1; for(ll i=head[h.d];i;i=nxt[i]) { if(f[ver[i]][h.l]>f[h.d][h.l]+wt[i]) { f[ver[i]][h.l]=f[h.d][h.l]+wt[i]; t.d=ver[i];t.l=h.l;t.v=f[ver[i]][h.l]; q.push(t); } if(h.l+1<=k&&f[ver[i]][h.l+1]>f[h.d][h.l]) { f[ver[i]][h.l+1]=f[h.d][h.l]; t.d=ver[i];t.l=h.l+1;t.v=f[ver[i]][h.l+1]; q.push(t); } } } }
void add(ll u,ll v,ll w) { ver[++tot]=v;wt[tot]=w; nxt[tot]=head[u];head[u]=tot; }
inline ll read() { ll ret=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9') {if(ch=='-') f=-f;ch=getchar();} while(ch>='0'&&ch<='9') {ret=(ret<<3)+(ret<<1)+ch-'0';ch=getchar();} return ret*f; }
void write(ll x) { static char buf[22];static ll len=-1; if(x>=0) { do{buf[++len]=x%10+48;x/=10;}while(x); } else { putchar('-'); do{buf[++len]=-(x%10)+48;x/=10;}while(x); } while(len>=0) putchar(buf[len--]); }
int main() {
n=read();m=read();k=read();
S=read();T=read();
for(ll i=1;i<=m;i++) { u=read();v=read();w=read(); add(u,v,w);add(v,u,w); }
dij();
ans=f[T][k]; for(ll i=0;i<k;i++) { ans=min(ans,f[T][i]); }
write(ans);
return 0; }
|