(i,j)表示当前在结点j,还剩下i次使用魔法的机会。
以(i,j)为结点建图,求最短路。
//#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<iostream> #include<sstream> #include<cmath> #include<climits> #include<string> #include<map> #include<queue> #include<vector> #include<stack> #include<set> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pii; #define pb(a) push(a) #define INF 0x1f1f1f1f #define lson idx<<1,l,mid #define rson idx<<1|1,mid+1,r #define PI 3.1415926535898 template<class T> T min(const T& a,const T& b,const T& c) { return min(min(a,b),min(a,c)); } template<class T> T max(const T& a,const T& b,const T& c) { return max(max(a,b),max(a,c)); } void debug() { #ifdef ONLINE_JUDGE #else freopen("in.txt","r",stdin); //freopen("d:\out1.txt","w",stdout); #endif } int getch() { int ch; while((ch=getchar())!=EOF) { if(ch!=' '&&ch!=' ')return ch; } return EOF; } struct HeapNode { int d,u; bool operator < (const HeapNode &ant) const { return d>ant.d; } }; struct Edge { int from,to; int dist; }; const int maxn=1205; struct Dijksta { int n; vector<int> g[maxn]; vector<Edge> edge; int done[maxn]; int d[maxn]; void init(int n) { this->n=n; for(int i=0;i<=n;i++) g[i].clear(); edge.clear(); } void add(int u,int v,int w) { Edge e=(Edge){u,v,w}; edge.push_back(e); g[u].push_back(edge.size()-1); } void solve(int s) { for(int i=0;i<=n;i++)d[i]=INF; memset(done,0,sizeof(done)); priority_queue<HeapNode> q; q.push((HeapNode){0,s}); d[s]=0; while(!q.empty()) { HeapNode x=q.top();q.pop(); if(done[x.u])continue; int u=x.u; done[u]=1; for(int i=0;i<g[u].size();i++) { Edge &e=edge[g[u][i]]; if(d[u]+e.dist<d[e.to]) { d[e.to]=d[u]+e.dist; q.push((HeapNode){d[e.to],e.to}); } } } } }; Dijksta solver; int g1[101][101],g2[101][101]; int A,B,L,K,M; void floyd() { for(int k=1;k<=A+B;k++) for(int i=1;i<=A+B;i++) for(int j=1;j<=A+B;j++) if(k<=A) g1[i][j]=min(g1[i][j],g1[i][k]+g1[k][j]); } int id[11][111]; int vcnt; int ID(int k,int u) { int &x=id[k][u]; if(x==0)x=++vcnt; return x; } void construct() { memset(id,0,sizeof(id)); vcnt=0; solver.init((A+B)*(K+1)); for(int i=K;i>=0;i--) for(int u=1;u<=A+B;u++) { for(int v=1;v<=A+B;v++)if(g2[u][v]!=INF) solver.add(ID(i,u),ID(i,v),g2[u][v]); if(i!=0)for(int v=1;v<=A+B;v++)if(g1[u][v]<=L) solver.add(ID(i,u),ID(i-1,v),0); } } int main() { int t; scanf("%d",&t); for(int ca=1;ca<=t;ca++) { scanf("%d%d%d%d%d",&A,&B,&M,&L,&K); memset(g1,INF,sizeof(g1)); for(int i=1;i<=M;i++) { int u,v,w; scanf("%d%d%d",&u,&v,&w); g1[u][v]=g1[v][u]=w; } memcpy(g2,g1,sizeof(g1)); floyd(); construct(); solver.solve(ID(K,A+B)); int ans=INF; for(int i=0;i<=K;i++) ans=min(ans,solver.d[ID(i,1)]); printf("%d ",ans); } return 0; }