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

这个题,比较简单的最短路,将城市按照犯罪率从低到高排序,松弛操作从最低的开始松弛,在通过一纬保存犯罪率的下标,这样的话就是3纬Floyd,但是这个题,太变态了,卡cache,第三维访问速度慢,就是靠近数组首地址的位置访问速度快,因为第三维访问的最多,所以这个题不把第三维放到第一维,优化了极限的速是3001MS超了1毫秒,很难受,别的队伍告诉我们要换一下维数,一开始不知道为什么,问他们也不知道,知道查了题解才知道,这个叫cache,等着学操作系统在深入,现在先知道访问最多的放前面,而且这个时间优化有多恐怖,1280ms过了,就简单换了个位置,长见识了以后记住了。

#include<iostream>
#include<queue>
#include<algorithm>
#include<set>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<bitset>
#include<cstdio>
#include<cstring>
//---------------------------------Sexy operation--------------------------// #define cini(n) scanf("%d",&n)
#define cinl(n) scanf("%lld",&n)
#define cinc(n) scanf("%c",&n)
#define cins(s) scanf("%s",s)
#define coui(n) printf("%d",n)
#define couc(n) printf("%c",n)
#define coul(n) printf("%lld",n)
//___________________________Dividing Line__________________________________/
using namespace std;
int n, m, T, q,i,j,k,ans,u,v,w;
int ma[300][300][300];
int wx[300];
int sx[300];
inline bool cmp( int x,int y )
{
return wx[x]<wx[y];
}
int main()
{
int cnt = 0;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n,&q);
for(i = 1; i <= n; i++)
{
sx[i] = i;
scanf("%d",&wx[i]);
}
sort(sx+1, sx+1+n, cmp);
for( i = 1; i <= n; i++)
{
for(j = 1; j <= n; j++)
{
scanf("%d",&ma[0][i][j]);
}
}
for(k = 1; k <= n; k++)
{
for(i = 1; i <= n; i++)
{
for(j = 1; j <= n; j++)
{
ma[k][i][j] = min(ma[k-1][i][j], ma[k-1][i][sx[k]]+ma[k-1][sx[k]][j]);
}
}
}
printf("Case #%d:\n",++cnt);
while(q--)
{
scanf("%d%d%d",&u,&v,&w);
ans = 0;
for(i = n; i >= 1; i--)
{
if(wx[sx[i]]<=w)
{
ans = i;
break;
}
}
printf("%d\n",ma[ans][u][v]);
}
}
return 0;
}

2019 ICPC 银川网络赛 F-Moving On (卡Cache)的更多相关文章

  1. 2019 ICPC南京网络赛 F题 Greedy Sequence(贪心+递推)

    计蒜客题目链接:https://nanti.jisuanke.com/t/41303 题目:给你一个序列a,你可以从其中选取元素,构建n个串,每个串的长度为n,构造的si串要满足以下条件, 1. si ...

  2. 2019 ICPC 银川网络赛 D. Take Your Seat (疯子坐飞机问题)

    Duha decided to have a trip to Singapore by plane. The airplane had nn seats numbered from 11 to nn, ...

  3. 2019 ICPC 银川网络赛 H. Fight Against Monsters

    It is my great honour to introduce myself to you here. My name is Aloysius Benjy Cobweb Dartagnan Eg ...

  4. 2019 ICPC 南京网络赛 F Greedy Sequence

    You're given a permutation aa of length nn (1 \le n \le 10^51≤n≤105). For each i \in [1,n]i∈[1,n], c ...

  5. 2019 ICPC 南昌网络赛

    2019 ICPC 南昌网络赛 比赛时间:2019.9.8 比赛链接:The 2019 Asia Nanchang First Round Online Programming Contest 总结 ...

  6. 2019 ICPC上海网络赛 A 题 Lightning Routing I (动态维护树的直径)

    题目: 给定一棵树, 带边权. 现在有2种操作: 1.修改第i条边的权值. 2.询问u到其他一个任意点的最大距离是多少. 题解: 树的直径可以通过两次 dfs() 的方法求得.换句话说,到任意点最远的 ...

  7. 2013 ACM/ICPC 长春网络赛F题

    题意:两个人轮流说数字,第一个人可以说区间[1~k]中的一个,之后每次每人都可以说一个比前一个人所说数字大一点的数字,相邻两次数字只差在区间[1~k].谁先>=N,谁输.问最后是第一个人赢还是第 ...

  8. 2013 ACM/ICPC 南京网络赛F题

    题意:给出一个4×4的点阵,连接相邻点可以构成一个九宫格,每个小格边长为1.从没有边的点阵开始,两人轮流向点阵中加边,如果加入的边构成了新的边长为1的小正方形,则加边的人得分.构成几个得几分,最终完成 ...

  9. 2018 icpc 徐州网络赛 F Features Track

    这个题,我也没想过我这样直接就过了 #include<bits/stdc++.h> using namespace std; ; typedef pair<int,int> p ...

随机推荐

  1. "高亮显示"组件:<mark> —— 快应用组件库H-UI

     <import name="mark" src="../Common/ui/h-ui/text/c_tag_mark"></import& ...

  2. Solr复杂查询一:函数查询

    一.简介 Solr的函数可以动态计算每个文档的值,而不是返回在索引阶段对应字段的静态数值集.函数查询是一类特殊的查询,它可以像关键词一样添加到查询中,对所有文档进行匹配并返回它们的函数计算值作为文档得 ...

  3. Python入门学习指导(VS Code配置向)

    代码编辑器或IDE 推荐Vs Code,Atom和Sublime(本文以Vs Code为例,Sublime对中文支持不是很好,时常弄好了Sublime的乱码,却在复制到其他编辑器时出了问题) Vs C ...

  4. web.xml配置参数context-param和init-param的区别

    web.xml配置参数context-param和init-param的区别 (2009-04-13 10:29:01) 转载▼ 标签: 杂谈 分类: JavaEE web.xml里面可以定义两种参数 ...

  5. JS数据结构与算法 - 剑指offer二叉树算法题汇总

    ❗❗ 必看经验 在博主刷题期间,基本上是碰到一道二叉树就不会碰到一道就不会,有时候一个下午都在搞一道题,看别人解题思路就算能看懂,自己写就呵呵了.一气之下不刷了,改而先去把二叉树的基础算法给搞搞懂,然 ...

  6. Android 开发小零碎

    1.EditText默认就会自动获取焦点, 如何让EditText不自动获取焦点? 解决之道:在EditText的父级控件中找一个,设置成 android:focusable="true&q ...

  7. 如何改变Xcode字体大小?

    运行Xcode后依次点击左上角Xcode/Preferences/Fonts & Colors里就可以调整,在右边随便点中一个字体就可以调整这个字体的大小和颜色了,按command+a可以将所 ...

  8. [算法总结]康托展开Cantor Expansion

    目录 一.关于康托展开 1.什么是康托展开 2.康托展开实现原理 二.具体实施 1.模板 一.关于康托展开 1.什么是康托展开 求出给定一个由1n个整数组成的任意排列在1n的全排列中的位置. 解决这样 ...

  9. 这价格看得我偷偷摸了泪——用python爬取北京二手房数据

    如果想了解更多关于python的应用,可以私信我,或者加群,里面到资料都是免费的 http://t.cn/A6Zvjdun 近期,有个朋友联系我,想统计一下北京二手房的相关的数据,而自己用Excel统 ...

  10. . Number throry

    steve 学完了快速幂,现在会他快速的计算:(ij)%d , Alex 作为一个数学大师,给了 steve 一个问题:已知i∈[1,n],j∈[1,m] ,计算满足 (ij)%d=0 的 (i,j) ...