思路:
邻接表存图,用path数组存待判路径,然后对每条路径dfs即可,
当时一直把dfs中的 x 看成了点,其实x是路径数组中点的索引,
后面又因为题意没看清,以为是到达过所有点就满足,但题意是所有景点到达一次。
代码:
#include <bits/stdc++.h>
#define fastio ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define debug(a) cout << "debug : " << (#a) << " = " << a << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int N = 210;
const int INF = 0x3f3f3f3f;
const double eps = 1e-3;
const int mod = 998244353;
int n, m, w;
vector<PII> a[N]; //记录邻接表
vector<PII> res; //记录答案
vector<int> path; //记录待判路线
bool flag;
int arr_num[N]; //记录到达次数
void dfs(int x)
{
if (x == path.size() - 1)
{
for (int i = 1; i <= n; i++)
{
if (arr_num[i] != 1)
return;
}
flag = true;
}
else
{
for (auto p : a[path[x]])
{
if (p.second == path[x + 1])
{
arr_num[path[x]]++;
w += p.first;
dfs(x + 1);
break;
}
}
}
}
int main()
{
cin >> n >> m;
for (int i = 1; i <= m; i++)
{
int l, r, w;
cin >> l >> r >> w;
a[l].push_back({w, r}), a[r].push_back({w, l});
}
int k, j;
cin >> k;
for (int i = 1; i <= k; i++)
{
w = 0, flag = false;
cin >> j;
path.clear();
path.push_back(0);
while (j--)
{
int x;
cin >> x;
path.push_back(x);
}
path.push_back(0);
memset(arr_num, 0, sizeof arr_num);
dfs(0);
if (flag)
res.push_back({w, i});
}
sort(res.begin(), res.end());
cout << res.size() << endl;
cout << res[0].second << ' ' << res[0].first << endl;
return 0;
}