Description
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≤2×104) 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≤105
, 1≤di,j≤105 (i≠j), di,i=0 and di,j=dj,i. Besides, each query satisfies 1≤u,v≤n and 1≤w≤105
.
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.
Sample Input
Input
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
Output
Case #1: 0 1 3 0 1 2
题意:求x到y的最短路,要求最短路上经过的城市危险度不能大于他给出的k
思路:flody,可以这么看,每两个点先判断能不能与危险度最低的城市连,连完后,然后再判断能不能与第次不危险的城市连。。。依次类推,这就是最外层k循环的含义
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<set>
#include<map>
#include<string>
#include<string.h>
#include<math.h>
#define maxn 150000
#define ll long long
#include<vector>
#include<queue>
const int INF=0x3f3f3f3f;
using namespace std;
int d[maxn],id[maxn];
bool cmp(int x,int y)
{
return d[x]<d[y];
}
int dp[210][210][210];
int main()
{
int t,h=0;
scanf("%d",&t);
while(t--)
{
memset(dp,INF,sizeof(dp));
int n,q;
scanf("%d %d",&n,&q);
for(int i=1;i<=n;i++)
{
id[i]=i;
scanf("%d",&d[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++)
{
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][id[k]]+dp[k-1][id[k]][j]);
}
}
}
printf("Case #%d:\n",++h);
while(q--)
{
int x,y,w,k=0;
scanf("%d %d %d",&x,&y,&w);
for(k=1;k<=n;k++)
{
if(d[id[k]]>w) break;
}
k--;
//printf("k=%d\n",k);
printf("%d\n",dp[k][x][y]);
}
}
}