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 ;
}

最新文章

  1. JDBC查询数据库中的数据
  2. C# UDP 连接通信 简单示例
  3. Mathematical operation
  4. gcc 创建库及使用
  5. 【转载】soapui基于持续集成工具自动化运行的调研姿势
  6. why app_start start
  7. 导航 - 利用系统自带的App导航
  8. 通过 INotifyPropertyChanged 实现观察者模式
  9. object- c 字符串操作
  10. Java中有关构造函数的一道笔试题解析
  11. BT是如何下载的
  12. implemented loader.php
  13. Angular - - $sce 和 $sceDelegate
  14. mysql生成百万级数量测试数据
  15. Stackoverflow 最受关注的 10 个 Java 问题
  16. 网络协议 21 - RPC 协议(中)- 基于 JSON 的 RESTful 接口协议
  17. 【mysql】mysql 调优之 ——执行计划 explain
  18. 【vue】饿了么项目的相关笔记链接
  19. codeforces347B
  20. 【Gym - 100796C 】Minimax Tree

热门文章

  1. jsp---》》》新闻发布系统的项目跟踪+++++++文件上传
  2. #include stdio.h(5)
  3. viewport信息设置
  4. AOSP 源码整编单编
  5. php的yii框架开发总结3
  6. 数据结构与算法分析java——线性表2(ArrarList )
  7. IOS 线程的总结(及cell的图片下载)
  8. 一款带有CSS的单选框以及选中事件
  9. framework7 可以拉动右侧工具栏和点击当前item就可以出发事件的HTML结构
  10. Android(java)学习笔记70:TabActivity使用