HDU - 2290 Find the Path(最短路)
HDU - 2290
Description Scofield is a hero in American show "Prison Break". He had broken the prison and started a big runaway.
Scofield has a map of US with cities and bidirectional roads between them. The lengths of roads are known. Some cities get a lot of cops who are very troublesome. Now Scofield needs your help to arrange his runaway route. He needs a shortest path between two cities, while the quantity of the police in any city, except the start city and end city, on the route is no more than k. You should know that it is very hard to escape. Scofield is very smart but not good at computer. Now Scofield is in trouble, can you help him with your computer? Input The input consists of several test cases. There is an integer T on the first line indicating the number of test cases.
For each case, the first line consists of two integers N and M. N is the number of cities; M is the number of roads. The next line contains N integers C1, C2... CN, where Ci is the number of cops in city i. Then followed M lines, each line consists of three integer, u, v, w, indicating there is a road with length w between city u and city v. The following line consists of an integer Q, indicating the number of queries. Each of the following Q lines consists of three integers, u, v, k, indicating the query for the shortest path between city u and city v with limitation of k cops. Technical Specification 1. T ≤ 20 Output For each query, output a single line contains the length of the shortest path. Output "-1" if you can't find the path. Please output an empty line after each test case.
Sample Input 1 Sample Output 3 Source |
题意是,有n个城市,m条无向路径,每个城市有ci个警察,给出q个询问,求从城市a到城市b,中途城市(不包括起点终点)警察数不超过k的权值和最小的路径
解法:由于起点和终点都不是固定的,所以要求多源最短路,很容易想到要用floyd。但是又有问题,每次询问都有k个警察的限制,怎么办呢。我们考虑floyd的原理,每次找到一个点,对经过那个点的边进行松弛,那么我们可以记录下每次松弛的结果。可以开个三维数组M[i][j][k],k表示松弛第k个点时的状态。然后进行floyd。问题在于,我怎么可以根据k来选择对应状态。floyd是从第一个点松弛到最后一个点,那我们只要根据ci的大小,对点进行排序,然后再floyd,就可以得到根据ci的大小floyd出来的状态。所以我们只要找到一个最大的ci,又不超过k的时候的状态,就找到结果了。
注意的地方:
1.由于c比较大,需要离散化。
2.每个case后都要有一个空行。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#define X first
#define Y second
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
const int maxn=;
const int INF=0x3f3f3f3f;
pii C[maxn];
int n,m,M[maxn][maxn][maxn],N[maxn];
void init()
{
for (int i=; i<n; i++)
for (int j=; j<n; j++)
M[i][j][]=INF;
}
void floyd()
{
for (int k=; k<=n; k++)
{
for (int l=; l<n; l++)
for (int r=; r<n; r++)
M[l][r][k]=M[l][r][k-];
for (int l=; l<n; l++)if (M[l][k-][k-]!=INF)
for (int r=; r<n; r++)if (M[k-][r][k-]!=INF)
if (M[l][r][k]>M[l][k-][k]+M[k-][r][k])//由于k从1开始,而下标从0开始,所以k-1
M[l][r][k]=M[l][k-][k]+M[k-][r][k];
}
}
int main()
{
int T,a,b,c,q;
scanf("%d",&T);
while (T--)
{
scanf("%d%d",&n,&m);
for (int i=; i<n; i++)
scanf("%d",&C[i].X),C[i].Y=i;
sort(C,C+n);
for (int i=; i<n; i++)//进行离散
N[C[i].Y]=i;
init();
for (int i=; i<m; i++)
{
scanf("%d%d%d",&a,&b,&c);
M[N[a]][N[b]][]=M[N[b]][N[a]][]=c;//这里用N[a]而不是a,因为N[a]存的才是排过序的
}
floyd();
scanf("%d",&q);
while (q--)
{
scanf("%d%d%d",&a,&b,&c);
int ans=M[N[a]][N[b]][n];
for (int i=; i<n; i++)//n比较小,可以枚举,也可以二分
if (C[i].X>c)
{
ans=M[N[a]][N[b]][i];//这里的i其实是i-1+1,因为状态从1开始,要+1
break;
}
printf("%d\n",ans==INF?-:ans);
}
puts("");
}
return ;
}
HDU - 2290 Find the Path(最短路)的更多相关文章
- HDU 4725 The Shortest Path in Nya Graph [构造 + 最短路]
HDU - 4725 The Shortest Path in Nya Graph http://acm.hdu.edu.cn/showproblem.php?pid=4725 This is a v ...
- Hdu 4725 The Shortest Path in Nya Graph (spfa)
题目链接: Hdu 4725 The Shortest Path in Nya Graph 题目描述: 有n个点,m条边,每经过路i需要wi元.并且每一个点都有自己所在的层.一个点都乡里的层需要花费c ...
- 2015合肥网络赛 HDU 5492 Find a path 动归
HDU 5492 Find a path 题意:给你一个矩阵求一个路径使得 最小. 思路: 方法一:数据特别小,直接枚举权值和(n + m - 1) * aver,更新答案. 方法二:用f[i][j] ...
- HDU 4725 The Shortest Path in Nya Graph(最短路拆点)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725 题意:n个点,某个点属于某一层.共有n层.第i层的点到第i+1层的点和到第i-1层的点的代价均是 ...
- HDU 4725 The Shortest Path in Nya Graph-【SPFA最短路】
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4725 题意:有N个点和N层..一层有X个点(0<=X<=N).两邻两层间有一条路花费C.还有M ...
- HDU 4725 The Shortest Path in Nya Graph (最短路)
The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- hdu 4725 The Shortest Path in Nya Graph (最短路+建图)
The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- HDU - 6582 Path (最短路+最小割)
题意:给定一个n个点m条边的有向图,每条边有个长度,可以花费等同于其长度的代价将其破坏掉,求最小的花费使得从1到n的最短路变长. 解法:先用dijkstra求出以1为源点的最短路,并建立最短路图(只保 ...
- HDU 4725 The Shortest Path in Nya Graph (最短路 )
This is a very easy problem, your task is just calculate el camino mas corto en un grafico, and just ...
随机推荐
- 2016年美国数学建模比赛(MCM/ICM) E题环境科学 Are we heading towards a thirsty planet? 人工精准翻译。
第二次参加建模,觉得建模太有趣了,用几天的时间,迅速学习新知识,并解决实际问题. ——————————————————————————————————————————————————————————— ...
- JS高程5.引用类型(5)Array类型的操作方法
一.操作方法 1.concat()方法 基于当前数组中的所有项创建一个新数组.具体说,是先创建当前数组的一个副本,然后将接收到的参数添加到这个副本的末尾,最后返回新构建的数组.在没有给concat() ...
- android 轮播图
轮播图是很常用的一个效果 核心功能已经实现 没有什么特殊需求 自己没事研究的 所以封装的不太好 一些地方还比较糙 为想要研究轮播图的同学提供个参考 目前测试图片为mipmap中的图片 没有写从网络加载 ...
- 二、spark入门之spark shell:文本中发现5个最常用的word
scala> val textFile = sc.textFile("/Users/admin/spark-1.5.1-bin-hadoop2.4/README.md") s ...
- hdu 2824 The Euler function(欧拉函数)
题目链接:hdu 2824 The Euler function 题意: 让你求一段区间的欧拉函数值. 题解: 直接上板子. 推导过程: 定义:对于正整数n,φ(n)是小于或等于n的正整数中,与n互质 ...
- js中substr,substring,indexOf,lastIndexOf等的用法
1.substrsubstr(start,length)表示从start位置开始,截取length长度的字符串. var src="images/off_1.png";alert( ...
- 东秦C#课设002-简单的文本编辑器
//加入的拖拽属性失败,dropenter声明方法待查. using System; using System.Collections.Generic; using System.ComponentM ...
- Python连接msyql、redis学习_Day12
一.数据库 关系型数据库(RDBMS): 1.数据以表格的形式出现2.每行为各种记录名称3.每列为记录名称所对应的数据域4.许多的行和列组成一张表单5.若干的表单组成database 1.MySQL基 ...
- [其他]Android SDK离线文件路径以及安装更新方法
一.离线安装Android SDK文件路径 转载自:http://www.oschina.net/code/snippet_1539302_45940 Google TV Addon, Android ...
- ntopng源码分析
参数初始化以及ntop主流程启动 #ifndef WIN32 ) && (argv[][] != '-')) rc = prefs->loadFromFile(argv[]); ...