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; 在写松弛条件的时候. 如果用 ...
随机推荐
- C++三大特性之封装
原文地址:https://qunxinghu.github.io/2016/09/12/C++%20%E4%B8%89%E5%A4%A7%E7%89%B9%E6%80%A7%E4%B9%8B%E5%B ...
- 奇特的Local System权限(转载)
转载自:http://mp.weixin.qq.com/s?__biz=MzA3NTM1MzE4Nw==&mid=202597764&idx=1&sn=0cef1a40fb3c ...
- ubuntu,windows 卸载安装mysql
首先删除mysql: sudo apt-get remove mysql-* 1 然后清理残留的数据 dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dp ...
- 【Cocos2dx 3.3 Lua】滚动字幕
参考资料: http://blog.csdn.net/jackystudio/article/details/12991977 1.原理 通过调用update来更新位置达到 ...
- iOS 网易彩票-1框架搭建
仿网易彩票,最终要做成的效果如下: 一.分层搭建 1.新建一个项目,Lottery.只支持7.1以上坚屏. 2.将素材全部图片全部拉到相应的文件夹里. 3.选中Lottery--右键Show in F ...
- Java编写验证码
Java后台代码(CheckCodeServlet.java) package web; import java.awt.Color; import java.awt.Font; import jav ...
- ubuntu shell脚本出错 dash
今天在Ubuntu下调试代码,明明是正确的,却仍然报错,查了错误信息才知道:Ubuntu中默认不是bash,而是为了加快开机速度,使用了dash. dash中需要严格的语法,而且与bash语法不同.例 ...
- JQuery Form AjaxSubmit(options)在Asp.net中的应用注意事项
所需引用的JS: 在http://www.malsup.com/jquery/form/#download 下载:http://malsup.github.com/jquery.form.js 在ht ...
- c#null值加法运算
加号都是一个含义啊,操作数不同,加号重载的方法就不一样,当加号的左边或右边含有字符串的时候,总是返回一个不为空的字符串.当加号左右两边都是数值的时候,就会对其进行数学运算,null+任何数都为null ...
- ajax post 数组
ajax post 传递数组参数后台接收的为null,需要将其连接为字符串后传递 var url = "@Url.Action("CheckOutProduct", &q ...