题意
有一张n*n的网格图。要从点(1,1)走到点(n,n)。有一些点上有加油站。
一开始汽车有k的油量,每走过一条边油量减少一单位。
如果汽车经过加油站,那么直接支付A的费用并且油量等于K。
汽车可以朝四个方向移动,如果向x坐标或者y坐标减小的方向移动,每次需要支付额外费用B。
可以花C的代价在某个位置建一个加油站。(不包括加油的费用)
求最小花费。
分析
其实就是最短路,考虑把油量放在一起作为某一个点然后跑最短路就行了。
然后就是复习一下重载运算符。
friend bool operator< (node n1, node n2){
return n1.priority < n2.priority;
}
code
#include<bits/stdc++.h>
using namespace std;
void read(int &x){
x=0; char c=getchar();
for (;c<48;c=getchar());
for (;c>47;c=getchar())x=(x<<1)+(x<<3)+(c^48);
}
struct node{
int cost,p,x,y;
friend bool operator< (node n1, node n2){
return n1.cost > n2.cost;
}
};
priority_queue<node>Q;
int n;
int dis[20][105][105],K,A,B,C,a[105][105];
void chk(int x,int y,int p,int cost){
if (x<1||x>n||y<1||y>n)return;
if (a[x][y])cost+=A,p=K;
if (cost>dis[p][x][y])return;
dis[p][x][y]=cost;
Q.push((node){cost,p,x,y});
}
void dijkstra(){
int x,y,p,cost;
memset(dis,63,sizeof(dis));
Q.push((node){0,K,1,1});
for (;;){
x=Q.top().x;
y=Q.top().y;
p=Q.top().p;
cost=Q.top().cost; Q.pop();
if (cost>dis[p][x][y])continue;
dis[p][x][y]=0;
if (x==n&&y==n){printf("%d\n",cost); return;}
if (p){
chk(x-1,y,p-1,cost+B);
chk(x+1,y,p-1,cost);
chk(x,y-1,p-1,cost+B);
chk(x,y+1,p-1,cost);
}
if (!a[x][y]){
p=K; cost+=A+C;
chk(x-1,y,p-1,cost+B);
chk(x+1,y,p-1,cost);
chk(x,y-1,p-1,cost+B);
chk(x,y+1,p-1,cost);
}
}
}
int main(){
read(n); read(K); read(A); read(B); read(C);
for (int i=1;i<=n;i++){
for (int j=1;j<=n;j++){
read(a[i][j]);
}
}
dijkstra();
return 0;
}