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. IsBackground对线程的重要作用

    要点: 1.当在主线程中创建了一个线程,那么该线程的IsBackground默认是设置为FALSE的. 2.当主线程退出的时候,IsBackground=FALSE的线程还会继续执行下去,直到线程执行 ...

  2. python面试题——数据库和缓存(46题)

    1.列举常见的关系型数据库和非关系型都有那些? 2.MySQL常见数据库引擎及比较? 3.简述数据三大范式? 4.什么是事务?MySQL如何支持事务? 5.简述数据库设计中一对多和多对多的应用场景? ...

  3. MyBatis之会话Session原理

    MyBatis 之会话 Session 执行逻辑 1.SQL 会话工厂构建器类 SqlSessionFactoryBuilder 的 build 方法用于构建 SqlSessionFactory 类的 ...

  4. Mysql慢查询 [第二篇]

    一.简介 pt-query-digest是用于分析mysql慢查询的一个工具,它可以分析binlog.General log.slowlog,也可以通过SHOWPROCESSLIST或者通过tcpdu ...

  5. Python人工智能之初识接口

    本节需要的两个工具: 1.FFmpeg: 链接:https://pan.baidu.com/s/1jonSAa_TG2XuaJEy3iTmHg 密码:w6hk 2.baidu-aip: pip ins ...

  6. jQueryMobile(一)

    一].jQuery Mobile 页面 <!DOCTYPE html> <html lang="zh-cn"> <head> <meta ...

  7. 【起航计划 014】2015 起航计划 Android APIDemo的魔鬼步伐 13 App->Activity->Translucent 半透明Activity Theme.Translucent

    Activity分类示例的最后几个例子是来显示半透明Activity.例子大同小异.实现Activity的半透明效果主要是通过Style和Theme来实现的. 看看TranslucentActivit ...

  8. python数据类型和数据运算

    数字 整型 包括正整数和负整数,和数学的表示方法一样.如:1.100.8008.-12等. 浮点型 浮点数字也称为小数,如果按照科学计数法表示时,小数点的位置是可变的.如:1.23x109==12.3 ...

  9. Coppermine-1.5.46 (Ubuntu 16.04.1)

      平台: Ubuntu 类型: 虚拟机镜像 软件包: coppermine-1.5.46 commercial content management coppermine media sharing ...

  10. 解决频繁自动弹出“QQ拼音升级程序”,可使用旧版QQ输入法

    QQ输入法(2017年9月6日版本)下载地址: http://dlc2.pconline.com.cn/filedown_90891_8506339/BZXMP3fp/QQPinyin_Setup_5 ...