Moving On 【Gym - 102222A】

Moving On
题面:
Firdaws and Fatinah are living in a country with n cities, numbered from 1 to n. Each city has a risk of kidnapping or robbery.

Firdaws’s home locates in the city u, and Fatinah’s home locates in the city v. Now you are asked to find the shortest path from the city u to the city v that does not pass through any other city with the risk of kidnapping or robbery higher than w, a threshold given by Firdaws.
Input
The input contains several test cases, and the first line is a positive integer T indicating the number of test cases which is up to 50.

For each test case, the first line contains two integers n (1≤n≤200) which is the number of cities, and q (1≤q≤2e4) which is the number of queries that will be given. The second line contains n integers r1,r2,⋯,rn indicating the risk of kidnapping or robbery in the city 1 to n respectively. Each of the following n lines contains n integers, the j-th one in the i-th line of which, denoted by di,j, is the distance from the city i to the city j.

Each of the following q lines gives an independent query with three integers u,v and w, which are described as above.

We guarantee that 1≤ri≤1e5, 1≤di,j≤1e5 (i≠j), di,i=0 and di,j=dj,i. Besides, each query satisfies 1≤u,v≤n and 1≤w≤1e5.

Output
For each test case, output a line containing Case #x: at first, where x is the test case number starting from 1. Each of the following q lines contains an integer indicating the length of the shortest path of the corresponding query.
题意: 每个城市都一个风险值,现在q次询问u到v且风险值低于w的最短路径
思路: 将城市的风险值从小到大排序后,跑一遍floy,跑floy时需要注意从风险值小的逐一跑

#include<bits/stdc++.h>
using namespace std;
int dp[205][205][205];
int id[205],a[205];
bool cmp(int q,int p)
{
	return a[q]<a[p];
}
int main()
{
	int t;
	scanf("%d",&t);
	for(int cas=1;cas<=t;cas++)
	{
		memset(dp,0x3f,sizeof dp);
		int n,m;
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
			id[i]=i;
		}
		sort(id+1,id+1+n,cmp);
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=n;j++)
			{
				scanf("%d",&dp[0][i][j]);
			}
		}
		for(int k=1;k<=n;k++)
		{
			int tk=id[k];
			for(int i=1;i<=n;i++)
			{
				for(int j=1;j<=n;j++)
				{
					dp[k][i][j]=min(dp[k-1][i][j],dp[k-1][i][tk]+dp[k-1][tk][j]);
				}
			}
		}
		printf("Case #%d:\n",cas);
		for(int i=1;i<=m;i++)
		{
			int u,v,w;
			scanf("%d%d%d",&u,&v,&w);
			int tmp=0;

			for(int j=n;j>=0;j--)
			{
				if(a[id[j]]<=w)
				{
					tmp=j;
					break;
				}
			}
			printf("%d\n",dp[tmp][u][v]);
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值