任意两点之间的最短路(floyed)

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)的更多相关文章
- Floyd_Warshall(任意两点之间的最短路)
/* O(V^3) 案例: 1 2 2 1 3 5 2 3 1 */ #include <cstdio>#include <iostream>using namespace s ...
- Geotools求shapefile路网中任意两点之间最短路径的距离
前言:之前在博问求助过这个问题.经过几天的思考,算是解决了(但仍有不足),另一方面对Geotools不是很熟,有些描述可能不正确,希望大家批评指正. 问题:作为一个新手,我并没有发现Geotools中 ...
- POJ 3660 Cow Contest 任意两点之间的关系 Floyd
题意:牛之间有绝对的强弱,给出一些胜负关系,问有多少头牛可以确定其绝对排名. #include <iostream> #include <cstdio> #include &l ...
- dfs+记忆化搜索,求任意两点之间的最长路径
C.Coolest Ski Route 题意:n个点,m条边组成的有向图,求任意两点之间的最长路径 dfs记忆化搜索 #include<iostream> #include<stri ...
- Floyd算法——计算图中任意两点之间的最短路径
百度百科定义:传送门 一.floyd算法 说实话这个算法是用来求多源最短路径的算法. 算法原理: 1,从任意一条单边路径开始.所有两点之间的距离是边的权,如果两点之间没有边相连,则权为无穷大. 2,对 ...
- AOJ -0189 Convenient Location && poj 2139 Six Degrees of Cowvin Bacon (floyed求任意两点间的最短路)
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=78207 看懂题就好. 求某一办公室到其他办公室的最短距离. 多组输入,n表示 ...
- 百度地图api文档实现任意两点之间的最短路线规划
两个点之间的路线是使用“Marker”点连接起来的,目前还没找到改变点颜色的方法,测试过使用setStyle没有效果. <html><head> <meta http-e ...
- poj - 3268 Silver Cow Party (求给定两点之间的最短路)
http://poj.org/problem?id=3268 每头牛都要去标号为X的农场参加一个party,农场总共有N个(标号为1-n),总共有M单向路联通,每头牛参加完party之后需要返回自己的 ...
- HDU2586(LCA应用:在带权树中求任意两点之间的距离)
How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
随机推荐
- python manage.py shell
启动python有两种方式:python manage.py shell和python. 这两个命令 都会启动交互解释器,但是manage.py shell命令有一个重要的不同: 在启动解释器之前,它 ...
- BeanUtils学习笔记
一. 简介 BeanUtils提供对Java反射和自省API的包装.其主要目的是利用反射机制对JavaBean的属性进行简化操作处理.一个JavaBean通常包含了大量的属性,很多情况下,对JavaB ...
- Django rest framework框架中有哪些组件
认证 权限(授权) 用户访问次数/频率限制 版本 解析器(parser) 序列化 分页 路由系统 视图 渲染器 认证 自定义认证的类 """ from rest_fram ...
- Mybatis中mapper.xml的使用
详解多对多,mybatis多对多查询(xml方式和注解方式) 链接:https://blog.csdn.net/qq_42524262/article/details/98383977 链接:http ...
- Nexus 3048的NX-OS升级方法
1.System Software和Kick Start 与普通IOS设备不同,NX OS升级时,共有2个文件需要升级.安装,如果只安装其中一个,可能会导致设备重启后无法进入系统.这2个文件包括: N ...
- 用js实现复制内容到操作系统粘贴板(兼容IE、谷歌、火狐等浏览器)
一.如果只考虑IE浏览器,可以直接用原声js实现 if(window.clipboardData){ //清空操作系统粘贴板 window.clipboardData.clearData(); //将 ...
- 《JavaScript高级程序设计》读书笔记(目录)
ajax创造性的技术让JavaScript变得更加强大起来.ECMAScript 和DOM标准JavaScript面向对象程序设计和继承----性能,优化 1.简介----JavaScript和ECM ...
- Python 之并发编程之线程上
一.线程概念 进程是资源分配的最小单位 线程是计算机中调度的最小单位 多线程(即多个控制线程)的概念是,在一个进程中存在多个控制线程,多个控制线程共享该进程的地址空间,相当于一个车间内有多条流水线,都 ...
- Python爬虫解析网页的4种方式 值得收藏
用Python写爬虫工具在现在是一种司空见惯的事情,每个人都希望能够写一段程序去互联网上扒一点资料下来,用于数据分析或者干点别的事情. 我们知道,爬虫的原理无非是把目标网址的内容下载下来存储到内存 ...
- MySQL - 设置UTF-8编码
1. 在Windows上,安装时请选择UTF-8编码,以便正确地处理中文. 在Mac或Linux上,需要编辑MySQL的配置文件,把数据库默认的编码全部改为UTF-8.MySQL的配置文件默认存放在/ ...