Description
一个公司有三个移动服务员。如果某个地方有一个请求,某个员工必须赶到那个地方去(那个地方没有其他员工),某一时刻只有一个员工能移动。被请求后,他才能移动,不允许在同样的位置出现两个员工。从p到q移动一个员工,需要花费c(p,q)。这个函数没有必要对称,但是c(p,p)=0。公司必须满足所有的请求。目标是最小化公司花费。
Input Format
第一行有两个整数L,N(3<=L<=200, 1<=N<=1000)。L是位置数;N是请求数。每个位置从1到L编号。下L行每行包含L个非负整数。第i+1行的第j个数表示c(i,j) ,并且它小于2000。最后一行包含N个数,是请求列表。一开始三个服务员分别在位置1,2,3。Output Format
一个数M,表示最小服务花费。Sample Input
5 9
0 1 1 1 1
1 0 2 3 2
1 1 0 4 1
2 1 5 0 1
4 2 3 4 0
4 2 4 1 5 4 3 2 1
Sample Output
5
Hint
Limitation
各个测试点1s
易设出状态
fk,x,y,z
f
k
,
x
,
y
,
z
表示完成第
k
k
个服务,三个服务生分别位于
每次大力转移
复杂度
O(qn3)
O
(
q
n
3
)
显然可以发现每次
x,y,z
x
,
y
,
z
必定有一维就是
plk
p
l
k
所以这一维显然是多余的
状态就可以压到
fk,x,y
f
k
,
x
,
y
表示完成第
k
k
个服务,三个服务生分别位于
#include<bits/stdc++.h>
using namespace std;
#define rep(i,j,k) for(int i = j;i <= k;++i)
#define repp(i,j,k) for(int i = j;i >= k;--i)
#define rept(i,x) for(int i = linkk[x];i;i = e[i].n)
#define P pair<int,int>
#define Pil pair<int,ll>
#define Pli pair<ll,int>
#define Pll pair<ll,ll>
#define pb push_back
#define pc putchar
#define mp make_pair
#define file(k) memset(k,0,sizeof(k))
#define ll long long
namespace fastIO{
#define BUF_SIZE 100000
#define OUT_SIZE 100000
bool IOerror = 0;
inline char nc(){
static char buf[BUF_SIZE],*p1 = buf+BUF_SIZE, *pend = buf+BUF_SIZE;
if(p1 == pend){
p1 = buf; pend = buf+fread(buf, 1, BUF_SIZE, stdin);
if(pend == p1){ IOerror = 1; return -1;}
}
return *p1++;
}
inline bool blank(char ch){return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';}
inline void read(int &x){
bool sign = 0; char ch = nc(); x = 0;
for(; blank(ch); ch = nc());
if(IOerror)return;
if(ch == '-') sign = 1, ch = nc();
for(; ch >= '0' && ch <= '9'; ch = nc()) x = x*10+ch-'0';
if(sign) x = -x;
}
inline void read(ll &x){
bool sign = 0; char ch = nc(); x = 0;
for(; blank(ch); ch = nc());
if(IOerror) return;
if(ch == '-') sign = 1, ch = nc();
for(; ch >= '0' && ch <= '9'; ch = nc()) x = x*10+ch-'0';
if(sign) x = -x;
}
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace fastIO;
int n , qq;
int c[210][210];
int dp[1010][210][210];
bool inq[1010][210][210];
int pl[1010];
int ans;
struct node{
int id,x,y;
};
queue<node>q;
void push(node a){if(!inq[a.id][a.x][a.y]) q.push(a),inq[a.id][a.x][a.y] = true;}
int main()
{
read(n);read(qq);
rep(i,1,n) rep(j,1,n) read(c[i][j]);
rep(i,1,qq) read(pl[i]);
pl[0] = 3;
memset(dp,10,sizeof(dp));ans = dp[0][0][0];
dp[0][1][2] = 0;
q.push((node){0,1,2});
while(!q.empty())
{
node now = q.front();
int x = now.x,y = now.y,id = now.id;q.pop();
int k = dp[id][x][y];
if(id == qq) {ans = min(ans,k);continue;}
if(pl[id+1] == pl[id]) push((node){id+1,x,y}),
dp[id+1][x][y] = min(dp[id+1][x][y],k);
else if(pl[id+1] == x) push((node){id+1,y,pl[id]}),
dp[id+1][y][pl[id]] = min(dp[id+1][y][pl[id]],k);
else if(pl[id+1] == y) push((node){id+1,x,pl[id]}),
dp[id+1][x][pl[id]] = min(dp[id+1][x][pl[id]],k);
else
{
push((node){id+1,x,y});
dp[id+1][x][y] = min(dp[id+1][x][y],k+c[pl[id]][pl[id+1]]);
push((node){id+1,x,pl[id]});
dp[id+1][x][pl[id]] = min(dp[id+1][x][pl[id]],k+c[y][pl[id+1]]);
push((node){id+1,y,pl[id]});
dp[id+1][y][pl[id]] = min(dp[id+1][y][pl[id]],k+c[x][pl[id+1]]);
}
}
printf("%d\n",ans);
return 0;
}