The shortest path between any two points (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(1n200) which is the number of cities, and q (1 \le q \le 2 \times 10^4)q(1q2×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} = 01ri105,1di,j105(i=j),di,i=0 and d_{i,j} = d_{j,i}di,j=dj,i. Besides, each query satisfies 1 \ and u, v \ n 1 you , v n and  1 \ and w \ 10 ^ 5 1 w 1 0 5.

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.

Extra spaces at the end of each row, the answer does not affect the validity of the output

Sample 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

Sample Output

#. 1 Case: 
0 
. 1 
. 3 
0 
. 1 
2 

that Italy: an input t, t represents a test sample
input n, q, n represents points, q interrogation times
the number of the next row and n represents a dangerous [1, n] point value
next n lines, each line number 3 is described an edge, from the point x to y to z;
last q rows of three numbers, starting from the seek to the end U V, does not exceed the value at each point dangerous the shortest distance under the premise of 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;
intDP [ 250 ] [ 250 ] [ 250 ]; // DP [K] [I] [J] represents added (after 1 ~ k Urban small risk value) is i-> j shortest 
int VIS [ 250 ] , RK [ 250 ];
 BOOL CMP ( int I, int J) 
{ 
    return RK [I] < RK [J]; 
} 
int main () 
{ 
    int T; 
    Scanf ( " % D " , & T);
     for ( int = TT . 1 ; TT <= T; TT ++ ) 
    { 
        Memset (DP, MX, the sizeof(DP));
         int n-, Q; 
        Scanf ( " % D% D " , & n-, & Q);
         for ( int I = . 1 ; I <= n-; I ++ ) 
        { 
            VIS [I] = I; // initialize rank 
            Scanf ( " % D " , & RK [I]); 
        } 
        Sort (VIS + . 1 , VIS + n-+ . 1 , CMP); // the city numbered level of risk from small to large, vis [ranking] = number
         // for (int I =. 1; I <= n-; I ++)
         //      COUT << VIS [I] << '';
         //<< COUT "<-----" << endl; 
        for ( int I = . 1 ; I <= n-; I ++ )
             for ( int J = . 1 ; J <= n-; J ++ ) 
                Scanf ( " % D " , & DP [ 0 ] [I] [J]); // hazard level initialize each edge is 0 
        for ( int K = . 1 ; K <= n-; K ++ ) 
        { 
            int now = VIS [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][now]+dp[k-1][now][j]); 
            }

        }
        printf("Case #%d:\n",tt);
        for(int i=1;i<=q;i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            int k=0;
            for(int j=1; J <= n-; J ++ )
                 IF (RK [VIS [J]] <= w) // shortest side after the risk value closest to w are added to the list 
                    K = J; 
            the printf ( " % D \ n- " , DP [K] [U] [V]); 
        } 
    } 
    return  0 ; 
}

 

Guess you like

Origin www.cnblogs.com/-citywall123/p/11528296.html