tju 3214 Find the Path(floyd)
http://acm.tju.edu.cn/toj/showp3214.html
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define MAXN 205
#define INF 999999
struct CNode
{
int k,idx;
};
CNode cops[MAXN];
bool mycmp(const CNode &c1,const CNode &c2)
{
return c1.k<c2.k;
}
int N,M,g[MAXN][MAXN][MAXN];
void init()
{
scanf("%d %d",&N,&M);
int i,j,k;
for(i=0; i<=N; i++)
{
for(j=0; j<=N; j++)
{
g[i][j][0]=INF;
}
}
for(i=1; i<=N; i++)
{
scanf("%d",&cops[i].k);
cops[i].idx=i;
}
sort(cops+1,cops+N+1,mycmp);
while(M--)
{
scanf("%d %d %d",&i,&j,&k);
i++;
j++;
g[i][j][0]=g[j][i][0]=k;
}
}
void floyd()
{
int i,j,k;
for(k=1; k<=N; k++)
{
for(i=1; i<=N; i++)
{
for(j=1; j<=N; j++)
{
g[i][j][k] = g[i][j][k-1];
}
}
int t = cops[k].idx;
for(i=1; i<=N; i++)
{
for(j=1; j<=N; j++)
{
if( g[i][j][k] > g[i][t][k-1] + g[t][j][k-1] )
g[i][j][k] = g[i][t][k-1] + g[t][j][k-1];
}
}
}
}
void solve()
{
init();
floyd();
int Q,i,j,k;
scanf("%d",&Q);
while(Q--)
{
scanf("%d %d %d",&i,&j,&k);
i++;
j++;
int t;
for(t=1; t<=N && cops[t].k<=k; t++) ;
if(g[i][j][t-1]==INF) printf("-1\n");
else printf("%d\n",g[i][j][t-1]);
}
printf("\n");
}
int main()
{
int T;
scanf("%d",&T);
while(T--) solve();
return 0;
}