题目链接

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. Node.js Express+Mongodb 项目实战

    Node.js Express+Mongodb 项目实战 这是一个简单的商品管理系统的小项目,包含的功能还算挺全的,项目涵盖了登录.注册,图片上传以及对商品进行增.删.查.改等操作,对于新手来说是个很 ...

  2. Java:xxx is not an enclosing class

    1. 错误原因 该错误一般出现在对内部类进行实例化时,例如 public class A{ public class B{ } } 此时B是A的内部类,如果我们要使用如下语句实例化一个B类的对象: A ...

  3. LeetCode:39. Combination Sum(Medium)

    1. 原题链接 https://leetcode.com/problems/combination-sum/description/ 2. 题目要求 给定一个整型数组candidates[ ]和目标值 ...

  4. PHP用url传递数组

    数组传递这么写:   echo "<a href=2.php?info=".base64_encode(serialize($information))." > ...

  5. STM32的GUI库使用

    1. 实验平台使用百为的STM32F103开发板 2. 例程目录\百为stm32开发板光盘\stm32_gui_lib\Project\Embedded_GUI_Example\EWARM 3. 直接 ...

  6. python3读取csv文件

    代码如下 import csv with open('D:\\abc\\userinfo.csv',newline='') as f: reader = csv.reader(f) for row i ...

  7. leetcode-数数并说

     数数并说     报数序列是指一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下: 1. 1 2. 11 3. 21 4. 1211 5. 111221 1 被读作  " ...

  8. 【springmvc+mybatis项目实战】杰信商贸-5.生产厂家DAO+SERVICE+CONTROLLER+JSP+配置文件

    上一篇我们创建了工程和一个Factory的po对象(javaBean),我们也写好了Mapper的映射文件,接下来我们来完成生产厂家的DAO与SERVICE,以及CONTROLLER,还有做显示的JS ...

  9. Apache——访问控制

    Order 指定执行允许访问规则和拒绝访问规则 Deny 定义拒绝访问列表 Allow 定义允许访问列表 Order allow,deny  先执行允许,再执行拒绝 Order deny,allow ...

  10. LeetCode 120——三角形最小路径和

    1. 题目 2. 解答 详细解答方案可参考北京大学 MOOC 程序设计与算法(二)算法基础之动态规划部分. 从三角形倒数第二行开始,某一位置只能从左下方或者右下方移动而来,因此,我们只需要求出这两者的 ...