SP703 SERVICE - Mobile Service
SP703 SERVICE - Mobile Service - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
容易想到状态
发现状态规模达到
更换定义,
初始时
时间复杂度
/*
* @Author: crab-in-the-northeast
* @Date: 2022-11-09 04:32:07
* @Last Modified by: crab-in-the-northeast
* @Last Modified time: 2022-11-09 04:53:57
*/
#include <bits/stdc++.h>
inline int read() {
int x = 0;
bool f = true;
char ch = getchar();
for (; !isdigit(ch); ch = getchar())
if (ch == '-')
f = false;
for (; isdigit(ch); ch = getchar())
x = (x << 1) + (x << 3) + ch - '0';
return f ? x : (~(x - 1));
}
inline bool gmi(int &a, int b) {
return b < a ? a = b, true : false;
}
const int maxl = 205;
const int maxn = 1005;
int c[maxl][maxl];
int p[maxn];
int f[maxn][maxl][maxl];
int main() {
int T = read();
while (T--) {
int l = read(), n = read();
std :: memset(f, 0x3f, sizeof(f));
for (int i = 1; i <= l; ++i)
for (int j = 1; j <= l; ++j)
c[i][j] = read();
for (int i = 1; i <= n; ++i)
p[i] = read();
p[0] = 1;
f[0][2][3] = 0;
for (int i = 0; i <= n; ++i) {
for (int x = 1; x <= l; ++x) {
for (int y = 1; y <= l; ++y) {
int z = p[i], t = p[i + 1];
int fn = f[i][x][y];
if (x == y || y == z || x == z)
continue;
if (x != t && y != t)
gmi(f[i + 1][x][y], fn + c[z][t]);
if (x != t && z != t)
gmi(f[i + 1][x][z], fn + c[y][t]);
if (y != t && z != t)
gmi(f[i + 1][y][z], fn + c[x][t]);
}
}
}
int ans = INT_MAX;
for (int x = 1; x <= l; ++x)
for (int y = 1; y <= l; ++y)
if (x != y && x != p[n] && y != p[n])
gmi(ans, f[n][x][y]);
printf("%d\n", ans);
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】