2018HDU多校训练-3-Problem M. Walking Plan
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.
, 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 ui
,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 ui
to vi
, and the length of it is wi
.
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 si
,t
i
,k
i
(1≤s
i
,t
i
≤n,1≤k
i
≤10000)
, describing the walking plan.
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
1
11
-1
#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int N=,M=,maxn=;
int T,n,m,q,u,v,w,s,t,K;
int a[maxn][N][N],b[maxn][N][N],Map[N][N];
int flag[N][N],dis[N][N]; void pre_work(int x[N][N],int y[N][N],int z[N][N])
{
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
flag[i][j]=INF;
for(int k=;k<n;k++)
flag[i][j]=min(flag[i][j],x[i][k]+y[k][j]);
}
}
for(int i=;i<n;i++)
for(int j=;j<n;j++) z[i][j]=flag[i][j];
} int main()
{
ios::sync_with_stdio(false);
cin.tie();
cin>>T;
while(T--)
{
cin>>n>>m;
for(int i=;i<n;i++)
{
for(int j=;j<n;j++) Map[i][j]=INF;
}
while(m--)
{
cin>>u>>v>>w;
Map[u-][v-]=min(Map[u-][v-],w);
} for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
a[][i][j]=b[][i][j]= i==j? :INF;
}
for(int i=;i<M;i++) pre_work(a[i-],Map,a[i]);//处理出经过i步从 x->y 的最短路
for(int i=;i<M;i++) pre_work(b[i-],a[],b[i]);//处理出从 x->y 恰好走 100*i步 //Floyd
for(int i=;i<n;i++)
{
for(int j=;j<n;j++) dis[i][j]= i==j? :Map[i][j];
}
for(int k=;k<n;k++)
{
for(int i=;i<n;i++)
{
for(int j=;j<n;j++) dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
}
} for(int x=;x<M;x++)
{
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
flag[i][j]=INF;
for(int k=;k<n;k++) flag[i][j]=min(flag[i][j],b[x][i][k]+dis[k][j]);
}
}
for(int i=;i<n;i++) for(int j=;j<n;j++) b[x][i][j]=flag[i][j];
} cin>>q;
while(q--)
{
cin>>s>>t>>K; s--,t--;
int r=K/,l=K%,ans=INF;
for(int i=;i<n;i++) ans=min(ans,b[r][s][i]+a[l][i][t]);
if(ans>=INF) cout<<-<<endl;
else cout<<ans<<endl;
}
} return ;
}
2018HDU多校训练-3-Problem M. Walking Plan的更多相关文章
- HDU6331 Problem M. Walking Plan【Floyd + 矩阵 + 分块】
HDU6331 Problem M. Walking Plan 题意: 给出一张有\(N\)个点的有向图,有\(q\)次询问,每次询问从\(s\)到\(t\)且最少走\(k\)条边的最短路径是多少 \ ...
- 2018HDU多校训练-3-Problem G. Interstellar Travel
链接:http://acm.hdu.edu.cn/showproblem.php?pid=6325 Interstellar Tra ...
- 2018HDU多校训练-3-Problem D. Euler Function
链接:http://acm.hdu.edu.cn/showproblem.php?pid=6322 Problem Description In number theory, Euler's toti ...
- 2018HDU多校训练一 K - Time Zone
Chiaki often participates in international competitive programming contests. The time zone becomes a ...
- 2018HDU多校训练-3-Problem F. Grab The Tree
Little Q and Little T are playing a game on a tree. There are n vertices on the tree, labeled by 1,2 ...
- 2018HDU多校训练一 D Distinct Values
hiaki has an array of nn positive integers. You are told some facts about the array: for every two e ...
- 2018HDU多校训练一 C -Triangle Partition
Chiaki has 3n3n points p1,p2,-,p3np1,p2,-,p3n. It is guaranteed that no three points are collinear. ...
- 2018HDU多校训练一 A - Maximum Multiple
Given an integer nn, Chiaki would like to find three positive integers xx, yy and zzsuch that: n=x+y ...
- hdu6331 Problem M. Walking Plan
传送门 题目大意 给你一个n点m条边的有向图,q次询问,给定s,t,k,求由s到t至少经过k条边的最短路. 分析 我们设dp[i][j][k]为从i到j至少经过k条边的最短路,sp[i][j]意为从i ...
随机推荐
- jsoup爬虫实战心得
1.heder很重要,一切尽在header中.尤其cookie,useragent. 2.对于加密的连接,查看js加密过程并试着通过java或你正在使用的语言去实现 3.查看在跳转之前前端发起的关键请 ...
- java线程池的介绍与使用(Executor框架)
1. 先来看一下类构成 public interface Executor { //顶级接口Executor,定义了线程执行的方法 void execute(Runnable command); } ...
- 设置Ubuntu下vim缩进为4个空格
输入命令编辑vim配置文件: sudo vim /etc/vim/vimrc 文件中添加如下: 保存退出即可
- 五分钟学会HTML5的WebSocket协议
1.背景 很多网站为了实现推送技术,所用的技术都是Ajax轮询.轮询是在特定的的时间间隔由浏览器对服务器发出HTTP请求,然后由服务器返回最新的数据给客户端的浏览器.这种传统的模式带来很明显的缺点 ...
- 正则表达式 第五篇:C# 正则元字符
本文整理C#正则表达式的元字符,正则表达式是由字符构成的表达式,每个字符代表一个规则,表达式中的字符分为两种类型:普通字符和元字符.普通字符是指字面含义不变的字符,按照完全匹配的方式匹配文本,而元字符 ...
- 力扣(LeetCode)位1的个数 个人题解
编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 ‘1’ 的个数(也被称为汉明重量). 示例 1: 输入:00000000000000000000000000001011 输出:3 ...
- keypress 和 blur 事件冲突的问题
需求:点击需求:点击添加标签,出来input框,内容输入完成后点击enter键和blur时都可以执行提交标签的效果,提交时对内容进行判断,执行完成后清除input内的内容.如下图 问题:内容输入完成后 ...
- Hash Map 在java中的解释及示例
目录 HashMap在java中的应用及示例 HashMap的内部结构 HashMap的性能 同步HashMap HashMap的构造函数 HashMap的时间复杂度 HashMap的方法 1. vo ...
- java学生管理系统
student类 package cn.itheima.Manag; /** * *标准类 * **/public class Student { //学号 private String id; // ...
- Stream系列(七)distinct方法使用
EmployeeTestCase.java package com.example.demo; import lombok.Data; import lombok.ToString; import l ...