这是一道比较经典的最短路,分层图+最短路。。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define re register
#define ls (o<<1)
#define rs (o<<1|1)
//#define m (l+r)/2
#define pb push_back
typedef pair<int,int> pii;
const double PI= acos(-1.0);
const int M = 1e5+7;
/*
int head[M],cnt=1;
void init(int n){cnt=1;for(int i=0;i<=n;i++)head[i]=0;}
struct EDGE{int to,nxt,w;}ee[M*2];
void add(int x,int y,int w){ee[++cnt].nxt=head[x],ee[cnt].w=w,ee[cnt].to=y,head[x]=cnt;}
*/
int n,k,A,B,C;
int a[110][110];
int dp[10100][11];
int vs[10100][11];
struct node{
int x,y,res,cost;
bool operator < (const node &r)const{
return cost>r.cost;
}
};
int id(int x,int y){
return (x-1)*n+y;
}
int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
void dij(){
priority_queue<node>q;
memset(dp,0x3f,sizeof(dp));
q.push(node{1,1,k,0});
dp[id(1,1)][k]=0;
while(q.size()){
node tp=q.top();q.pop();
int x=tp.x,y=tp.y,res=tp.res,cost=tp.cost;
int x2,y2,u=id(x,y);
if(vs[u][res]||res==0)continue;
vs[u][res]=1;
for(int i=0;i<4;i++){
x2=x+dx[i];y2=y+dy[i];
if(x2<1||x2>n||y2<1||y2>n)continue;
int v=id(x2,y2),res2=res,cost2=cost;
if(i>=2)cost2+=B;
if(a[x2][y2])
res2=k,cost2+=A;
else{
res2--;
//下面这个注意,要C+A,增设加油站的C不包括加油的钱
if(dp[v][k]>cost2+C+A){//增设加油点
dp[v][k]=cost2+C+A;
q.push(node{x2,y2,k,cost2+C+A});
}
}
if(dp[v][res2]>cost2){
dp[v][res2]=cost2;
q.push(node{x2,y2,res2,cost2});
}
}
}
int mn=1e9;
for(int i=0;i<=k;i++)mn=min(mn,dp[id(n,n)][i]);
cout<<mn<<endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>k>>A>>B>>C;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++){
cin>>a[i][j];
}
dij();
return 0;
}
然后为了练习费用流,下面用费用流去解:
和分层图最短路的建边基本相同,把分层图建边的变成设成费用,流量均设为1.
然后再设立起点S连向(1,1,k) 流量1,费用0,然后(n,n,0)连向终点T,费用0
上面是dij写法,下面是spfa写法。 感觉不刻意卡spfa的话,spfa真的很快!
#include<bits/stdc++.h>
using namespace std;
const int N = 200001;
const int M = 5000001;
struct MCMF
{
int n, s, t, cnt = 1;
long long maxflow=0, mincost=0;
int dis[N], head[N], incf[N], pre[N];//dis表示最短路,incf表示当前增广路上最小流量,pre表示前驱
bool vs[N];
int h[N];//dij中的势能
struct EDGE {
int nxt, to, flow,cost;
}ee[M << 1];
inline void AD(int x, int y, int flow, int cost) {
ee[++cnt].nxt = head[x];
ee[cnt].to = y;
ee[cnt].cost = cost;
ee[cnt].flow = flow;
head[x] = cnt;
// cout<<x<<" -- "<<head[x]<<endl;
}
inline void add(int x, int y, int flow, int cost){
// cout<<x<<" - > "<<y<<" "<<flow<<" "<<cost<<endl;
AD(x,y,flow,cost);AD(y,x,0,-cost);
//双向边
}
inline bool Dijkstra()
{
priority_queue<pair<int,int> ,vector<pair<int,int> >,greater<pair<int,int> > >q;
for(; !q.empty(); q.pop());
for(int i=1;i<=n;i++)dis[i]=0x3f3f3f3f,vs[i]=0;
dis[s]=0;incf[s] = 1 << 30;
q.push({dis[s],s});
while(!q.empty())
{
pair<int,int> tp=q.top();q.pop();
int u=tp.second ;
if(vs[u])continue;
vs[u]=1;
//cout<<u<<" - - "<<head[u]<<" "<<endl;
for(int i=head[u];i;i=ee[i].nxt)
{
int v=ee[i].to,cost=ee[i].cost,flow=ee[i].flow;
// cout<<"=== "<<u<<" -> "<<v<<endl;
if(!flow)continue;//没有剩余流量
if(dis[v]>dis[u]+cost+h[u]-h[v]){
dis[v]=dis[u]+cost + h[u] - h[v];
q.push({dis[v],v});
incf[v] = min(incf[u], flow);//更新incf
pre[v]=i;//记录路径
}
}
}
if(dis[t] == 0x3f3f3f3f) return 0;
return 1;
}
void gao()
{
while(Dijkstra()) {//如果有增广路
for(int i=1;i<=n;i++)
if(dis[i]!=0x3f3f3f3f)h[i]+=dis[i];
int x = t;
maxflow += incf[t];
mincost += (long long)h[t] * incf[t];
int i;
while(x != s) {//遍历这条增广路,正向边减流反向边加流
// cout<<x<<" - > ";
i = pre[x];
ee[i].flow -= incf[t];
ee[i^1].flow += incf[t];
x = ee[i^1].to;
}
}
}
//先初始化
void init(int nn,int S,int T)
{
n=nn,s=S,t=T;
cnt=1;
for(int i=0;i<=n;i++)head[i]=0,h[i]=0;//势能清零
mincost=maxflow=0;
}
}mc;
int n,K,A,B,C;
int a[110][110];
int id(int x,int y,int k){
return k*n*n+((x-1)*n+y);
}
int main()
{
cin>>n>>K>>A>>B>>C;
int N=n*n*(K+1)+2;
int s=N-1,t=N;mc.init(N,s,t);
for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)cin>>a[i][j];
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++){
for(int k=0;k<=K;k++){
int u=id(i,j,k),v;
mc.add(u,id(i,j,K),1,A+C);
if(k==0)continue;
//只有左上费用是正数,所以不会存在负环!!
if(i>1){
if(a[i-1][j])mc.add(u,id(i-1,j,K),1,B+A);
else mc.add(u,id(i-1,j,k-1),1,B);
}
if(j>1){
if(a[i][j-1])mc.add(u,id(i,j-1,K),1,B+A);
else mc.add(u,id(i,j-1,k-1),1,B);
}
if(i<n){
if(a[i+1][j])mc.add(u,id(i+1,j,K),1,0+A);
else mc.add(u,id(i+1,j,k-1),1,0);
}
if(j<n){
if(a[i][j+1])mc.add(u,id(i,j+1,K),1,0+A);
else mc.add(u,id(i,j+1,k-1),1,0);
}
}
}
mc.add(s,id(1,1,K),1,0);
mc.add(id(n,n,0),t,1,0);
for(int i=K;i>=1;i--)mc.add(id(n,n,i),id(n,n,0),1,0);
mc.gao();//最小费用最大流
printf("%lld\n",mc.mincost);
return 0;
}
/*
2 1 1 2 3
0 1
1 0
*/
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 200000+7;
const int MAXM = 5000000+7;
struct MCMF
{
int n, s, t, cnt = 1;
long long maxflow=0, mincost=0;
int dis[MAXN], head[MAXN], incf[MAXN], pre[MAXN];//dis表示最短路,incf表示当前增广路上最小流量,pre表示前驱
bool vis[MAXN];
struct EDGE {
int next, to, dis, flow;
}ee[MAXM << 1];
inline void AD(int from, int to, int flow, int dis) {
ee[++cnt].next = head[from];
ee[cnt].to = to;
ee[cnt].dis = dis;
ee[cnt].flow = flow;
head[from] = cnt;
}
inline void add(int x, int y, int flow, int dis){
AD(x,y,flow,dis);AD(y,x,0,-dis);
//双向边
}
inline bool spfa()
{
queue <int> q;
for(int i=1;i<=n;i++)dis[i]=0x3f3f3f3f,vis[i]=0;
q.push(s);
dis[s] = 0;vis[s] = 1;
incf[s] = 1 << 30;
while(!q.empty()) {
int u = q.front();
vis[u] = 0;
q.pop();
for(register int i = head[u]; i; i = ee[i].next) {
if(!ee[i].flow) continue;//没有剩余流量
int v = ee[i].to;
if(dis[v] > dis[u] + ee[i].dis) {
dis[v] = dis[u] + ee[i].dis;
incf[v] = min(incf[u], ee[i].flow);//更新incf
pre[v] = i;
if(!vis[v]) vis[v] = 1, q.push(v);
}
}
}
if(dis[t] == 0x3f3f3f3f) return 0;
return 1;
}
void gao()
{
while(spfa()) {//如果有增广路
int x = t;
maxflow += incf[t];
mincost += (long long)dis[t] * incf[t];
int i;
while(x != s) {//遍历这条增广路,正向边减流反向边加流
i = pre[x];
ee[i].flow -= incf[t];
ee[i^1].flow += incf[t];
x = ee[i^1].to;
}
}
}
//先初始化
void init(int nn,int S,int T)
{
n=nn,s=S,t=T;
cnt=1;
for(int i=0;i<=n;i++)head[i]=0;
mincost=maxflow=0;
}
}mc;
int n,K,A,B,C;
int a[110][110];
int id(int x,int y,int k){
return k*n*n+((x-1)*n+y);
}
int main()
{
cin>>n>>K>>A>>B>>C;
int N=n*n*(K+1)+2;
int s=N-1,t=N;mc.init(N,s,t);
for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)cin>>a[i][j];
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++){
for(int k=0;k<=K;k++){
int u=id(i,j,k),v;
mc.add(u,id(i,j,K),1,A+C);
if(k==0)continue;
//只有左上费用是正数,所以不会存在负环!!
if(i>1){
if(a[i-1][j])mc.add(u,id(i-1,j,K),1,B+A);
else mc.add(u,id(i-1,j,k-1),1,B);
}
if(j>1){
if(a[i][j-1])mc.add(u,id(i,j-1,K),1,B+A);
else mc.add(u,id(i,j-1,k-1),1,B);
}
if(i<n){
if(a[i+1][j])mc.add(u,id(i+1,j,K),1,0+A);
else mc.add(u,id(i+1,j,k-1),1,0);
}
if(j<n){
if(a[i][j+1])mc.add(u,id(i,j+1,K),1,0+A);
else mc.add(u,id(i,j+1,k-1),1,0);
}
}
}
mc.add(s,id(1,1,K),1,0);
mc.add(id(n,n,0),t,1,0);
for(int i=K;i>=1;i--)mc.add(id(n,n,i),id(n,n,0),1,0);
mc.gao();//最小费用最大流
printf("%lld\n",mc.mincost);
return 0;
}