参考大佬博客 :https://blog.csdn.net/godjing007/article/details/97683174
题意大致如下 给你一个图你可以将k条边的权值变成0,求最短路
ac代码如下
#include <bits/stdc++.h>
typedef long long ll;
typedef long long ld;
using namespace std;
const ll maxn = 1e5 + 7;
ll n,m,k;
struct node
{
ll x,step,num;
node(ll xx,ll ste,ll nu)
{
x = xx;
step = ste;
num = nu;
}
};
bool operator < (const node a,const node b)
{
return a.step > b.step;
}
vector<pair<ll,ll> > mp[maxn];
ll dis[maxn][11];
void dij()
{
priority_queue<node> q;
q.push(node(1,0,0));
memset(dis,0x3f,sizeof(dis));
dis[1][0] = 0;
bool flag[maxn][11] = {0};
while(!q.empty())
{
node te = q.top();
q.pop();
if(flag[te.x][te.num])continue;
flag[te.x][te.num] = 1;
for(int i = 0; i < mp[te.x].size(); i ++)
{
ll to = mp[te.x][i].first;
ll val = mp[te.x][i].second;
if(dis[to][te.num] > te.step + val && !flag[to][te.num])
{
dis[to][te.num] = te.step + val;
q.push(node(to,dis[to][te.num],te.num));
}
if(te.num + 1 <= k)
{
if(dis[to][te.num + 1] > te.step && !flag[to][te.num])
{
dis[to][te.num + 1] = te.step;
q.push(node(to,dis[to][te.num + 1],te.num + 1));
}
}
}
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
ll t;
cin >> t;
while(t--)
{
cin >> n >> m >> k;
for(int i = 1; i <= n; i ++)
{
mp[i].clear();
}
for(int i = 1; i <= m; i ++)
{
ll x,y,z;
cin >> x >> y >> z;
mp[x].push_back(make_pair(y,z));
}
dij();
ll mi = 1e18;
for(int i = 0; i <= k; i ++)
{
mi = min(dis[n][i],mi);
}
cout << mi << endl;
}
}