ACM-ICPC 2018 南京赛区网络预赛 L. Magical Girl Haze

  • 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

题目来源

ACM-ICPC 2018 南京赛区网络预赛

 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

时间: 2024-11-06 19:54:41

ACM-ICPC 2018 南京赛区网络预赛 L. Magical Girl Haze的相关文章

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

题意:有n个地方,m条带权值的路,你有k次机会将一条路的权值变为0,求1到n的最短距离. 分析:这是一题分层dijkstra的模板题,因为k的大小不是很大,最坏的情况也就是10,而我们一般的最短路问题只是建一个平面的图. 而分层dijkstra是一个空间的.怎么操作呢? 举个简单的栗子 当数据如下是 n  m  k 3  2  1 1  2  5(路的起点1,终点2,权值5) 2  3  6 没有k的情况就是这样的 如果有k的情况,即1到2权值可能为0,2到3也可能为0,我们可以加一个平面表示他

ACM-ICPC 2018 南京赛区网络预赛 L题(分层最短路)

题目链接:https://nanti.jisuanke.com/t/31001 题目大意:给出一个含有n个点m条边的带权有向图,求1号顶点到n号顶点的最短路,可以使<=k条任意边的权值变为0. 样例输入 复制 1 5 6 1 1 2 2 1 3 4 2 4 3 3 4 1 3 5 6 4 5 2 样例输出 复制 3 解题思路:可以用两种做法,不过都差不多,应该算是同一种思路的不同写法.第一种是在建图时,将一个点拆成k个层次的点,应该总共有k+1层,每个相同层次的点按输入的边权连接,每个点可以向它

ACM-ICPC 2018 南京赛区网络预赛 Lpl and Energy-saving Lamps 线段树

目录 ACM-ICPC 2018 南京赛区网络预赛 Lpl and Energy-saving Lamps 线段树 题面 题意 思路 ACM-ICPC 2018 南京赛区网络预赛 Lpl and Energy-saving Lamps 线段树 题面 During tea-drinking, princess, amongst other things, asked why has such a good-natured and cute Dragon imprisoned Lpl in the

ACM-ICPC 2018 南京赛区网络预赛 E题

ACM-ICPC 2018 南京赛区网络预赛 E题 题目链接: https://nanti.jisuanke.com/t/30994 Dlsj is competing in a contest with n (0 < n \le 20)n(0<n≤20) problems. And he knows the answer of all of these problems. However, he can submit ii-th problem if and only if he has s

ACM-ICPC 2018 南京赛区网络预赛 J.Sum

Sum A square-free integer is an integer which is indivisible by any square number except 11. For example, 6 = 2 \cdot 36=2⋅3 is square-free, but 12 = 2^2 \cdot 312=22⋅3 is not, because 2^222 is a square number. Some integers could be decomposed into

计蒜客 ACM-ICPC 2018 南京赛区网络预赛 A. An Olympian Math Problem-数学公式题

A. An Olympian Math Problem 54.28% 1000ms 65536K Alice, a student of grade 66, is thinking about an Olympian Math problem, but she feels so despair that she cries. And her classmate, Bob, has no idea about the problem. Thus he wants you to help him.

ACM-ICPC 2018 焦作赛区网络预赛 L 题 Poor God Water

God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him that some sequence of eating will make them poisonous. Every hour, God Water will eat one kind of food among meat, fish and chocolate. If there are 33 

ACM-ICPC 2018 南京赛区网络预赛

A An Olympian Math Problem #include <bits/stdc++.h> using namespace std; typedef long long ll; ll T,n; int main(){ scanf("%lld",&T); while(T--){ scanf("%lld",&n); printf("%lld\n",n-1); } } B The writing on the w

ACM-ICPC 2018 南京赛区网络预赛 做题记录

比赛的时候被J题卡常然后B题自闭最后G题没调完 5题gg 现在补题进度:6/12 A. 题解: 高考数学题,裂项完之后答案就是n!-1 (n!)%n=0,所以就是n-1 1 #include<bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 int T; 5 ll n; 6 int main() 7 { 8 cin>>T; 9 while(T--) 10 { 11 cin>>n; 12 cout