数据规模较小,直接暴力模拟即可。
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int n, m;
int g[210][210], st[210][210];
int path[210];
int ans = 1e9, cnt, resid;
int main ()
{
cin >> n >> m;
for (int i = 1; i <= m; i ++)
{
int a, b, c;
cin >> a >> b >> c;
g[a][b] = g[b][a] = c; //记录两个景点的距离
st[a][b] = st[b][a] = 1; //两个景点是否联通
}
int k; cin >> k;
for (int j = 1; j <= k; j ++)
{
int flag = 0;
set <int> s;
int tot; cin >> tot;
for (int i = 1; i <= tot; i ++)
{
cin >> path[i];
s.insert (path[i]);
}
path[0] = 0; path[tot + 1] = 0;
if (tot != n) continue; //数量合不合法
if (s.size () != n) continue; // 景点数量不合法
int sum = 0;
for (int i = 1; i <= tot + 1; i ++)
{
int a = path[i - 1], b = path[i];
if (st[a][b])
sum += g[a][b];
else
flag = 1;
}
if (flag == 1) continue;
cnt ++;
if (sum < ans) //更新最小值
{
ans = sum;
resid = j;
}
}
cout << cnt << "\n";
cout << resid << " " << ans;
return 0;
}