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. 分布式系统(二) --SOA 以及一些网络通信协议TCP/UDP SYN攻击

    SOA(面向服务的架构):Service Oriented Architecture面向服务的架构.也就是把工程拆分成服务层.表现层两个工程.服务层中包含业务逻辑,只需要对外提供服务即可.表现层只需要 ...

  2. Json日期格式化,出去返回的T

    第一种办法:前端JS转换: //格式化显示json日期格式 function showDate(jsonDate) { var date = new Date(jsonDate); var forma ...

  3. [题解] 2019牛客暑期多校第三场H题 Magic Line

    题目链接:https://ac.nowcoder.com/acm/contest/883/H 题意:二维平面上有n个不同的点,构造一条直线把平面分成两个点数相同的部分. 题解:对这n个点以x为第一关键 ...

  4. 1-1_微信小程序Buddy群记账背景

    1-1_微信小程序需求背景及评审 背景: 我是一个做了2年的Java后台开发 ,最近换了份工作 改做全栈了,需要对各方面的知识都有一定程度的认识及掌握, 虽然现如今还未要求开发小程序,但是已经有趋势了 ...

  5. 初学做uniapp项目过程梳理的一些记录

    1.uniapp不显示h5头部 第一种写法: { "path" : "pages/yunshi/yunshi", "style" : { & ...

  6. Java中引用类型、对象的创建与销毁

    引用类型 在java中,除了基本数据类型之外的,就是引用数据类型了,引用指的是对象的一个引用,通过引用可以操作对象,控制对象,向对象发送消息. 简单来说,引用可以访问对象的属性,并调用对象的方法 创建 ...

  7. 【模板】凸包向内推进求不严格的半平面交——poj3384

    想不明白这题写严格的半平面交为什么会错 /* 凸包所有边向内推进r */ #include<iostream> #include<cstring> #include<cs ...

  8. 连接数据库的url

    mysql: jdbc:mysql://localhost:3306:test这句里面分如下解析:jdbc:mysql:// 是指JDBC连接方式:localhost: 是指你的本机地址:3306 S ...

  9. ANSYS 瞬态热分析--样件加热

    目录 1. 要求 2. ANSYS有限元分析 2.1 APDL建模 1. 要求 一块0.8m*0.4m*0.2m厚的钢板,在300℃的炉子中进行加热,其材料参数如下表所示: 材料参数 序号 名称 参数 ...

  10. leetcode网解题心得——61. 旋转链表

    目录 leetcode网解题心得--61. 旋转链表 1.题目描述 2.算法分析: 3.用自然语言描述该算法 4.java语言实现 5.C语言实现 leetcode网解题心得--61. 旋转链表 1. ...