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,vi,wi(1≤ui,vi≤n,ui≠vi,1≤wi≤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,ti,ki(1≤si,ti≤n,1≤ki≤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 ...
随机推荐
- aop的简单使用(代码和配置记录)
Spring aop 简单示例 简单的记录一下spring aop的一个示例 基于两种配置方式: 基于xml配置 基于注解配置 这个例子是模拟对数据库的更改操作添加事物 其实并没有添加,只是简单的输出 ...
- 多线程-等待(Wait)和通知(notify)
1.为了支撑多线程之间的协作,JDK提供了两个非常重要的线程接口:等待wait()方法和通知notify()方法. 这两个方法并不是在Thread类中的,而是输出在Object类.这意味着任何对象都可 ...
- Asciinema文章勘误及Web端使用介绍
欠下的债迟早是要还的,查文档,重验证,出结果,不误导 文章勘误 在上一篇文章Asciinema:你的所有操作都将被录制中有两个地方表述有错误或瑕疵,这里更正一下 第一个地方为录制时的参数--stdin ...
- poj 3974 Palindrome (manacher)
Palindrome Time Limit: 15000MS Memory Limit: 65536K Total Submissions: 12616 Accepted: 4769 Desc ...
- Mybatis动态SQL(where元素、set元素、if元素)
Mybatis动态SQL(where元素.set元素.if元素) - where 元素只会在至少有一个子元素的条件返回 SQL 子句的情况下才去插入“WHERE”子句.而且,若语句的开头为“AND”或 ...
- 新一代开源即时通讯应用源码定制 运营级IM聊天源码
公司介绍:我们是专业的IM服务提供商!哇呼Chat是一款包含android客户端/ios客户端/pc客户端/WEB客户端的即时通讯系统.本系统完全自主研发,服务器端源码直接部署在客户主机.非任何第三方 ...
- UML:类图关系总结
UML类图几种关系的总结,泛化 = 实现 > 组合 > 聚合 > 关联 > 依赖在UML类图中,常见的有以下几种关系: 泛化(Generalization), 实现(Reali ...
- 对js中局部变量、全局变量和闭包的理解
对js中局部变量.全局变量和闭包的理解 局部变量 对于局部变量,js给出的定义是这样的:在 JavaScript函数内部声明的变量(使用 var)是局部变量,所以只能在函数内部访问它.(该变量的作用域 ...
- python3 之 匿名函数
一.语法: lambda 参数:方法(或三元运算) #最多支持3元运算 二.实例1:基础 #函数1: a = lambda x:x*x print(a(2)) #函数2: def myfun(x): ...
- Deep attention tracking via Reciprocative Learning
文章:Deep attention tracking via Reciprocative Learning 出自NIPS2018 文章链接:https://arxiv.org/pdf/1810.038 ...