Magical Girl Haze
There are NN cities in the country, and MM directional roads from uu to v(1\le u, v\le n)v(1≤u,v≤n). Every road has a distance c_ici. Haze is a Magical Girl that lives in City 11, she can choose no more than KK roads and make their distances become 00. Now she wants to go to City NN, please help her calculate the minimum distance.
Input
The first line has one integer T(1 \le T\le 5)T(1≤T≤5), then following TTcases.
For each test case, the first line has three integers N, MN,M and KK.
Then the following MM lines each line has three integers, describe a road, U_i, V_i, C_iUi,Vi,Ci. There might be multiple edges between uu and vv.
It is guaranteed that N \le 100000, M \le 200000, K \le 10N≤100000,M≤200000,K≤10,
0 \le C_i \le 1e90≤Ci≤1e9. There is at least one path between City 11 and City NN.
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
题目来源
为了方便理解,稍微转换一下题意
n个城市,m条有向路,每条路距离为c,最多可以把k条路距离变为0,求从1到n的最小距离
将图的点分为k+1层,也就是说,在每个点,可以通过某一条路到达另一个点,距离w,也可以直接坐电梯到下一层的另一个点,距离视为0,由于只有k层,所以最多只能把k条路距离变为0(坐k次电梯,按我的理解,也可以不坐电梯,所以最后在每一层的终点找出最短距离)
图建好后用dijkstra堆优化找出最短路径
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<string.h>
#include<iostream>
#include<fstream>
#include<math.h>
#include<stack>
#include<queue>
#include<bitset>
#include<utility>
#include<set>
#include<map>
#define INF 0x7f7f7f7f
using namespace std;
typedef long long ll;
const int N=3000000;
int t;
int n,m,k;
struct P{
ll i;
ll dist;
friend bool operator<(P a,P b){
return a.dist>b.dist;
}
};
struct Edge{
ll y;
ll w;
ll nt;
};
Edge e[N<<1];
int head[N];
ll dist[N];
int cnt;
void add(ll x,ll y,ll w){
e[cnt].y=y;
e[cnt].w=w;
e[cnt].nt=head[x];
head[x]=cnt;
cnt++;
}
void dij(int x){
priority_queue<P> q;
memset(dist,INF,sizeof(dist));
dist[x]=0;
P s;
s.i=x;
s.dist=dist[0];
q.push(s);
while(!q.empty()){
P old=q.top();
q.pop();
int a=old.i;
for(int i=head[a];i!=-1;i=e[i].nt){
int b=e[i].y;
if(dist[b]>dist[a]+e[i].w){
dist[b]=dist[a]+e[i].w;
P now;
now.i=b;
now.dist=dist[b];
q.push(now);
}
}
}
}
int main(){
scanf("%d",&t);
while(t--){
scanf("%d%d%d",&n,&m,&k);
memset(head,-1,sizeof(head));
if(k>=m){
printf("0\n");
continue;
}
ll x,y,w;
cnt=0;
for(int i=1;i<=m;i++){
scanf("%lld%lld%lld",&x,&y,&w);
for(int j=0;j<=k;j++){
add(x+j*n,y+j*n,w);
if(j!=k){
add(x+j*n,y+(j+1)*n,0);
}
}
}
dij(1);
ll ans=INF;
for(int i=0;i<=k;i++){
ans=min(ans,dist[n+i*n]);
}
printf("%lld\n",ans);
}
return 0;
}
打懵圈了,不知道为什么最后做着做着理解成要把前k步变为0,还一直在找错~%…,# *'☆&℃$︿★? (@_@;)