ACM-ICPC 2018 南京赛区网络预赛 L.Magical Girl Haze(分层最短路)

时间:2024-01-18 14:30:08

There are N cities in the country, and M directional roads from u to v(1≤u,v≤n). Every road has a distance ci. Haze is a Magical Girl that lives in City 1, she can choose no more than K roads and make their distances become 0. Now she wants to go to City N, please help her calculate the minimum distance.

Input
The first line has one integer T(1≤T≤5), then following T cases.
For each test case, the first line has three integers N,M and K.
Then the following M lines each line has three integers, describe a road, Ui,Vi,Ci​. There might be multiple edges between u and v.
It is guaranteed that N≤100000,M≤200000,K≤10,0≤Ci≤1e9. There is at least one path between City 1 and City N.

Output
For each test case, print the minimum distance.

样例输入
1
5 6 1
1 2 2
1 3 4
2 4 3
3 4 1
3 5 6
4 5 2

样例输出
3

题意

给你一个图,问你从1到N最多让K条边为0的最短路

题解

d[i][j]代表到j点已经删去i条边的最小值

跑一遍dij

代码

 #include<bits/stdc++.h>
using namespace std; #define ll long long
const int maxn=1e5+,maxm=2e5+;
ll d[][maxn];
int n,m,k;
int head[maxn],cnt;
struct edge
{
int v,next;
ll w;
}edges[maxm];
struct node
{
ll w;
int v,k;
bool operator<(const node &D)const{
return w>D.w;
}
};
void dij()
{
memset(d,0x3f,sizeof d);
priority_queue<node>q;
q.push({,,});
d[][]=;
while(!q.empty())
{
auto u=q.top();q.pop();
if(u.v==n)return;
for(int i=head[u.v];i!=-;i=edges[i].next)
{
edge &v=edges[i];
if(u.k<k&&d[u.k+][v.v]>d[u.k][u.v])
q.push({d[u.k+][v.v]=d[u.k][u.v],v.v,u.k+});
if(d[u.k][v.v]>d[u.k][u.v]+v.w)
q.push({d[u.k][v.v]=d[u.k][u.v]+v.w,v.v,u.k});
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(head,-,sizeof head);
cnt=;
scanf("%d%d%d",&n,&m,&k);
for(int i=,u,v,w;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
edges[cnt].v=v;
edges[cnt].w=1LL*w;
edges[cnt].next=head[u];
head[u]=cnt++;
}
dij();
ll minn=0x3f3f3f3f3f3f3f3f;
for(int i=;i<=k;i++)
minn=min(minn,d[i][n]);
printf("%lld\n",minn);
}
return ;
}