题目大意就是:马里奥有个可以用K次的魔法鞋,然后每次魔法鞋可以穿过不超过L米,并且不可以穿过城堡,问最短时间。
思路:用dp[i][j]表示,到第i个点,还剩j次魔法鞋使用机会的最小时间。
这里预处理是用Floyd做的,floyd的本质就是枚举一个中间点,然后再枚举两边,所以只要中间这个k是城堡就直接跳过,不是的话就跑floyd。
#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
#include<stdio.h>
#include<queue>
using namespace std;
const int maxn=120;
struct node
{
int to;
int w;
int rem;
bool operator < (node tar) const
{
if(tar.w>w) return true;
return false;
}
};
int fly[maxn][maxn];
vector<node> G[maxn];
int dis[maxn][12];
int A,B,M,L,K;
void init()
{
memset(fly,0x3f,sizeof(fly));
memset(dis,0x3f,sizeof(dis));
for(int i=0;i<maxn;i++) G[i].clear();
}
void floyd()
{
int ns=A+B;
for(int k=1;k<=ns;k++)
{
if(k>=A+1) continue;//???????
for(int i=1;i<=ns;i++)
{
for(int j=1;j<=ns;j++)
{
fly[i][j]=min(fly[i][j],fly[i][k]+fly[k][j]);
}
}
}
}
void Dijstrka()
{
memset(dis,0x3f,sizeof(dis));
dis[A+B][K]=0;
node temp;
temp.to=A+B;
temp.w=0;
temp.rem=K;
priority_queue<node> PQ;
PQ.push(temp);
while(PQ.empty()==false)
{
int now=PQ.top().to;
int rem=PQ.top().rem;
int sw=PQ.top().w;
PQ.pop();
int size=G[now].size();
for(int j=0;j<size;j++)
{
int to=G[now][j].to;
int w=G[now][j].w;
if(dis[to][rem]>dis[now][rem]+w)
{
dis[to][rem]=dis[now][rem]+w;
node newone;
newone.w=dis[to][rem];
newone.rem=rem;
newone.to=to;
PQ.push(newone);
}
}
if(rem<1) continue;
//??????
for(int j=1;j<=A+B;j++)
{
if(fly[now][j]>L) continue;//???
if(dis[j][rem-1]>sw)
{
dis[j][rem-1]=sw;
node newone;
newone.w=sw;
newone.to=j;
newone.rem=rem-1;
PQ.push(newone);
}
}
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
init();
cin>>A>>B>>M>>L>>K;
for(int i=0;i<M;i++)
{
int a,b,w;
scanf("%d %d %d",&a,&b,&w);
fly[a][b]=fly[b][a]=min(fly[a][b],w);
node temp;
temp.to=b;
temp.w=w;
G[a].push_back(temp);
temp.to=a;
G[b].push_back(temp);
}
floyd();
Dijstrka();
int ans=0x3f3f3f3f;
for(int i=K;i>=0;i--)
{
ans=min(ans,dis[1][i]);
}
printf("%d\n",ans);
}
return 0;
}