C++
#include <iostream>
#include <string.h>
using namespace std;
const int N = 1010, INF = 0x3f3f3f3f;
int g[N][N];
int vis[N];
int a[N]; //store k rounds of node lists
int dist[N];
int n, m;
bool dijkstra()
{
memset(vis, 0, sizeof(vis)); //new case, refresh
memset(dist, 0x3f, sizeof(dist));
dist[a[1]] = 0;
bool valid = true;
for (int i = 1; i <= n; i++ ) {
int c_min = INF; //compute current minimum dist[]
for (int j = 1; j <= n; j++ ) {
if (!vis[j]) {
c_min = min(c_min, dist[j]);
}
}
//if the sequence is valid, a[i] must have the minimum dist, if not, this sequence is invalid
if (dist[a[i]] != c_min) {
valid = false;
break;
}
int u = a[i];
vis[u] = 1;
for (int j = 1; j <= n; j++ ) {
if (!vis[j] and dist[j] > dist[u] + g[u][j]) {
dist[j] = dist[u] + g[u][j];
}
}
}
//having done the whole algorithm, valid = true means yes, otherwise no
return valid;
}
int main()
{
memset(g, 0x3f, sizeof(g));
scanf("%d%d", &n, &m);
while (m-- ) {
int u, v, w; scanf("%d%d%d", &u, &v, &w);
g[u][v] = g[v][u] = w; //building grid;
}
int k; scanf("%d", &k);
while (k-- ) {
for (int i = 1; i <= n; i++ ) scanf("%d", &a[i]); //get data from current round
if (dijkstra()) {
printf("Yes");
}
else {
printf("No");
}
if (k) puts("");
}
return 0;
}