Problem Description
zxa went to Q town as a volunteer, and the town mayor intended to achieve network coverage for the
n families living in the town. This
n families are able to be seen as the points in the axis, and the families from east to west are numbered from
1 to
n, where the distance between the
i-th family and the
(i+1)-th family is
d_i(1\leq i < n).
zxa was in charge of the planning of this project, and he was informed that the carriers were given two ways to set up the network. One way is using one wireless router and cables associated at the i-th family for some families network coverage, where the distance from the i-th family to each covered family (include the i-th family) is no more than r_i , which needs a_i costs. Another way is using one optical fiber cable at the i-th family for the i-th family network coverage, which needs b_i costs.
zxa is interested to know, assuming that it is only permitted to use at most k wireless routers for network coverage in order to avoid too large Wi-Fi radiation, then what is the minimum cost for this n families network coverage, can you help him?
zxa was in charge of the planning of this project, and he was informed that the carriers were given two ways to set up the network. One way is using one wireless router and cables associated at the i-th family for some families network coverage, where the distance from the i-th family to each covered family (include the i-th family) is no more than r_i , which needs a_i costs. Another way is using one optical fiber cable at the i-th family for the i-th family network coverage, which needs b_i costs.
zxa is interested to know, assuming that it is only permitted to use at most k wireless routers for network coverage in order to avoid too large Wi-Fi radiation, then what is the minimum cost for this n families network coverage, can you help him?
Input
The first line contains an positive integer
T, represents there are
T test cases.
For each test case:
The first line contains two positive integers n and k.
The second line contains (n-1) positive integers, represent d_1,d_2,\cdots,d_{n-1}.
The next n lines, the i-th line contains three positive integers a_i,r_i and b_i.
There is a blank between each integer with no other extra space in one line.
1\leq T\leq 100,2\leq n\leq 2\cdot10^4,1\leq k\leq\min(n, 100),1\leq a_i,b_i,d_i,r_i\leq 10^5,1\leq\sum{n}\leq10^5
For each test case:
The first line contains two positive integers n and k.
The second line contains (n-1) positive integers, represent d_1,d_2,\cdots,d_{n-1}.
The next n lines, the i-th line contains three positive integers a_i,r_i and b_i.
There is a blank between each integer with no other extra space in one line.
1\leq T\leq 100,2\leq n\leq 2\cdot10^4,1\leq k\leq\min(n, 100),1\leq a_i,b_i,d_i,r_i\leq 10^5,1\leq\sum{n}\leq10^5
Output
For each test case, output in one line a positive integer, repersents the minimum cost for this
n
families network coverage.
Sample Input
2 2 1 1 12 11 3 1 7 4 5 5 7 4 8 6 13 6 3 14 2 3 3 6 4 11 12 2 9 14 4
Sample Output
1 12HintFor the second sample, zxa used one wireless router at the $3$-th family and three optical fiber cables at the $1$-th family, the $4$-th family and the $5$-th family, so that the total cost is $3+3+2+4=12$.
把路由器当做是连接的边,然后就是dp过去,同时维护一个最小值即可。
#include<map> #include<set> #include<queue> #include<stack> #include<cmath> #include<cstdio> #include<bitset> #include<string> #include<vector> #include<cstring> #include<iostream> #include<algorithm> #include<functional> using namespace std; typedef long long LL; const int low(int x) { return x&-x; } const int INF = 0x7FFFFFFF; const int mod = 1e9 + 7; const int maxn = 2e4 + 10; int T, n, m, d[maxn], x; int a, b[maxn], R; int l, r; vector<pair<int, int> > p[maxn]; int dp[maxn][105], ans; int g[105][maxn]; int main() { scanf("%d", &T); while (T--) { scanf("%d%d", &n, &m); d[1] = 0; for (int i = 1; i < n; i++) { scanf("%d", &x); d[i + 1] = d[i] + x; } for (int i = 0; i <= n; i++) p[i].clear(); for (int i = 1; i <= n; i++) { scanf("%d%d%d", &a, &R, &b[i]); l = lower_bound(d + 1, d + n + 1, d[i] - R) - d - 1; r = upper_bound(d + 1, d + n + 1, d[i] + R) - d - 1; p[l].push_back(make_pair(r, a)); for (int j = 0; j <= m; j++) g[j][i] = dp[i][j] = INF; } memset(dp[0], 0, sizeof(dp[0])); for (int i = 0; i <= n; i++) { if (i) { for (int j = 0; j <= m; j++) { dp[i][j] = min(dp[i][j], dp[i - 1][j] + b[i]); for (int k = n - i + 1; k; k -= low(k)) { dp[i][j] = min(dp[i][j], g[j][k]); } } } if (i < n) { for (int j = 1; j <= m; j++) { for (int k = 0; k < p[i].size(); k++) { int u = p[i][k].first, v = p[i][k].second; dp[u][j] = min(dp[u][j], dp[i][j - 1] + v); for (int kk = n - u + 1; kk <= n; kk += low(kk)) { g[j][kk] = min(g[j][kk], dp[u][j]); } } } } } ans = INF; for (int i = m; i >= 0; i--) ans = min(ans, dp[n][i]); printf("%d\n", ans); } return 0; }