/*
The shortest circuit problem,
Test instructions: A Q represents the maximum number of stops that can be stopped in the process of arriving at a destination.
Method: Use a DP array dp[i][j] to represent the minimum cost of Stop J times for station I.
Then BFS will be able to
*/
#include <cstdio> #include <cstring>
#include <string>
#include <map>
#include <vector>
#include <queue>
#define M 1010
using namespace Std;
struct Edge
{
int from,to,dist;
};
struct Node
{
int u,cost,step;
};
Char ct[110][30];
Vector<edge> edges;
Vector<int> G[m];
Map<string,int> ID;
int n,m,inq[m][m],cnt[m],dp[m][m];
int init ()
{
Id.clear ();
Edges.clear ();
for (int i = 0; i < n; i++)
G[i].clear ();
}
int add (int from,int to,int Dist)
{
Edges.push_back (Edge)
{
From,to,dist
});
int L = edges.size ();
G[from].push_back (L-1);
}
int BFS (int strat,int end,int len)
{
Queue<node> Q;
memset (inq,0,sizeof (INQ));
int INF = 1<<30;
for (int i = 0; i < n; i++)
for (int j = 0; J <= Len; j + +)
DP[I][J] = INF;
while (!q.empty ())
Q.pop ();
Dp[strat][0] = 0;
Inq[strat][0] = 1;
Node A;
A.U = Strat;
A.cost = 0;
A.step = 0;
Q.push (a);
int u,v,s,c,dd;
while (!q.empty ())
{
A = Q.front ();
Q.pop ();
u = a.u;
c = a.cost;
s = a.step;
printf ("-----\n%d%d%d\n", u,s,c);
Inq[u][s] = 0;
for (int i = 0; i < g[u].size (); i++)
{
Edge &e = edges[g[u][i]];
v = e.to;
dd = e.dist;
if (S+1<=LEN&&DP[V][S+1]>DP[U][S]+DD)
{
DP[V][S+1]=DP[U][S]+DD;
if (!inq[v][s+1])
{//printf ("%d%d\n", v,s+1);
INQ[V][S+1] = 1;
a.u = v;
A.step = s+1;
A.cost = dp[v][s+1];
Q.push (a);
}
}
}
}
int ans = INF;
for (int i = 0; I <= len; i++)
if (ans > dp[end][i])
ans = dp[end][i];
if (ans = = INF)
printf ("No satisfactory flights\n");
Else
printf ("Total cost of flight (s) is $%d\n", ans);
}
int main ()
{
int t,strat,end,count=0;
scanf ("%d", &t);
while (t--)
{
count++;
scanf ("%d", &n);
Init ();
for (int i = 0; i < n; i++)
{
scanf ("%s", Ct[i]);
Id[ct[i]] = i;
if (!strcmp (Ct[i], "Calgary"))
Strat = i;
if (!strcmp (Ct[i], "Fredericton"))
end = i;
}
scanf ("%d", &m);
Char t1[30],t2[30];
int d,u,v;
for (int i = 0; i < m; i++)
{
scanf ("%s%s%d", t1,t2,&d);
U = id[t1];
v = id[t2];
Add (u,v,d);
}
if (count>1)
Puts ("");
printf ("Scenario #%d\n", count);
int p,sp;
scanf ("%d", &p);
while (p--)
{
scanf ("%d", &SP);
printf ("%d..!!!! \ n ", SP);
BFS (strat,end,sp+1);
}
}
return 0;
}