题目链接

Problem Description
There are $$$n$$$ intersections in Bytetown, connected with $$$m$$$ one way streets. Little $$$Q$$$ likes sport walking very much, he plans to walk for q days. On the $$$i$$$-th day, Little $$$Q$$$ plans to start walking at the $$$s_i$$$-th intersection, walk through at least k$$$_i$$$ streets and finally return to the $$$t_i$$$-th intersection.
Little $$$Q$$$'s smart phone will record his walking route. Compared to stay healthy, Little $$$Q$$$ cares the statistics more. So he wants to minimize the total walking length of each day. Please write a program to help him find the best route.
Input
The first line of the input contains an integer $$$T(1≤T≤10)$$$, denoting the number of test cases.
In each test case, there are 2 integers $$$n,m(2≤n≤50,1≤m≤10000)$$$ in the first line, denoting the number of intersections and one way streets.
In the next m lines, each line contains 3 integers $$$u_i,v_i,w_i(1≤u_i,v_i≤n,u_i≠v_i,1≤w_i≤10000)$$$, denoting a one way street from the intersection $$$u_i$$$ to $$$v_i$$$, and the length of it is $$$w_i$$$.
Then in the next line, there is an integer $$$q(1≤q≤100000)$$$, denoting the number of days.
In the next $$$q$$$ lines, each line contains 3 integers $$$s_i,t_i,k_i(1≤s_i,t_i≤n,1≤k_i≤10000)$$$, describing the walking plan.
Output
For each walking plan, print $$$a$$$ single line containing an integer, denoting the minimum total walking length. If there is no solution, please print -1.
Sample Input
2

3 3
1 2 1
2 3 10
3 1 100
3
1 1 1
1 2 1
1 3 1
2 1
1 2 1
1
2 1 1
Sample Output
111
1
11
-1
题意
有一个n个点,m条有向边的图,每次询问求从s到t至少经过k条边的最短路径长度
分析
节点最多只有50个,为了高效的处理如此多的询问,肯定需要预处理。首先需要知道的是,有一个数据结构,特别适合存储在图上“转移几次的最短距离”,那就是邻接矩阵。如果邻接矩阵$$$A$$$用$$$A_{ij}=0/1$$$来记录$$$i, j$$$之间是否有一条有向边,那么$$$A\times A$$$的结果也是一个矩阵,$$$A^2_{ij}$$$的含义是从$$$i$$$出发走两步到$$$j$$$的方案个数。邻接矩阵的乘法之所以这么神奇,原理在于矩阵乘法的过程:$$$$$${A^2}_{ij}=\sum_{k=1}^n A_{ik}\times A_{kj}$$$$$$
$$$A_{ik}$$$ 可以理解为从$$$i$$$走到$$$k$$$的方案个数
$$$A_{kj}$$$ 可以理解为从$$$k$$$走到$$$j$$$的方案个数
那么最后求和的过程就是把$$$i$$$到$$$j$$$的所有方案求和了。以此类推,就能算出所有的方案个数。
回到这道题中,如果把矩阵乘法的细节修改一下,让$$$A_{ik}$$$ 记录$$$i$$$到$$$k$$$的最短距离,$$$A_{kj}$$$记录$$$k$$$到$$$j$$$的最短距离,计算$$${A^2}_{ij}$$$的过程变为取最小值,也就是$$$$$${A^2}_{ij}=min_{k}(A_{ik}+A_{kj})$$$$$$,就可以用类似矩阵乘法的思想,很快得到转移k步任意两点之间最短距离的表。
总结
text

2018 杭电多校3 - M.Walking Plan的更多相关文章

  1. hdu6312 2018杭电多校第二场 1004 D Game 博弈

    Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  2. 2018 杭电多校1 - Chiaki Sequence Revisited

    题目链接 Problem Description Chiaki is interested in an infinite sequence $$$a_1,a_2,a_3,...,$$$ which i ...

  3. 2018 杭电多校2 - Naive Operations

    题目链接 Problem Description In a galaxy far, far away, there are two integer sequence a and b of length ...

  4. 2018 杭电多校1 - Distinct Values

    题目链接 Problem Description Chiaki has an array of n positive integers. You are told some facts about t ...

  5. 2018杭电多校第二场1003(DFS,欧拉回路)

    #include<bits/stdc++.h>using namespace std;int n,m;int x,y;int num,cnt;int degree[100007],vis[ ...

  6. 2018杭电多校第六场1009(DFS,思维)

    #include<bits/stdc++.h>using namespace std;int a[100010];char s[20];int zhiren[100010];vector& ...

  7. 2018杭电多校第五场1002(暴力DFS【数位】,剪枝)

    //never use translation#include<bits/stdc++.h>using namespace std;int k;char a[20];//储存每个数的数值i ...

  8. 2018杭电多校第三场1003(状态压缩DP)

    #include<bits/stdc++.h>using namespace std;const int mod =1e9+7;int dp[1<<10];int cnt[1& ...

  9. 可持久化线段树的学习(区间第k大和查询历史版本的数据)(杭电多校赛第二场1011)

    以前我们学习了线段树可以知道,线段树的每一个节点都储存的是一段区间,所以线段树可以做简单的区间查询,更改等简单的操作. 而后面再做有些题目,就可能会碰到一种回退的操作.这里的回退是指回到未做各种操作之 ...

随机推荐

  1. browser-cookies 一个管理cookies的插件,好用

    一.browser-cookies 地址:https://github.com/voltace/browser-cookies 用法 存放cookies是 cookies.set('firstName ...

  2. WebRTC中Android Demo中的远程视频流的获取到传输

    1.CallActivity#onCreate 执行startCall开始连接或创建房间 2.WebSocketClient#connectToRoom 请求一次服务器 3.回调到CallActivi ...

  3. Jedis+Redis+spring缓存

    Redis程序使用它?Jedis 访问redis java api Redis-server & //后台运行防火墙要关闭 ts-parent的pom.xml加上jedis依赖 <dep ...

  4. 成都Uber优步司机奖励政策(1月12日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  5. 青岛Uber优步司机奖励政策(1月11日~1月17日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  6. 使用element-ui 的table 渲染数据遇到的问题

    通常我们使用一个table 来渲染服务的返回来的数据时,数据结构一般都是按row 来返回的,并且表头也是固定的 但是如果接口返回的数据结构不是我们想要的,表头也不确定时,我们该如何解析数据,将数据进行 ...

  7. Putty远程连接Ubuntu14.04

    步骤一.在ubuntu系统中安装ssh,可使用如下的命令进行安装: sudo apt-get install openssh-server 步骤二.为了保险起见,安装完成后重启一下ssh服务,命令如下 ...

  8. 使用 adb 命令一次性为多个设备安装 apk

    使用 adb 命令一次性为多个设备安装 apk 原创 2016年07月15日 10:40:53 3154 命令简介 adb install [-lrtsdg] (file) 把包文件推送到设备上并安装 ...

  9. JMeter常用元器件

    测试计划, 是整个工程的根节点, 可以取别名, 并添加注释, 里面的设置是全局变量: 线程组, 是一组线程的集合, 可以取别名, 并添加注释, 里面的设置只对本线程组有效: HTTP请求, 也就是取样 ...

  10. HTTP基本定义

    一.网络的简单定义: 1.http:是www服务器传输超文本向本地浏览器的传输协议.(应用层) 2.IP:是计算机之间相互识别通信的机制.(网络层) 3.TCP:是应用层通信之间通信.(传输层) IP ...