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
还是一个最短路 只是可以将其中的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 最短路+分层图的更多相关文章
- 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 ...
- 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 ...
- ACM-ICPC 2018 南京赛区网络预赛 - L Magical Girl Haze (分层迪杰斯特拉)
题意:N个点,M条带权有向边,求可以免费K条边权值的情况下,从点1到点N的最短路. 分析:K<=10,用dist[i][j]表示从源点出发到点i,免费j条边的最小花费.在迪杰斯特拉的dfs过程中 ...
- 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). ...
- ACM-ICPC 2018 南京赛区网络预赛 L题(分层最短路)
题目链接:https://nanti.jisuanke.com/t/31001 题目大意:给出一个含有n个点m条边的带权有向图,求1号顶点到n号顶点的最短路,可以使<=k条任意边的权值变为0. ...
- ACM-ICPC 2018 南京赛区网络预赛 L 【分层图最短路】
<题目链接> 题目大意: 有N个城市,这些城市之间有M条有向边,每条边有权值,能够选择K条边 边权置为0,求1到N的最短距离. 解题分析: 分层图最短路模板题,将该图看成 K+1 层图,然 ...
- ACM-ICPC 2018 南京赛区网络预赛 L题(分层图,堆优化)
题目链接: https://nanti.jisuanke.com/t/31001 超时代码: #include<bits/stdc++.h> using namespace std; # ...
- ACM-ICPC 2018 南京赛区网络预赛 L && BZOJ 2763 分层最短路
https://nanti.jisuanke.com/t/31001 题意 可以把k条边的权值变为0,求s到t的最短路 解析 分层最短路 我们建立k+1层图 层与层之间边权为0,i 向 i+1层转 ...
- 【ACM-ICPC 2018 南京赛区网络预赛 L】Magical Girl Haze
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 定义dis[i][j]表示到达i这个点. 用掉了j次去除边的机会的最短路. dis[1][0]= 0; 在写松弛条件的时候. 如果用 ...
随机推荐
- 高性能mysql 第1,2,3章。
一: 第一章 1:使用事务 start transaction; select * from t1; commit; 2:查看事务状态 mysql> show variables like 'a ...
- [LeetCode] 724. Find Pivot Index_Easy tag: Dynamic Programming
Given an array of integers nums, write a method that returns the "pivot" index of this arr ...
- Redis 十分钟快速入门
本教程是一个快速入门教程,所以Redis的命令只是简单介绍了几个常用的,如果有其他需求请求官网查看API 使用. 1. Redis简介 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的ke ...
- STA分析(六) cross talk and noise
在深亚微米技术(deep submicron)中,关于crosstalk和noise对design的signal integrate的影响越来越大.主要表现在glitch和对delay的影响. 1)m ...
- uva1330 在一个大的矩阵中寻找面积最大的子矩阵
大白书 P50页 #include <algorithm> #include <cstdio> using namespace std; ; int ma[maxn][maxn ...
- python+Django框架运用(三)
Django模型 模式指的是根据数据库中数据表的结构来创建出来的class,每一张表到Python中就是一个 class,表中的每一个列,到Python中就是class的一个属性. 在模型中可以完成对 ...
- Python: 字典列表: 通过某个字段将记录分组
问题:有一个字典或者实例的序列,想根据某个特定的字段比如date 来分组迭代访问. answer: itertools.groupby函数对于这样的数据分组操作非常实用 eg: rows = [{'a ...
- Spring,Struts2,MyBatis,Activiti,Maven,H2,Tomcat集成(四)——Activiti集成
1.添加Activiti Maven依赖: <!-- ==============================activiti=========================== --&g ...
- SpringMVC中controller返回图片(转)
本文转自:http://blog.csdn.net/u011637069/article/details/51112187 SpringMVC中controller通过返回ModelAndView然后 ...
- Linux基础命令---dump
dump 检查ext2/3/4文件系统,确定哪些文件需要备份,这些需要备份的文件将会被复制到指定的磁盘或者其他存储介质.dump检查Ext 2/3/4文件系统上的文件,并确定哪些文件需要备份.这些文件 ...