[区间DP] Tinkoff Challenge - Elimination Round D题
Posted 晁棠
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[区间DP] Tinkoff Challenge - Elimination Round D题相关的知识,希望对你有一定的参考价值。
NamomoCamp Daily 2
codeforces原题网址https://codeforces.com/problemset/problem/793/D
代码源oj网址http://oj.daimayuan.top/problem/437
题解:
两个地方的条件有点小小的不同,一个是开区间一个是闭区间,但是没什么影响,而且写法也可以规避出现环的情况。
注意
记忆化搜索写法会更慢。
代码:
DP
// Good Good Study, Day Day AC.
#include <iostream>
#include <stdio.h>
#include <cstdio>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <cstring>
#include <math.h>
#include <cmath>
#include <queue>
#include <deque>
#include <stack>
#include <vector>
#include <map>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#define ffor(i,a,b) for(int i=(a) ;i<=(b) ;i++)
#define rrep(i,a,b) for(int i=(a) ;i>=(b) ;i--)
#define mst(v,s) memset(v,s,sizeof(v))
#define IOS ios::sync_with_stdio(false),cin.tie(0)
#define ll long long
#define INF 0x7f7f7f7f7f7f7f7f
#define inf 0x7f7f7f7f
#define PII pair<int,int>
//#define int long long
using namespace std;
const int N = 103, M = 2020;
int n, T, k, m;
int pi, p[N], nxt[M], to[M], cost[M];
inline void add_in(int u, int v, int c)
pi++; nxt[pi] = p[u]; p[u] = pi; to[pi] = v; cost[pi] = c;
void ready()
IOS;
cin >> n >> k >> m;
//ffor(i, 1, n) ffor(j, 1, n) ma[i][j] = INF;
ffor(i, 1, m)
int u, v, c;
cin >> u >> v >> c;
add_in(v, u, c);
//ma[u][v] = min(ma[u][v], c);
struct DP
int L, R, Cnt, Flag;
;
int dp[N][N][N][3];
bool vis[N][N][N][3];
void work()
mst(dp, inf);
int ans = inf;
queue<DP> q;
ffor(i, 1, n)
q.push( i,i,1,0 );
q.push( i,i,1,1 );
dp[i][i][1][0] = dp[i][i][1][1] = 0;
vis[i][i][1][0] = vis[i][i][1][1] = true;
while (q.size())
DP U = q.front(); q.pop();
int l = U.L, r = U.R, cnt = U.Cnt, flag = U.Flag;
vis[l][r][cnt][flag] = false;
if (cnt == k) continue;
int u;
if (flag) u = r;
else u = l;
for (int ki = p[u]; ki; ki = nxt[ki])
int v = to[ki], c = cost[ki];
if (v < l)
if (dp[l][r][cnt][flag] + c <= dp[v][r][cnt + 1][0])
dp[v][r][cnt + 1][0] = dp[l][r][cnt][flag] + c;
if (!vis[v][r][cnt + 1][0])
vis[v][r][cnt + 1][0] = true;
q.push( v,r,cnt + 1,0 );
if (v > r)
if (dp[l][r][cnt][flag] + c <= dp[l][v][cnt + 1][1])
dp[l][v][cnt + 1][1] = dp[l][r][cnt][flag] + c;
if (!vis[l][v][cnt + 1][1])
vis[l][v][cnt + 1][1] = true;
q.push( l,v,cnt + 1,1 );
ffor(i, 1, n)
ffor(j, 1, n)
ans = min(ans, dp[i][j][k][0]);
ans = min(ans, dp[i][j][k][1]);
if (ans == inf) ans = -1;
cout << ans;
int main()
ready();
work();
return 0;
记忆化搜索
// Good Good Study, Day Day AC.
#include <iostream>
#include <stdio.h>
#include <cstdio>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <cstring>
#include <math.h>
#include <cmath>
#include <queue>
#include <deque>
#include <stack>
#include <vector>
#include <map>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#define ffor(i,a,b) for(int i=(a) ;i<=(b) ;i++)
#define rrep(i,a,b) for(int i=(a) ;i>=(b) ;i--)
#define mst(v,s) memset(v,s,sizeof(v))
#define IOS ios::sync_with_stdio(false),cin.tie(0)
#define ll long long
#define INF 0x7f7f7f7f7f7f7f7f
#define inf 0x7f7f7f7f
#define PII pair<int,int>
//#define int long long
using namespace std;
const int N = 103, M = 2020;
int n, T, k, m;
int pi, p[N], nxt[M], to[M], cost[M];
inline void add_in(int u, int v, int c)
pi++; nxt[pi] = p[u]; p[u] = pi; to[pi] = v; cost[pi] = c;
void ready()
IOS;
cin >> n >> k >> m;
//ffor(i, 1, n) ffor(j, 1, n) ma[i][j] = INF;
ffor(i, 1, m)
int u, v, c;
cin >> u >> v >> c;
add_in(v, u, c);
//ma[u][v] = min(ma[u][v], c);
int dp[N][N][N][3];
int dfs(int l, int r, int cnt, int flag)
if (dp[l][r][cnt][flag] != inf) return dp[l][r][cnt][flag];
if (cnt == k)
return dp[l][r][cnt][flag]=0;
if (flag == 0)
int u = l;
for (int ki = p[u]; ki; ki = nxt[ki])
int v = to[ki], c = cost[ki];
if (v<l)
dp[l][r][cnt][flag] = min(dp[l][r][cnt][flag], dfs(v, r, cnt + 1, 0) + c);
if (v > r)
dp[l][r][cnt][flag] = min(dp[l][r][cnt][flag], dfs(l, v, cnt + 1, 1) + c);
else
int u = r;
for (int ki = p[u]; ki; ki = nxt[ki])
int v = to[ki], c = cost[ki];
if (v < l)
dp[l][r][cnt][flag] = min(dp[l][r][cnt][flag], dfs(v, r, cnt + 1, 0) + c);
if (v > r)
dp[l][r][cnt][flag] = min(dp[l][r][cnt][flag], dfs(l, v, cnt + 1, 1) + c);
return dp[l][r][cnt][flag];
void work()
mst(dp, inf);
int ans = inf;
ffor(i, 1, n)
ans = min(ans, dfs(i, i, 1, 0));
ans = min(ans, dfs(i, i, 1, 1));
if (ans == inf) ans = -1;
cout << ans;
int main()
ready();
work();
return 0;
以上是关于[区间DP] Tinkoff Challenge - Elimination Round D题的主要内容,如果未能解决你的问题,请参考以下文章
Tinkoff Challenge - Elimination Round 部分解题报告