任意两点之间的最短路(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 ...
随机推荐
- Uva 11300 Spreading the Wealth(贪心)
题目链接:https://vjudge.net/problem/UVA-11300 这道题的思路太神了,但很难想到是贪心. 用M表示每个人最终拥有的金币数. 首先假设有四个人.假设1号给2号3枚,2号 ...
- CSS3实现魔方动画
本文将借助css3实现魔方动画效果,设计思路如下: HTML方面采用六个div容器形成六个立方面: CSS方面采用transform-style: preserve-3d;形成三维场景:transfo ...
- ABC156 F - Modularness
题目链接 题意还是比较清楚的,给你q个询问,对每组询问的模数和初始值不同,求满足条件\(a_j~\textrm{mod}~m_i < a_{j + 1}~\textrm{mod}~m_i,(0 ...
- 用myeclipse快速搭建hibernate实现数据库访问
前言 hibernate使用的大致过程为引入jar包.配置主配置文件.配置映射文件.编写实体类.编写dao.但是每一步都需要知道的内容都相对不少,造成困难.如果使用myeclipse提供的支持将非常容 ...
- Euler Sums系列(四)
\[\Large\displaystyle \sum_{n=1}^\infty (-1)^n \frac{H_n}{2n+1}=\mathbf{G}-\frac{\pi}{2}\ln(2)\] \(\ ...
- java中关于类和对象的一些思考
就这个问题而言 第一种和第二种定义的变量并不是一种形式 前者我们称为原始数据变量 后者我们称为对象变量 这两种变量的创建方式,定义方式,使用方式都有着很多不同 需要引起注意. 在java中,有着基本的 ...
- python爬虫(八) requests库之 get请求
requests库比urllib库更加方便,包含了很多功能. 1.在使用之前需要先安装pip,在pycharm中打开: 写入pip install requests命令,即可下载 在github中有关 ...
- sparksql报错
执行时报错: org.apache.spark.sql.AnalysisException: Unable to generate an encoder for inner class `cn.itc ...
- javascript 变量、常量 、 函数 声明
声明变量: 方式一: 使用 var 定义变量,可在定义的同时赋值 或 不赋值 . 方式二: 直接使用[变量名 = 值]的形式,这会定义一个全局变量,但在严格模式下会出现引用错误.[不建议使用] 方式三 ...
- AbstractQueuedSynchronizer AQS源码分析
申明:jdk版本为1.8 AbstractQueuedSynchronizer是jdk中实现锁的一个抽象类,有排他和共享两种模式. 我们这里先看排他模式,共享模式后面结合java.util.concu ...