第三题: void helper(int idx, int nums, int &res, int cur, vector<vector<pair<int, int>>> &neigh, vector<bool> &flag) {     if (idx == 0 && flag[idx] == true) {         if (nums == flag.size())             res = min(res, cur);         return;     }     for (int i = 0; i < neigh[idx].size(); i++) {         if (flag[neigh[idx][i].first] == false) {             flag[neigh[idx][i].first] = true;             helper(neigh[idx][i].first, nums + 1, res, cur + neigh[idx][i].second, neigh, flag);             flag[neigh[idx][i].first] = false;         }     } } int main() {     int n, m;     cin >> n >> m;     if (n == 1) {         cout << 0 << endl;         return 0;     }     vector<vector<pair<int, int>>> neigh(n);     while (m--) {         int a, b, t;         cin >> a >> b >> t;         neigh[a].push_back(make_pair(b, t));         neigh[b].push_back(make_pair(a, t));     }     vector<bool> flag(n, false);     int res = INT_MAX;     helper(0, 0, res, 0, neigh, flag);     if (res == INT_MAX)         cout << -1 << endl;     else         cout << res << endl;     system("pause");     return 0; }
点赞 6

相关推荐

点赞 评论 收藏
分享
双尔:反手回一个很抱歉,经过慎重考虑,您与我的预期暂不匹配,感谢您的投递
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务