L2-036 网红点打卡攻略 (25 分)

题目链接

数据规模较小,直接暴力模拟即可。

代码:

#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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值