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 Format

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 nnlines 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 ii to 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^51≤ri​≤105, 1\le d_{i,j}\le 10^5~(i \neq j)1≤di,j​≤105 (i≠j), d_{i,i}=0di,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 Format

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

题目来源

The 2018 ACM-ICPC Chinese Collegiate Programming Contest

思路:动态规划f[i][j][k]记录经过前i个城市从j到k的最小风险,再将每个城市的风险进行排序,用于转移与计算。

代码:

 #include"bits/stdc++.h"
#define ci(x) scanf("%d",&x)
const int N = 1e6 + ;
using namespace std;
int t,n,q;
struct P
{
int id,r;
};
P a[N];
int f[][][];
bool cmp(P a,P b){
return a.r<b.r;
}
void cal(int m){
for(int i=;i<=m;i++){
int id=a[i].id;
for(int j=;j<=n;j++){
for(int k=;k<=n;k++){
f[i][j][k]=min(f[i-][j][k],f[i-][j][id]+f[i-][id][k]);
}
}
}
}
int main(){
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
scanf("%d",&t);
for(int ii=;ii<=t;ii++){
scanf("%d%d",&n,&q);
for(int i=;i<=n;i++) ci(a[i].r),a[i].id=i;
memset(f,inf,sizeof(f));
for(int i=;i<=n;i++)
for(int j=;j<=n;j++) ci(f[][i][j]);
sort(a+,a+n+,cmp);
cal(n);
printf("Case #%d:\n",ii);
int u,v,w;
for(int i=;i<q;i++){
ci(u),ci(v),ci(w);
int k;
for(k=;k<=n;k++) if(a[k].r>w) break;
printf("%d\n",f[k-][u][v]);
}
}
return ;
}

宁夏邀请赛F FLOYD的更多相关文章

  1. 2018宁夏邀请赛 L Continuous Intervals(单调栈+线段树)

    2018宁夏邀请赛 L Continuous Intervals(单调栈+线段树) 传送门:https://nanti.jisuanke.com/t/41296 题意: 给一个数列A 问在数列A中有多 ...

  2. 2018acm-icpc宁夏邀请赛后记

    由于要准备期末考试的缘故,时隔一个月之后我才想起来还有一场比赛没有总结. 大概是出去过的最远的地方了,宁夏理工学院离杭州有两千多千米. 不得不说感到了主办方满满的诚意,虽然是第一次办比赛,但是第一天组 ...

  3. “2018宁夏邀请赛 ” 兼 “The 2019 Asia Yinchuan First Round Online Programming”

    ------------7题弟弟,被各位半小时13题的大佬打惨了(滑稽)---------- 签到题就不写了. F :Moving On            (1247ms) 题意:给定大小为N的带 ...

  4. 2018宁夏邀请赛网赛 I. Reversion Count(java练习题)

    题目链接 :https://nanti.jisuanke.com/t/26217 Description: There is a positive integer X, X's reversion c ...

  5. 2018宁夏邀请赛G(DFS,动态规划【VECTOR<PAIR>】)

    //代码跑的很慢四秒会超时,结尾附两秒代码(标程) #include<bits/stdc++.h>using namespace std;typedef long long ll;cons ...

  6. 【郑轻邀请赛 F】 Tmk吃汤饭

    [题目链接]:https://acm.zzuli.edu.cn/zzuliacm/problem.php?id=2132 [题意] [题解] 很容易想到用队列来模拟; 这个队列维护的是正在煮的4个人煮 ...

  7. 2019icpc南昌邀请赛F(线段树)

    题目链接:https://nanti.jisuanke.com/t/40258 题意:给长为n的数组a,有m次操作,包括单点修改和查询F(l,r),其值为所有f(i,j)的异或和,l<=i< ...

  8. [ICPC 2018 宁夏邀请赛] A-Maximum Element In A Stack(思维)

    >传送门< 前言 辣鸡网络赛,虽然我是个菜鸡,然而好几个队伍十几分钟就AK???我心态那会彻底崩了,后来群里炸了,话题直接上知乎热搜,都是2018ICPC宁夏网络赛原题,这怎么玩,拼手速? ...

  9. hdu - 6281,2018CCPC湖南全国邀请赛F题,快排

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6281 题意: 根据已给出的式子,进行排序,然后输出排完序后原先的下表. 题解:用结构体保存,在用结构体 ...

随机推荐

  1. maven课程 项目管理利器-maven 3-4 eclipse安装maven插件和新建maven项目

    本节主要讲了两个主要内容, 1       eclipse安装maven插件 2 新建maven项目 3 本人实操 1       eclipse安装maven插件 eclipse4.0以上和myec ...

  2. 计算Sn

    求Sn=a+aa+aaa+…+aa…aaa(有n个a)之值,其中a是一个数字. 例如:2+22+222+2222+22222(n=5), 输入 输入两个数.第一个为a ,第二个为n(表示有多少个数相加 ...

  3. 4.JavaScript

    1.简介 JavaScript 是一种轻量级的编程语言,是一种动态类型.弱类型.基于原型的脚本语言. JavaScript,通常缩写为JS,是一种高级的,解释执行的编程语言.JavaScript是一门 ...

  4. webpack-dev-server.js 服务器配置说明

    connect-history-api-fallback 使用: var app = express() var histroy = require('connect-history-api-fall ...

  5. open ssh 常用的东西

    清除已经存在的但是不同设备的连接信息 ssh-keygen -f "/users/he/.ssh/known_hosts" -R 192.168.1.118 无密码登录openss ...

  6. 建立本地yum源

    使用环境 服务器处于内网,需要更新 网络资源紧张,节约带宽 建立yum目录 mkdir -p /opt/opmgmt/yum rsync服务器列表 centos mirrors epel mirror ...

  7. Python基础学习之语句和语法

    语句和语法 python语句中有一些基本规则和特殊字符: 井号键“#”表示之后的字符为python注释: 三引号(‘‘‘ ’’’)可以多行注释 换行“\n”是标准的行分隔符(通常一个语句一行): 反斜 ...

  8. Node.js-sublime text3 配置node.js(ERROR: The process "node.exe" not found.)

    默认已经安装好sublime.node和npm 1.sublime的node.js插件下载 由于在package control上经常下载失败,所以这里直接从GitHub上进行下载! GitHub下载 ...

  9. Linux终端(terminal)清屏命令

    windows CMD终端的清屏命令是cls Linux终端中的清屏命令有 1) clear 2) reset

  10. 关于硬盘分区使用exFat格式的优势及劣势(含摘抄)

    优势 可以设置最大32M的簇: 不记录日志. 劣势 无法使用windows的“文件共享”: 通过近期某个文件数量密级任务的测试发现,在大量文件的处理性能上,NTFS比exFAT文件系统的性能高出不少. ...