- 262144K
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 TT cases.
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
题目来源
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <string> 5 #include <utility> 6 #include <algorithm> 7 #include <vector> 8 #include <queue> 9 #include <stack> 10 using namespace std; 11 #define N 100009 12 #define M 200009 13 #define lowbit(x) x&(-x) 14 #define ll long long 15 const ll inf =9e18; 16 int head[N],t,n,m,k,cnt; 17 ll dp[N][15]; 18 struct Edge{ 19 int from,to,nex; 20 ll w; 21 }e[M*2];//当然本题不用*2(有向图) 22 struct Node{ 23 int a,b;// a:终点 ,b : 已经用了几次免费路 24 ll dis;//1到a的当前最短距离 25 bool operator <(const Node &p)const{ 26 return dis>p.dis;//因此下面只能用priority_queue<Node>que; 27 //return dis<p.dis; 是错的,不可以这么定义 28 } 29 }; 30 void init() 31 { 32 for(int i=0;i<=n;i++){ 33 head[i]=-1; 34 } 35 cnt=0; 36 } 37 void add(int u,int v,ll val)// ll val 38 { 39 e[cnt].from=u; 40 e[cnt].to=v; 41 e[cnt].nex=head[u]; 42 e[cnt].w=val; 43 head[u]=cnt++; 44 } 45 void bfs() 46 { 47 for(int i=0;i<=n;i++) 48 { 49 for(int j=0;j<=15;j++){ 50 dp[i][j]=inf; 51 } 52 } 53 dp[1][0]=0;//dp[i][j] :从1到i ,已经有j次免费的路的最短路径 54 priority_queue<Node>que; 55 que.push(Node{1,0,0}); 56 while(!que.empty()){ 57 Node tmp=que.top(); 58 que.pop(); 59 int u=tmp.a; 60 int b=tmp.b; 61 for(int i=head[u];i!=-1;i=e[i].nex){ 62 int v=e[i].to; 63 if(dp[v][b]>tmp.dis+e[i].w){//这条路不当作免费路 64 dp[v][b]=tmp.dis+e[i].w; 65 que.push(Node{v,b,dp[v][b]}); 66 } 67 if(b+1<=k){ 68 if(dp[v][b+1]>tmp.dis){//这条路当作免费路 69 dp[v][b+1]=tmp.dis; 70 que.push(Node{v,b+1,tmp.dis}); 71 } 72 } 73 } 74 } 75 } 76 int main() 77 { 78 scanf("%d",&t); 79 while(t--) 80 { 81 scanf("%d%d%d",&n,&m,&k); 82 init(); 83 int u,v; 84 ll w; 85 for(int i=0;i<m;i++) 86 { 87 scanf("%d%d%lld",&u,&v,&w); 88 add(u,v,w); 89 } 90 bfs(); 91 printf("%lld\n",dp[n][k]); 92 } 93 return 0; 94 }
原文地址:https://www.cnblogs.com/tingtin/p/9588563.html