1018 Public Bike Management 结题记录(dfs剪枝)

个人觉得直接放入代码是最管用的。

其他方法类似,题意请参考其他博主。

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 50;

int maxn = 2000000000;
int c, n, ed, s[N], m, minlen, needn, backn, pre[N];
bool flag, book[N];
vector<pair<int, int > > e[N];

inline void dfs(int u, int points, int maxneed, int stores, int len)
{
    if (len > minlen) return ;
    int nd = points * c / 2 - stores;
    
    maxneed = max(maxneed, max(0, nd));

    if (u == ed) {
        int tneedn, tbackn;
        if (nd <= 0) {
            // dont need
            tneedn = maxneed; tbackn = -nd + maxneed;
        } else {
            // need;
            tneedn = max(maxneed, nd); tbackn = 0;
        }
        if (len == minlen) {
            if (needn > tneedn || (needn == tneedn && backn > tbackn)) {
                needn = tneedn; backn = tbackn;
            } 

        } else if (minlen > len) {
            minlen = len; needn = tneedn; backn = tbackn;
        }
        return ;
    }

    for (int i = 0; i < e[u].size(); i++) {
        int v = e[u][i].first, w = e[u][i].second;
        if (book[v]) continue;
        book[v] = true;
        dfs(v, points + 1, maxneed, stores + s[v], len + w);
        book[v] = false;
    }
}

inline void dfs2(int u, int points, int maxneed, int stores, int len)
{
    if (flag) return ;
    if (len > minlen) return ;

    int nd = points * c / 2 - stores;
    maxneed = max(maxneed, max(0, nd));

    if (u == ed) {
        int tneedn, tbackn;
        if (nd <= 0) {
            // dont need
            tneedn = maxneed; tbackn = -nd + maxneed;
        } else {
            // need;
            tneedn = max(maxneed, nd); tbackn = 0;
        }
        if (len == minlen) {
            if (tneedn == needn && tbackn == backn) {
                // puts("test 1");
                flag = true;
            }
        } 
        return ;
    }

    for (int i = 0; i < e[u].size(); i++) {
        int v = e[u][i].first, w = e[u][i].second;
        if (book[v]) continue;
        book[v] = true;
        pre[v] = u;
        dfs2(v, points + 1, maxneed, stores + s[v], len + w);
        if (flag) return ;
        book[v] = false;
    }
}

signed main()
{
    scanf("%d%d%d%d", &c, &n, &ed, &m);

    for (int i = 1; i <= n; i++) scanf("%d", &s[i]);

    while (m--) {
        int u, v, w; scanf("%d%d%d", &u, &v, &w);
        e[u].push_back({v, w});
        e[v].push_back({u, w});
    }
    minlen = maxn, needn = maxn, backn = maxn;
    book[0] = true;
    dfs(0, 0, 0, 0, 0);

    // printf("%d %d %d\n", minlen, needn, backn);

    memset(book, 0, sizeof book);
    book[0] = true;
    dfs2(0, 0, 0, 0, 0);

    vector<int > res;
    while (ed != 0) {
        res.push_back(ed); ed = pre[ed];
    }
    
    printf("%d %d", needn, 0);

    for (int i = res.size() - 1; i >= 0; i--) {
        printf("->%d", res[i]);
    }

    printf(" %d\n", backn);

    return 0;
}
相关推荐
肥猪猪爸36 分钟前
使用卡尔曼滤波器估计pybullet中的机器人位置
数据结构·人工智能·python·算法·机器人·卡尔曼滤波·pybullet
readmancynn1 小时前
二分基本实现
数据结构·算法
萝卜兽编程1 小时前
优先级队列
c++·算法
盼海1 小时前
排序算法(四)--快速排序
数据结构·算法·排序算法
一直学习永不止步1 小时前
LeetCode题练习与总结:最长回文串--409
java·数据结构·算法·leetcode·字符串·贪心·哈希表
Rstln2 小时前
【DP】个人练习-Leetcode-2019. The Score of Students Solving Math Expression
算法·leetcode·职场和发展
芜湖_2 小时前
【山大909算法题】2014-T1
算法·c·单链表
珹洺2 小时前
C语言数据结构——详细讲解 双链表
c语言·开发语言·网络·数据结构·c++·算法·leetcode
几窗花鸢3 小时前
力扣面试经典 150(下)
数据结构·c++·算法·leetcode
.Cnn3 小时前
用邻接矩阵实现图的深度优先遍历
c语言·数据结构·算法·深度优先·图论