L2-036 网红点打卡攻略

题目链接

思路:
邻接表存图,用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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值