| 题目编号 | T1 | T2 | T3 | T4 | 总结 |
|---|---|---|---|---|---|
| pts | 100 | 100 | 40 | 5 | 245 |
| 耗时 | 20min | 65min | 30min | 5min | 2h |
T1
世界上第一道看样例解释就会做的题诞生了。
解析:
观察样例解释,容易发现答案为 。
T2
状压DP大法好!!!
解析:
哈密瓜通路的板子题,注意该题的距离指的是图上两点的最短路径长度,并非欧式或曼哈顿距离。
跑一遍BFS再状压DP求哈密尔顿通路路径长度的最小值即可,此题固定起点,但并不固定终点。
Code
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e2 + 10, INF = 1e9 + 7;
const int D[][2] = {{0, 0}, {1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int n, m, t, dis[N][N], id[N][N], dp[1 << 18][18], ans = INF, cnt;
char mp[N][N];
bool vis[N][N];
struct node {
int x, y, dis;
} a[N * N];
bool check(int x, int y) {
return (x >= 1 && x <= n && y >= 1 && y <= m && mp[x][y] != '#' && !vis[x][y]);
}
void Bfs(int st) {
queue<node> Q;
Q.push(a[st]);
vis[a[st].x][a[st].y] = 1;
while(!Q.empty()) {
node now = Q.front();
Q.pop();
for(int i = 1, nx, ny; i <= 4; i ++) {
nx = now.x + D[i][0], ny = now.y + D[i][1];
if(check(nx, ny)) {
if(mp[nx][ny] == 'T' || mp[nx][ny] == 'S') {
if(now.dis + 1 <= dis[st][id[nx][ny]]) dis[st][id[nx][ny]] = now.dis + 1;
}
vis[nx][ny] = 1;
node nxt = {nx, ny, now.dis + 1};
Q.push(nxt);
}
}
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m >> t;
for(int i = 1; i <= n; i ++) {
for(int j = 1; j <= m; j ++) {
cin >> mp[i][j];
node tmp = {i, j, 0};
if(mp[i][j] == 'S') a[cnt ++] = tmp;
}
}
for(int i = 1; i <= n; i ++) {
for(int j = 1; j <= m; j ++) {
node tmp = {i, j, 0};
if(mp[i][j] == 'T') a[cnt ++] = tmp, id[i][j] = cnt - 1;
}
}
memset(dis, 127, sizeof dis);
for(int i = 0; i <= cnt; i ++) {
memset(vis, 0, sizeof vis);
Bfs(i);
}
for(int i = 0; i < 1 << cnt; i ++){
for(int j = 0; j < n; j ++){
dp[i][j] = 1e12;
}
}
dp[1][0] = 0ll;
for(int i = 1; i < 1 << cnt; i ++){
for(int j = 0; j < n; j ++){
if(i | (1 << j) == i){
for(int k = 0; k < cnt; k ++){
if(i | (1 << k) != i && k != j){
dp[i | (1 << k)][k] = min(dp[i | (1 << k)][k], dp[i][j] + dis[j][k] + t);
}
}
}
}
}
for(int i = 0; i < cnt; i ++) {
ans = min(ans, dp[(1 << cnt) - 1][i] + dis[i][0]);
}
cout << ans;
return 0;
}T3
绝美数学题,40pts真香。
解析:
两行列的多米诺骨牌摆放问题,很容易发现这跟斐波那契有关,因为当前列的方案数就是上上列+横着放两个或上一列+竖着放一个。
所以只考虑放障碍的情况,显然放奇数个障碍会导致方案数为零,所以障碍一定是偶数个的。
那么容易发现夹杂在障碍上下的方案是固定的,而左右两侧区块方案数可以根据乘法原理相乘得到,定义为斐波那契数列,为选中的区块长度,则答案为,满足。
记忆化DFS找一遍即可。
Code
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e2 + 10;
int dp[N];
map<int, int> f;
int T, m, minn = 2e9;
int dfs(int cur, int pre){
if(cur == 1) return -1;
if(f[cur] != 0) return f[cur];
f[cur] = 1e9;
for(int i = pre; i <= 87 && dp[i] <= cur; i ++){
if(cur % dp[i] == 0) f[cur] = min(f[cur], dfs(cur / dp[i], i) + i + 1);
}
return f[cur];
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
dp[1] = 1, dp[2] = 2;
for(int i = 3; i <= 87; i ++) dp[i] = dp[i - 1] + dp[i - 2];
cin >> T;
while(T --){
f.clear();
f[1] = 1;
cin >> m;
if(m == 1){
cout << "1\n";
continue;
}
minn = dfs(m, 2);
if(minn == 1e9) cout << "0\n";
else cout << minn << '\n';
}
return 0;
}T4
状压DP你加油。。。
解析:
此处引用齐国芒果的解析。
此题数据范围较小,明显是状态压缩dp。
但是需要注意的是,外卖此时有三种状态,分别是未取,取到了但是没有送达,送达。
在转移的时候,采用三进制表示,
上述的三种状态分别表示为 0,1,2 。
如果状态是从0变成了状态1,此时需要额外判断是否到达预设的时间,还没有的话需要等待。
如果状态是从1变成了状态2,直接判断时间是否合适。
另外处理任意两点之间的最短路,可以使用
floyd 。
code:
i % third[k + 1] / third[k];
可以直接处理三进制第k位的状态处理出最大的答案即可。
Code
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int N = 20 + 10, M = 6e4 + 10, INF = 1e9 + 7;
int n, m, q, dis[N][N], th[N], dp[N][M];
struct node{
int s, t, l, r;
}a[15];
void floyed(){
for(int k = 1; k <= n; k ++)
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= n; j ++)
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
}
void init(){
th[1] = 1;
for(int i = 2; i <= q + 1; i ++) th[i] = th[i - 1] * 3;
}
void solve(int Max){
memset(dp, 0x3f, sizeof dp);
dp[1][0] = 0;
for(int i = 0; i <= Max; i ++){
for(int j = 1; j <= n; j ++){
for(int k = 1; k <= q; k ++){
int tmp = i % th[k + 1] / th[k];
if(tmp == 0 && dp[j][i] + dis[j][a[k].s] <= a[k].r){
dp[a[k].s][i + th[k]] = min(dp[a[k].s][i + th[k]], max(a[k].l, dp[j][i] + dis[j][a[k].s]));
}
if(tmp == 1 && dp[j][i] + dis[j][a[k].t] <= a[k].r){
dp[a[k].t][i + th[k]] = min(dp[a[k].t][i + th[k]], dp[j][i] + dis[j][a[k].t]);
}
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m >> q;
for(int i = 1; i <= n; i ++){
for(int j = 1; j <= n; j ++){
if(i == j){
dis[i][j] = 0;
} else{
dis[i][j] = 0x3f3f3f3f;
}
}
}
int st, ed, val;
while(m --){
cin >> st >> ed >> val;
dis[st][ed] = min(dis[st][ed], val);
}
for(int i = 1; i <= q; i ++){
cin >> a[i].s >> a[i].t >> a[i].l >> a[i].r;
}
floyed();
init();
solve(th[q + 1] - 1);
int ans = 0;
for(int i = 1; i <= n; i ++){
for(int j = 0; j <= th[q + 1] - 1; j ++){
if(dp[i][j] >= 0x3f3f3f3f){
continue;
}
int tmp = 0;
for(int k = 1; k <= q; k ++){
if((j % th[k + 1] / th[k]) == 2) tmp ++;
}
ans = max(ans, tmp);
}
}
cout << ans;
return 0;
}