F、Moving On

Firdaws and Fatinah are living in a country with nn cities, numbered from 11 to nn. Each city has a risk of kidnapping or robbery.

Firdaws's home locates in the city uu, and Fatinah's home locates in the city vv. Now you are asked to find the shortest path from the city uu to the city vv that does not pass through any other city with the risk of kidnapping or robbery higher than ww, a threshold given by Firdaws.

Input

The input contains several test cases, and the first line is a positive integer TT indicating the number of test cases which is up to 5050.

For each test case, the first line contains two integers n (1 \le n \le 200)n(1≤n≤200) which is the number of cities, and q (1 \le q \le 2 \times 10^4)q(1≤q≤2×104) which is the number of queries that will be given. The second line contains nn integers r_1, r_2, \cdots , r_nr1​,r2​,⋯,rn​ indicating the risk of kidnapping or robbery in the city 11 to nn respectively. Each of the following nn lines contains nn integers, the jj-th one in the ii-th line of which, denoted by d_{i,j}di,j​, is the distance from the city iito the city jj.

Each of the following qq lines gives an independent query with three integers u,vu,v and ww, which are described as above.

We guarantee that 1 \le r_i \le 10^5, 1 \le d_{i,j} \le 10^5 (i \neq j), d_{i,i} = 01≤ri​≤105,1≤di,j​≤105(i​=j),di,i​=0 and d_{i,j} = d_{j,i}di,j​=dj,i​. Besides, each query satisfies 1 \le u,v \le n1≤u,v≤n and 1 \le w \le 10^51≤w≤105.

Output

For each test case, output a line containing Case #x: at first, where xx is the test case number starting from 11. Each of the following qq lines contains an integer indicating the length of the shortest path of the corresponding query.

输出时每行末尾的多余空格,不影响答案正确性

样例输入复制

1
3 6
1 2 3
0 1 3
1 0 1
3 1 0
1 1 1
1 2 1
1 3 1
1 1 2
1 2 2
1 3 2

样例输出复制

Case #1:
0
1
3
0
1
2 题意:输入t,表示t个测试样例
输入n,q,表示n个点,q次询问
下一行n个数表示[1,n]个点的危险值
接下来n行,每行3个数描述一条边,点x到y的距离为z;
最后q行,每行3个数,求从起点U到终点V,在每一个点危险值不超过W的前提下的最短距离
#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
#include<math.h>
#include<string>
#include<string.h>
#include<vector>
#include<utility>
#include<map>
#include<queue>
#include<set>
#define mx 0x3f3f3f3f
#define ll long long
#define MAXN 100
using namespace std;
int dp[][][];//dp[k][i][j]表示 添加(第1~k小的危险值的城市后)的 i->j 最短路
int vis[],rk[];
bool cmp(int i,int j)
{
return rk[i]<rk[j];
}
int main()
{
int t;
scanf("%d",&t);
for(int tt=;tt<=t;tt++)
{
memset(dp,mx,sizeof(dp));
int n,q;
scanf("%d%d",&n,&q);
for(int i=;i<=n;i++)
{
vis[i]=i;//初始化排名
scanf("%d",&rk[i]);
}
sort(vis+,vis+n+,cmp);//把城市的编号按风险等级从小到大排序,vis[排名]=编号
// for(int i=1;i<=n;i++)
// cout<<vis[i]<<' ';
// cout<<"<-----"<<endl;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
scanf("%d",&dp[][i][j]);//初始化每一条边的危险等级为0
for(int k=;k<=n;k++)
{
int now=vis[k];
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
dp[k][i][j]=min(dp[k-][i][j],dp[k-][i][now]+dp[k-][now][j]);
} }
printf("Case #%d:\n",tt);
for(int i=;i<=q;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
int k=;
for(int j=;j<=n;j++)
if(rk[vis[j]]<=w)//把危险值最接近w的边都加进去之后的最短路
k=j;
printf("%d\n",dp[k][u][v]);
}
}
return ;
}

任意两点之间的最短路(floyed)的更多相关文章

  1. Floyd_Warshall(任意两点之间的最短路)

    /* O(V^3) 案例: 1 2 2 1 3 5 2 3 1 */ #include <cstdio>#include <iostream>using namespace s ...

  2. Geotools求shapefile路网中任意两点之间最短路径的距离

    前言:之前在博问求助过这个问题.经过几天的思考,算是解决了(但仍有不足),另一方面对Geotools不是很熟,有些描述可能不正确,希望大家批评指正. 问题:作为一个新手,我并没有发现Geotools中 ...

  3. POJ 3660 Cow Contest 任意两点之间的关系 Floyd

    题意:牛之间有绝对的强弱,给出一些胜负关系,问有多少头牛可以确定其绝对排名. #include <iostream> #include <cstdio> #include &l ...

  4. dfs+记忆化搜索,求任意两点之间的最长路径

    C.Coolest Ski Route 题意:n个点,m条边组成的有向图,求任意两点之间的最长路径 dfs记忆化搜索 #include<iostream> #include<stri ...

  5. Floyd算法——计算图中任意两点之间的最短路径

    百度百科定义:传送门 一.floyd算法 说实话这个算法是用来求多源最短路径的算法. 算法原理: 1,从任意一条单边路径开始.所有两点之间的距离是边的权,如果两点之间没有边相连,则权为无穷大. 2,对 ...

  6. AOJ -0189 Convenient Location && poj 2139 Six Degrees of Cowvin Bacon (floyed求任意两点间的最短路)

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=78207 看懂题就好. 求某一办公室到其他办公室的最短距离. 多组输入,n表示 ...

  7. 百度地图api文档实现任意两点之间的最短路线规划

    两个点之间的路线是使用“Marker”点连接起来的,目前还没找到改变点颜色的方法,测试过使用setStyle没有效果. <html><head> <meta http-e ...

  8. poj - 3268 Silver Cow Party (求给定两点之间的最短路)

    http://poj.org/problem?id=3268 每头牛都要去标号为X的农场参加一个party,农场总共有N个(标号为1-n),总共有M单向路联通,每头牛参加完party之后需要返回自己的 ...

  9. HDU2586(LCA应用:在带权树中求任意两点之间的距离)

    How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

随机推荐

  1. nginx介绍以及nginx的反向代理

    什么是nginx? Nginx 是一个高性能的轻量级的HTTP和反向代理服务器,也是一个邮件服务器. 下载地址 本人使用的是Tengine,它是由淘宝网发起的Web服务器项目.它在Nginx的基础上, ...

  2. ELK日志分析系统部署

    ======================================================================================= 操作系统 IP地址 主机 ...

  3. centos7 安装pip2和pip3

    linux pip2 安装cd /usr/bin yum install -y epel-release yum install -y python-pip _____________________ ...

  4. 数据结构--Java语言描述

    本篇文章是为了记录自己在学习数据结构时的笔记,会对常见的数据结构做基本的介绍以及使用Java语言进行实现.包括 动态数组 栈 队列 链表 二分搜索树 优先队列和堆 线段树 Trie树 并查集 AVL树 ...

  5. HDU1285-确定比赛名次(拓扑+优先队列)

    对于拓扑排序,每次能入队的只有入度为0的点,所以用优先队列即可. 以及,第一组数据日常卡OJ,这组数据跳了一个点,我的程序这个版本也过不了(其实写了另一个版的),稍微改改更正确. #include & ...

  6. python 语法-参数注释

    python 语法-参数注释 最近碰到的这样的代码: def func(a:"shuoming") -> int: print("函数已运行.") fun ...

  7. java获取tomcat中的properties文件

    System.getProperty("catalina.home") 获取tomcat的绝对路径 获取文件的绝对路径 在windous中拼接路径是" \ " ...

  8. nodejs下载

    nodejs历史版本 查看npm镜像库 npm config get registry 更改npm镜像库 npm config set registry https://registry.npm.ta ...

  9. intellij idea设置(字体大小、背景)

    1. 配置信息说明 Intellij Idea: 2017.2.5 2.具体设置 <1> 设置主题背景.字体大小 File---->Settings----->Appearan ...

  10. Python学习第二十六课——PyMySql(python 链接数据库)

    Python 链接数据库: 需要先安装pymysql 包 可以设置中安装,也可以pip install pymysql 安装 加载驱动: import pymysql # 需要先安装pymysql 包 ...