题目链接

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. struts2学习笔记三

    一.国际化概念(了解) 1.什么是国际化 软件的国际化:软件开发时,要使它能同时应对世界不同地区和国家的访问,并针对不同地区和国家的访问,提供相应的.符合来访者阅读习惯的页面或数据. 2.什么需要国际 ...

  2. vue核心概念

    # 1. vuex是什么 github站点: https://github.com/vuejs/vuex 在线文档: https://vuex.vuejs.org/zh-cn/ 简单来说: 对应用中组 ...

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

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

  4. 北京Uber优步司机奖励政策(3月12日~3月13日)

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

  5. OSG-简单模型控制

    本文转至http://www.cnblogs.com/shapherd/archive/2010/08/10/osg.html 作者写的比较好,再次收藏,希望更多的人可以看到这个文章 互联网是是一个相 ...

  6. web自动化原理揭秘

    做过两年自动化测试的小伙伴说web自动化测试真的不难,无非就是一些浏览器操作,页面元素操作,常规的情况很容易处理,再学一学特殊元素的处理,基本就能应付项目的测试了. 这个话倒没错,但是真正要学好自动化 ...

  7. CentOS 7.2使用tomcat部署jenkins2.130

    一.jenkins介绍 Jenkins是一个功能强大的应用程序,允许持续集成和持续交付项目,无论用的是什么平台.这是一个免费的源代码,可以处理任何类型的构建或持续集成.集成Jenkins可以用于一些测 ...

  8. NGUI组件整理总结

    一图流: 注意: private void RClickUI(Vector3 newPos) { this.gameObject.SetActive(true); this.transform.loc ...

  9. Siki_Unity_0_Unity A计划直播视频

    Unity A计划直播视频 2017-07-04直播 任务1:如何识别以招聘来招培训生的公司: 打着招聘的旗号帮培训机构找培训生 关键词:实训生 任务2:如何识别一个公司的好坏和规模大小: 猎聘(中高 ...

  10. 【WXS】简要介绍说明

    WXS(WeiXin Script)是小程序的一套脚本语言. WXS有二种写法: 1) 以<wxs>标签书写脚本: 语法: <wxs module="[String]&qu ...