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;
}
相关推荐
九圣残炎30 分钟前
【从零开始的LeetCode-算法】1456. 定长子串中元音的最大数目
java·算法·leetcode
lulu_gh_yu36 分钟前
数据结构之排序补充
c语言·开发语言·数据结构·c++·学习·算法·排序算法
丫头,冲鸭!!!1 小时前
B树(B-Tree)和B+树(B+ Tree)
笔记·算法
Re.不晚1 小时前
Java入门15——抽象类
java·开发语言·学习·算法·intellij-idea
为什么这亚子2 小时前
九、Go语言快速入门之map
运维·开发语言·后端·算法·云原生·golang·云计算
2 小时前
开源竞争-数据驱动成长-11/05-大专生的思考
人工智能·笔记·学习·算法·机器学习
~yY…s<#>2 小时前
【刷题17】最小栈、栈的压入弹出、逆波兰表达式
c语言·数据结构·c++·算法·leetcode
幸运超级加倍~3 小时前
软件设计师-上午题-16 算法(4-5分)
笔记·算法
yannan201903133 小时前
【算法】(Python)动态规划
python·算法·动态规划
埃菲尔铁塔_CV算法3 小时前
人工智能图像算法:开启视觉新时代的钥匙
人工智能·算法