ACM-ICPC 2018 南京赛区网络预赛 L. 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 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 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现