类似题解

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
还是一个最短路 只是可以将其中的k条路 权值变为0,那么就可以 用一个d[maxn][K] 跑K次最短路 就能得到结果了,算法思路并不复杂
在此认识了 链式前向星,以及 爆int 之类的 问题,另外本题 卡Vector,所以不得不用 前向星
#include <bits/stdc++.h>
using namespace std; typedef long long ll;
typedef pair<ll,int> pli;
const int N = +;
const int M = +; int n,m,k,tot,head[N];
ll d[N][];bool vis[N][]; struct node {
int to,nex,val;
}mp[M]; void init() {
tot = ;
memset(head,,sizeof(head));
memset(vis,,sizeof(vis));
} void addedge(int u,int v,int val) {
tot++;
mp[tot].to = v;
mp[tot].nex = head[u];
mp[tot].val = val;
head[u] = tot;
} void solve() {
priority_queue<pli, vector<pli>, greater<pli> > que;
//while(que.size()) que.pop();
memset(d,0x3f,sizeof(d));
d[][] = ; que.push(pli(, ));
while (!que.empty()) {
int u = que.top().second;
ll dis = que.top().first;
int level = u/(n+); u %= (n+);
que.pop();
if(vis[u][level]) continue;
vis[u][level] = ;
for(int i=head[u]; i; i=mp[i].nex) {
int v = mp[i].to;
if(dis + mp[i].val <= d[v][level]) {
d[v][level] = dis + mp[i].val;
que.push({d[v][level], level*(n+)+v});
}
if(level==k) continue;
if(dis <= d[v][level+]) {
d[v][level+] = dis;
que.push({dis, (level+)*(n+)+ v});
}
}
}
cout << d[n][k] <<endl;
} int main () {
freopen("in.txt","r",stdin);
int T; scanf("%d", &T);
while(T--) {
init();
scanf("%d %d %d", &n, &m, &k);
for(int i=; i<=m; i++) {
int u,v,val;
scanf("%d %d %d", &u,&v,&val);
addedge(u,v,val);
}
solve();
}
return ;
}

ACM-ICPC 2018 南京赛区网络预赛 L. Magical Girl Haze 最短路+分层图的更多相关文章

  1. 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 ...

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

    There are N cities in the country, and M directional roads from u to v(1≤u,v≤n). Every road has a di ...

  3. ACM-ICPC 2018 南京赛区网络预赛 - L Magical Girl Haze (分层迪杰斯特拉)

    题意:N个点,M条带权有向边,求可以免费K条边权值的情况下,从点1到点N的最短路. 分析:K<=10,用dist[i][j]表示从源点出发到点i,免费j条边的最小花费.在迪杰斯特拉的dfs过程中 ...

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

    There are NN cities in the country, and MMdirectional roads from uu to v(1\le u, v\le n)v(1≤u,v≤n). ...

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

    题目链接:https://nanti.jisuanke.com/t/31001 题目大意:给出一个含有n个点m条边的带权有向图,求1号顶点到n号顶点的最短路,可以使<=k条任意边的权值变为0. ...

  6. ACM-ICPC 2018 南京赛区网络预赛 L 【分层图最短路】

    <题目链接> 题目大意: 有N个城市,这些城市之间有M条有向边,每条边有权值,能够选择K条边 边权置为0,求1到N的最短距离. 解题分析: 分层图最短路模板题,将该图看成 K+1 层图,然 ...

  7. ACM-ICPC 2018 南京赛区网络预赛 L题(分层图,堆优化)

    题目链接: https://nanti.jisuanke.com/t/31001 超时代码: #include<bits/stdc++.h> using namespace std; # ...

  8. ACM-ICPC 2018 南京赛区网络预赛 L && BZOJ 2763 分层最短路

    https://nanti.jisuanke.com/t/31001 题意 可以把k条边的权值变为0,求s到t的最短路 解析  分层最短路  我们建立k+1层图 层与层之间边权为0,i 向 i+1层转 ...

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

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 定义dis[i][j]表示到达i这个点. 用掉了j次去除边的机会的最短路. dis[1][0]= 0; 在写松弛条件的时候. 如果用 ...

随机推荐

  1. JavaScript如何实现拖放功能

    1.在学习ExtJs时,对其拖放功能感到很陌生,然后找了个拖放功能实现. 转载地址 2.拖拽的基本原理就是根据鼠标的移动来移动被拖拽的元素.鼠标的移动也就是x.y坐标的变化:元素的移动就是style. ...

  2. 【Python】如何切换浏览器的tap页?

    当点击浏览器当前页面中的某个链接后自动弹出一个新的浏览器的tap页面时,浏览器正常都会在当前操作的tap页面右1位置打开新弹出的tap页. 当前selenium还不具备自动切换页面后窗口切换功能.需要 ...

  3. google浏览器mac上跨域问题解决

    open -n /Applications/Google\ Chrome.app/ --args --disable-web-security --user-data-dir=/Users/ /Use ...

  4. byte处理的几种方法

    /** * 字符串转16进制byte * @param * @return * @throws Exception * @author hw * @date 2018/10/19 9:47 */ pr ...

  5. vue学习七之Axios

    JQuery时代,我们使用ajax向后台提交数据请求,Vue时代,Axios提供了前端对后台数据请求的各种方式. 什么是Axios Axios是基于Promise的Http客户端,可以在浏览器和nod ...

  6. 一行代码彻底禁用WordPress缩略图自动裁剪功能

    记得在博客分享七牛缩略图教程的时候,提到过 WordPress 默认会将上传的图片裁剪成多个,不但占用磁盘空间,也会拖慢网站性能,相当闹心! 当时也提到了解决办法: ①.关闭主题自带缩略图裁剪功能(若 ...

  7. java和python中的string和int数据类型的转换

    未经允许,禁止转载!!! 在平时写代码的时候经常会用到string和int数据类型的转换 由于java和python在string和int数据类型转换的时候是不一样的 下面总结了java和python ...

  8. JSTL—标签

    什么是JSTL标签? Jsp标准标签库(JSP Standerd Tag Library) JSTL的优点是什么? 1) 提供一组标准的标签 2)可用于编写动态功能 使用JSTL的步骤? 1)引入ja ...

  9. Java求解迷宫问题:栈与回溯算法

    摘要: 使用栈的数据结构及相应的回溯算法实现迷宫创建及求解,带点JavaGUI 的基础知识. 难度: 中级 迷宫问题是栈的典型应用,栈通常也与回溯算法连用. 回溯算法的基本描述是: (1)  选择一个 ...

  10. python中repr和eval可以用来在数据结构和字符串间互转

    在这个功能上,repr和str的作用一样,把一个数据结构转换成字符串,例如: >>> str([1,2,3,4])'[1, 2, 3, 4]' >>> repr([ ...