类似题解

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. Python装饰器与面向切面编程(转)

    add by zhj: 装饰器的作用是将代码中可以独立的功能独立出来,实现代码复用,下面那个用于统计函数运行时间的装饰器就是很好的例子,我们不用修改原有的函数和调用原有函数的地方,这遵循了开闭原则.装 ...

  2. sublime 使用总结

    不管你用什么编辑,sublime是首选编辑器,就是sublime淘汰,但已成为标准.例如:atom,几乎等同于sublime,及其他可以几乎调成到sublime操作方式. 一.常用插件 插件搜索地址: ...

  3. 共享访问在.NET中的编程实现

    转载:http://blog.csdn.net/zhzuo/article/details/1732937 共享访问在.NET中的编程实现 发布日期:2007-08-08 | 更新日期:2009-03 ...

  4. mysql 内置功能 视图介绍

    之前的多表查询本质是把多张有关系的表连接在一起组成一张虚拟表,从而进行查询 视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名], 用户使用时只需使用[名称]即 ...

  5. pem转pfx

    openssl req -new -key privkey.pem -out root.csr openssl x509 -req -days -sha1 -extensions v3_ca -sig ...

  6. 【Rewrite重定向】Nginx使用rewrite重新定向

    使用nginx做重新定向. nginx参考网址:http://blog.sina.com.cn/s/blog_97688f8e0100zws5.html 语法规则: location [=|~|~*| ...

  7. sql查询一列 重复的数据

    select * from 表 where num  in(select num  from 表 group by num having count(num)>1)

  8. sql distinct去除重复

    distinct select  distinct * from  table1 或者用 group  by

  9. mybatis例子

    mybatis的mapper不允许重载,因为它需要通过方法名称[不加签名]去查找需要执行的sql 1.批量删除 <delete id="deletePlanLocations" ...

  10. Object-C-NSArray

    NSArray *fruitArray=[[NSArray alloc] initWithObjects:@"apple",@"banana",@"p ...