链接:http://acm.hdu.edu.cn/showproblem.php?pid=6331
                                Walking Plan 
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 si -th intersection, walk through at least ki streets and finally return to the ti -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 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.

 
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
 
Source
 
Recommend
chendu
 
这题时间复杂度卡的。。。。
题解:这题主要用来分块+DP+Folyd.对于数据范围,我们分100位每一块(一般大一点,我取110  Orz).我们可以先预处理出任意两点间走从0~110步的最短路,然后利用走100为一个单位步,
去更新1*100,2*100,....100*100步的最短路,
由于是至少为K条路的最短路,因此>=k.   我们可以可以再预处理更新一遍恰好走x*100步的情况,查找还有没有于x*100的情况使得i->j的距离变小(因为最多50个点,所以不会超过100)   我们把K 分为K/100,,和K%100,分别求;
参考代码为:

 #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的更多相关文章

  1. HDU6331 Problem M. Walking Plan【Floyd + 矩阵 + 分块】

    HDU6331 Problem M. Walking Plan 题意: 给出一张有\(N\)个点的有向图,有\(q\)次询问,每次询问从\(s\)到\(t\)且最少走\(k\)条边的最短路径是多少 \ ...

  2. 2018HDU多校训练-3-Problem G. Interstellar Travel

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=6325                                   Interstellar Tra ...

  3. 2018HDU多校训练-3-Problem D. Euler Function

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=6322 Problem Description In number theory, Euler's toti ...

  4. 2018HDU多校训练一 K - Time Zone

    Chiaki often participates in international competitive programming contests. The time zone becomes a ...

  5. 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 ...

  6. 2018HDU多校训练一 D Distinct Values

    hiaki has an array of nn positive integers. You are told some facts about the array: for every two e ...

  7. 2018HDU多校训练一 C -Triangle Partition

    Chiaki has 3n3n points p1,p2,-,p3np1,p2,-,p3n. It is guaranteed that no three points are collinear.  ...

  8. 2018HDU多校训练一 A - Maximum Multiple

    Given an integer nn, Chiaki would like to find three positive integers xx, yy and zzsuch that: n=x+y ...

  9. 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 ...

随机推荐

  1. 小白历险记:spingboot之helloworld

    还记得入职第一天的时候,先安装了相关的软件,配置了环境.boss叫我写的第一个程序:搭建一个springboot工程,输出helloworld. 哈哈话不多说,回忆一下. 1.打开IDEA,点击Cre ...

  2. 忘记Linux登录密码的破解方法

    注意:1.破解方式只限于7.0以后的Linux系统. 2.要注意自己linux系统中有没有开启selinux,如果开启则在后面要建一个名为:autorelabel的隐藏文件.     1.启动Linu ...

  3. Salesforce学习之路(十二)Aura组件表达式

    1. 表达式语法 在上篇文章组件属性示例中,新建了一个属性whom, 引用该属性时使用了表达式:{!v.whom},负责该属性的动态输出. 语法:{!expression} 上述示例中,我们的属性名称 ...

  4. 使用C#+FFmpeg+DirectX+dxva2硬件解码播放h264流

    本文门槛较高,因此行文看起来会乱一些,如果你看到某处能会心一笑请马上联系我开始摆龙门阵 如果你跟随这篇文章实现了播放器,那你会得到一个高效率,低cpu占用(单路720p视频解码播放占用1%左右cpu) ...

  5. python:Asyncio模块处理“事件循环”中的异步进程和并发执行任务

    python模块Asynico提供了管理事件.携程.任务和线程的功能已经编写并发代码的同步原语. 组成模块: 事件循,Asyncio 每个进程都有一个事件循环. 协程,子例程概念的泛化,可以暂停任务, ...

  6. J.U.C剖析与解读1(Lock的实现)

    J.U.C剖析与解读1(Lock的实现) 前言 为了节省各位的时间,我简单介绍一下这篇文章.这篇文章主要分为三块:Lock的实现,AQS的由来(通过演变的方式),JUC三大工具类的使用与原理剖析. L ...

  7. 性能测试——记weblogic 连接池满无法链接故障诊断过程

    记weblogic 连接池满无法链接故障诊断过程 前段时间公司负责建行的一个票据系统在,上线前几个分行试运行环境下,每天后台日志都会报oracle.jdbc.xa.OracleXAException, ...

  8. 阿里云:uwsgi--配置出错 bind(): Address already in use [core/socket.c line 769]

    按照网上配置nginx+uwsgi+django的文章,nginx启动成功,django启动也成功,单独用uwsgi --http :8000 命令启动uwsgi也成功.使用uwsgi  --sock ...

  9. Mysql数据库调优和性能优化的21条最佳实践

    Mysql数据库调优和性能优化的21条最佳实践 1. 简介 在Web应用程序体系架构中,数据持久层(通常是一个关系数据库)是关键的核心部分,它对系统的性能有非常重要的影响.MySQL是目前使用最多的开 ...

  10. 从BWM生产学习工厂模式

    工厂模式应用非常之广,在JDK底层源码以及各大主流框架中随处可见,一般以Factory结尾命名的类,比如Mybatis中的SqlSessionFactory,Spring中的BeanFactory等, ...