BZOJ 物流运输

一个神奇的dp,,为数不多自己搞出来的dp。。。。
其实可以发现对于这个题, 单纯的最短路乱搞是错误的
那么,,,,dp
我们可以用cost[i][j] 表示从第i天到第j天的不换路花费, 当然算的时候保证这条路在i到j天都可以走, 那么是一定存在每个时间段不存在一条从起点到终点的道路的, 那么这个时候要判断一下赋值成+oo
我们再用dp[i] 表示前i天的总花费

dp[i] = min{dp[j] + cost[j+1][i] + k| 0<=j< i};

那么由于我们在算第一天的时候多加了一个k, 那么结果减去一个k就好了

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const int N = 200;
const int M = 100*100;
const int oo = 0x3f3f;
#define next Next
#define begin Begin
#define rep(i, s, t) for(int i = s; i <= t; ++i)

int d, t, n, m;
long long k;
int pd[N][N];
struct node {
    int u, d;
    bool operator <(const node& rhs) const{
        return d > rhs.d;
    }
};
struct dijkstra {
    long long f[N], cost[N][N], dis[N];
    int vis[N];
    int begin[N], to[M], next[M], w[M], e; 

    void init() {
        e = 0; 
        memset(begin, 0, sizeof(begin));
    }

    void add(int x, int y, int z) {
        to[++e] = y;
        next[e] = begin[x];
        w[e] = z;
        begin[x] = e;
    }

    void dij(int s, int S, int T) {
        memset(vis, 0, sizeof(vis));
        priority_queue<node > q;
        rep(i, 1, n) dis[i] = oo;
        dis[s] = 0; q.push((node) {s, dis[s]});
        while(!q.empty()) {
            int u = q.top().u, v; q.pop();
            if(vis[u]) continue;
            bool flag = false;
            rep(o, S, T) if(pd[u][o]) {flag = true; break;}
            if(flag) continue;
            vis[u] = 1;
            for(int i = begin[u]; i; i = next[i])
                if(dis[v = to[i]] > dis[u] + w[i])
                    dis[v] = dis[u] + w[i], q.push((node) {v, dis[v]});
        }
    }

/*  void doit() {
        rep(i, 1, n) la[i] = pre[i];
    }
    bool check() {
        int a = n, b = n;
        while(a != 1 && b != 1) {
            a = la[a], b = pre[b];
        //  printf("%d %d!!\n", a, b);
            if(a != b) return true;
        }
        return false;
    }*/

    void solve() {
        init();
        scanf("%d%d%lld%d", &t, &n, &k, &m);
        rep(i, 1, m) {
            int x, y, z;
            scanf("%d%d%d", &x, &y, &z);
            add(x, y, z); add(y, x, z);
        }
        scanf("%d", &d);
        rep(i, 1, d) {
            int u, l, r;
            scanf("%d%d%d", &u, &l, &r);
            rep(j, l, r) pd[u][j] = 1;
        }
        rep(i, 1, t)
            rep(j, i, t) {
//              doit();
                dij(1, i, j);
//              res += dis[n];
                cost[i][j] = dis[n] >= oo ?dis[n] : dis[n] * (j-i+1);
                //  if(check()) ++cnt;
            }
        memset(f, 0x3f3f3f, sizeof(f));
        f[0] = 0;
        rep(i, 1, t)
            rep(j, 0, i-1)
                f[i] = min(f[i], f[j]+cost[j+1][i]+k);
        printf("%lld\n", f[t]-k);
    }
}T;

int main() {
#ifndef ONLINE_JUDGE
    freopen("trans.in", "r", stdin);
    freopen("trans.out", "w", stdout);
#endif
    T.solve();
    return 0;
}

毕。

### NOIP2015 运输计划 BZOJ4326 题解分析 #### 问题背景 该问题是经典的图论优化问题之一,主要考察树结构上的路径操作以及高效的数据处理能力。题目要求在一个由 $n$ 个节点组成的无向连通树中找到最优的一条边将其改造为虫洞(通过此边不需要耗费时间),从而使得给定的 $m$ 条运输路径中的最长耗时最小化。 --- #### 解决方案概述 解决这一问题的核心在于利用 **二分答案** 和 **树上差分技术** 的组合来实现高效的计算过程。以下是具体的技术细节: 1. **二分答案**: 设当前目标是最小化的最大路径长度为 $T_{\text{max}}$。我们可以通过二分的方式逐步逼近最终的结果。每次尝试验证是否存在一种方式将某条边改为虫洞后使所有路径的最大值不超过当前设定的目标值 $mid$[^1]。 2. **路径标记与统计**: 使用树上差分的思想对每一条路径进行标记并快速统计受影响的情况。假设两点之间的最近公共祖先 (Lowest Common Ancestor, LCA) 是 $r = \text{lca}(u_i, v_i)$,则可以在三个位置分别施加影响:增加 $(u_i + 1), (v_i + 1)$ 同时减少 $(r - 2)$。这种操作能够有效覆盖整条路径的影响范围,并便于后续统一查询和判断[^1]。 3. **数据结构支持**: 结合线段树或者 BIT (Binary Indexed Tree),可以进一步加速区间修改和单点查询的操作效率。这些工具帮助我们在复杂度范围内完成大量路径的同时更新和检索需求[^2]。 4. **实际编码技巧**: 实现过程中需要注意一些边界条件和技术要点: - 正确维护 DFS 序列以便映射原树节点到连续编号序列; - 准备好辅助函数用于快速定位 LCA 节点及其对应关系; - 编码阶段应特别留意变量初始化顺序及循环终止逻辑以防潜在错误发生。 下面给出一段基于上述原理的具体 Python 实现代码作为参考: ```python from collections import defaultdict, deque class Solution: def __init__(self, n, edges): self.n = n self.graph = defaultdict(list) for u, v, w in edges: self.graph[u].append((v, w)) self.graph[v].append((u, w)) def preprocess(self): """Preprocess the tree to get dfs order and lca.""" pass def binary_search_answer(self, paths): low, high = 0, int(1e9) best_possible_time = high while low <= high: mid = (low + high) // 2 if self.check(mid, paths): # Check feasibility with current 'mid' best_possible_time = min(best_possible_time, mid) high = mid - 1 else: low = mid + 1 return best_possible_time def check(self, limit, paths): diff_array = [0]*(self.n+1) for path_start, path_end in paths: r = self.lca(path_start, path_end) # Apply difference on nodes based on their relationship. diff_array[path_start] += 1 diff_array[path_end] += 1 diff_array[r] -= 2 suffix_sum = [sum(diff_array[:i]) for i in range(len(diff_array)+1)] # Verify whether any edge can be modified within given constraints. possible_to_reduce_max = False for node in range(1, self.n+1): parent_node = self.parent[node] if suffix_sum[node]-suffix_sum[parent_node]>limit: continue elif not possible_to_reduce_max: possible_to_reduce_max=True return possible_to_reduce_max # Example usage of class methods would follow here... ``` --- #### 总结说明 综上所述,本题的关键突破点在于如何巧妙运用二分策略缩小搜索空间,再辅以恰当的树形结构遍历技术和差分手段提升整体性能表现。这种方法不仅适用于此类特定场景下的最优化求解任务,在更广泛的动态规划领域也有着广泛的应用前景[^3]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值