#include <bits/stdc++.h>
using namespace std;
const int MAX_E = 200;
const int MAX_N = 2000;
const int INF = 1 << 30;
int kase, tot, num_v, num_e, way[MAX_N], dist[MAX_N], pre[MAX_N], a, b, head[MAX_N];
bool vis[MAX_N];
struct Edge
{
int u, v, cost , next;
Edge() {}
Edge(int a, int b, int c, int d): u(a), v(b), cost(c), next(d) {}
Edge(int a, int c): u(a), cost(c) {}
bool operator<(const Edge &x) const
{
return cost < x.cost;
}
};
Edge edge[MAX_E];
void SPFA(int s = 1)
{
for (int i = 1; i <= num_v; i++)
dist[i] = (i == s ? 0 : -INF);
queue<Edge>que;
que.push(Edge(s, 0));
vis[s] = true;
while (!que.empty())
{
Edge tmp = que.front();
int u = tmp.u;
que.pop();
vis[u] = false;
for (int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].v;
if (dist[v] < edge[i].cost + dist[u])
{
dist[v] = edge[i].cost + dist[u];
pre[v] = u;
if (!vis[v])
{
que.push(Edge(v, dist[v]));
vis[v] = true;
}
}
}
}
return ;
}
void addedge(int u, int v, int cost)
{
Edge e = Edge(u, v, cost, head[u]);
edge[tot] = e;
head[u] = tot++;
return ;
}
int main(int argc, char const *argv[])
{
cin >> kase;
for (int T = 1; T <= kase; T++)
{
tot=0;
memset(vis, 0, sizeof(vis));
memset(head, -1, sizeof(head));
cin >> num_v;
for (int i = 1; i <= num_v; i++)
cin >> way[i];
way[++num_v] = way[1];
cin >> num_e;
for (int i = 0; i < num_e; i++)
{
cin >> a >> b;
addedge(min(a, b), max(a, b), way[max(a, b)]);
}
SPFA();
cout << "CASE " << T << "#" << endl << "points : " << dist[num_v] << endl << "circuit : 1";
int cnt = 0;
for (int j = pre[num_v]; j != 1; ++cnt, j = pre[j])
head[cnt] = j;
for (int i = cnt - 1; i >= 0; --i)
cout << "->" << head[i];
cout << "->1" << endl;
if (T != kase)
cout << endl;
}
return 0;
}
111
挖坑代填