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
| #include<algorithm> #include<iostream> #include<iomanip> #include<cstring> #include<cstdlib> #include<vector> #include<cstdio> #include<cmath> #include<queue> using namespace std; inline const int Get_Int() { int num=0,bj=1; char x=getchar(); while(x<'0'||x>'9') { if(x=='-')bj=-1; x=getchar(); } while(x>='0'&&x<='9') { num=num*10+x-'0'; x=getchar(); } return num*bj; } const int maxn=10005; int n,m,s,t,k,dist[maxn][15],vst[maxn][15]; struct QueNode { int d,u,v; bool operator < (const QueNode& b) const { return d>b.d; } }; struct Edge { int from,to,dist; }; vector<Edge>edges[maxn]; void AddEdge(int x,int y,int v) { edges[x].push_back((Edge) { x,y,v }); } void Dijkstra(int s) { for(int i=1; i<=n; i++) for(int j=0; j<=k; j++) dist[i][j]=0x7fffffff/2; dist[s][0]=0; priority_queue<QueNode>Q; Q.push((QueNode) { 0,s,0 }); while(!Q.empty()) { QueNode Now=Q.top(); Q.pop(); if(vst[Now.u][Now.v])continue; vst[Now.u][Now.v]=1; for(Edge& e:edges[Now.u]) { int Next=e.to; if(dist[Next][Now.v]>dist[Now.u][Now.v]+e.dist) { dist[Next][Now.v]=dist[Now.u][Now.v]+e.dist; Q.push((QueNode) { dist[Next][Now.v],Next,Now.v }); } if(Now.v<k&&dist[Next][Now.v+1]>dist[Now.u][Now.v]) { dist[Next][Now.v+1]=dist[Now.u][Now.v]; Q.push((QueNode) { dist[Next][Now.v+1],Next,Now.v+1 }); } } } } int main() { n=Get_Int(); m=Get_Int(); k=Get_Int(); s=Get_Int(); t=Get_Int(); for(int i=1; i<=m; i++) { int x=Get_Int(),y=Get_Int(),v=Get_Int(); AddEdge(x,y,v); AddEdge(y,x,v); } Dijkstra(s); int ans=0x7fffffff/2; for(int i=0; i<=k; i++)ans=min(ans,dist[t][i]); printf("%d\n",ans); return 0; }
|