Tinkoff Challenge

D. Presents in Bankopolis
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Bankopolis is an incredible city in which all the n crossroads are located on a straight line and numbered from 1 to n along it. On each crossroad there is a bank office.

The crossroads are connected with m oriented bicycle lanes (the i-th lane goes from crossroad ui to crossroad vi), the difficulty of each of the lanes is known.

Oleg the bank client wants to gift happiness and joy to the bank employees. He wants to visit exactly k offices, in each of them he wants to gift presents to the employees.

The problem is that Oleg don't want to see the reaction on his gifts, so he can't use a bicycle lane which passes near the office in which he has already presented his gifts (formally, the i-th lane passes near the office on the x-th crossroad if and only if min(ui, vi) < x < max(ui, vi))). Of course, in each of the offices Oleg can present gifts exactly once. Oleg is going to use exactly k - 1bicycle lane to move between offices. Oleg can start his path from any office and finish it in any office.

Oleg wants to choose such a path among possible ones that the total difficulty of the lanes he will use is minimum possible. Find this minimum possible total difficulty.

Input

The first line contains two integers n and k (1 ≤ n, k ≤ 80) — the number of crossroads (and offices) and the number of offices Oleg wants to visit.

The second line contains single integer m (0 ≤ m ≤ 2000) — the number of bicycle lanes in Bankopolis.

The next m lines contain information about the lanes.

The i-th of these lines contains three integers uivi and ci (1 ≤ ui, vi ≤ n1 ≤ ci ≤ 1000), denoting the crossroads connected by the i-th road and its difficulty.

Output

In the only line print the minimum possible total difficulty of the lanes in a valid path, or -1 if there are no valid paths.

Examples
input
7 4
4
1 6 2
6 2 2
2 4 2
2 7 1
output
6
input
4 3
4
2 1 2
1 3 2
3 4 2
4 1 1
output
3
Note

In the first example Oleg visiting banks by path 1 → 6 → 2 → 4.

Path 1 → 6 → 2 → 7 with smaller difficulity is incorrect because crossroad 2 → 7 passes near already visited office on the crossroad 6.

In the second example Oleg can visit banks by path 4 → 1 → 3.

题目大意:有n个十字路口,从左到右直线相连,每个路口都有一个办公室。另外有m个自行车道,使u和v的路口相连,这条路有一个权值。

这个主人公想通过自行车道逛k个办公室,但是他不能经过他已经拜访过的办公室。

例如:他的拜访顺序为 1,6, 2,他下一个的拜访顺序要在(2, 6)区间内,所以不能拜访7

题目解析:我们可以枚举起始点u,我们可以拜访的区间范围为[l, r], 然后从u的子节点v,

如果v < u, 那么我们可以搜[l, u]和[u, r]了;

那么取这个搜索的最小值就好了,具体看代码

#include <bits/stdc++.h>

using namespace std;
typedef pair<int, int>P;
vector<P>G[105];
int dp[85][85][85][85], n, m, k;

int ans = 0;
int dfs(int x, int y, int u, int cnt){
    if(cnt == k) return 0;
    int &ret = dp[cnt][x][y][u];
    if(ret >= 0) return ret;
    ret = 0x3f3f3f3f;
    for(auto v: G[u]){
        if(v.first > x && v.first < y) {
            if(u > v.first)
            ret = min(ret, v.second + dfs(x, u, v.first, cnt+1));
            else
            ret = min(ret, v.second + dfs(u, y, v.first, cnt+1));
        }
    }
    return ret;
}
int main() {
    scanf("%d%d%d", &n, &k, &m);
    memset(dp, -1, sizeof(dp));
    for(int i = 0, u, v, d; i < m; i++){
        scanf("%d%d%d", &u, &v, &d), G[u].push_back(P(v, d));
    }
    int ans = 0x3f3f3f3f;
    for(int i = 1; i <= n; i++){
        ans = min(ans, dfs(0, n+1, i, 1));
    }
    if(ans == 0x3f3f3f3f) printf("-1
");
    else printf("%d
", ans);
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/cshg/p/6759589.html