最小费用最大流。
建图,把从第i个目的点到到第i+1个目的点视为一个点,作为二分图的X部,源点连流量1费用0的边。把第i辆车视为Y部,向汇点连流量1费用0的边,表示用第i辆车。Y部外加一个点,表示走路,向汇点连接流量无穷,费用0的边。X,Y部之间建边,如果从第i个目的点到i+1个目的点用第j辆车,就建立费用为从i走到j加上j到i+1开车的费用即可。连走路的点直接是走过去的费用。跑最小费用最大流就是解。
#include <bits/stdc++.h>
#define maxn 509
#define maxm 2000009
using namespace std;
const int INF=1e9;
struct Edge
{
int v,cap,cost,next;
}edge[maxm];
int head[maxn],tot,n,src,des;
int d[maxn],q[maxn],inq[maxn],pre[maxn],dis[maxn][maxn];
int flow,cost;
inline void addedge(int u,int v,int cap,int cost)
{
edge[tot].v=v;
edge[tot].cap=cap;
edge[tot].cost=cost;
edge[tot].next=head[u];
head[u]=tot++;
edge[tot].v=u;
edge[tot].cap=0;
edge[tot].cost=-cost;
edge[tot].next=head[v];
head[v]=tot++;
}
void init()
{
memset(head,-1,sizeof(head));
tot=flow=cost=0;
}
bool spfa()
{
int h=0,t=0;
for(int i=0;i<=n;i++)
{
d[i]=INF;
}
d[src]=0;
q[t++]=src;
while(h!=t)
{
int u=q[h++];
h%=maxn;
inq[u]=0;
for(int e=head[u];e!=-1;e=edge[e].next)
{
int v=edge[e].v;
if(edge[e].cap&&d[v]>d[u]+edge[e].cost)
{
d[v]=d[u]+edge[e].cost;
pre[v]=e;
if(!inq[v])
{
inq[v]=1;
q[t++]=v;
t%=maxn;
}
}
}
}
return d[des]!=INF;
}
void end()
{
int u,e,mi=INF;
for(u=des;u!=src;u=edge[e^1].v)
{
e=pre[u];
mi=min(mi,edge[e].cap);
}
for(u=des;u!=src;u=edge[e^1].v)
{
e=pre[u];
edge[e].cap-=mi;
edge[e^1].cap+=mi;
cost+=mi*edge[e].cost;
}
flow+=mi;
}
class SlimeXGrandSlimeAuto
{
public:
int travel(vector <int> cars, vector <int> a, vector <string> roads, int walk, int drive)
{
int num=roads.size();
init();
for(int i=0;i<num;i++)
{
for(int j=0;j<num;j++)
{
if(i==j)
dis[i][j]=0;
else
{
if(roads[i][j]>='0'&&roads[i][j]<='9')
dis[i][j]=roads[i][j]-'0'+1;
else if(roads[i][j]>='a'&&roads[i][j]<='z')
dis[i][j]=roads[i][j]-'a'+11;
else if(roads[i][j]>='A'&&roads[i][j]<='Z')
dis[i][j]=roads[i][j]-'A'+37;
else
dis[i][j]=INF;
}
}
}
for(int k=0;k<num;k++)
for(int i=0;i<num;i++)
for(int j=0;j<num;j++)
dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
int N=a.size(),M=cars.size()+1;
src=0;des=N+M+1;n=des+1;
for(int i=1;i<=N;i++)
{
addedge(src,i,1,0);
}
for(int i=1;i<=M;i++)
{
if(i==M)
addedge(i+N,des,INF,0);
else
addedge(i+N,des,1,0);
}
for(int i=1;i<=N;i++)
{
for(int j=1;j<=M;j++)
{
if(j==M)
{
if(i==1)
{
addedge(i,N+j,1,dis[0][a[i-1]]*walk);
}
else
{
addedge(i,N+j,1,dis[a[i-2]][a[i-1]]*walk);
}
}
else
{
if(i==1)
{
addedge(i,N+j,1,dis[0][cars[j-1]]*walk+dis[cars[j-1]][a[i-1]]*drive);
}
else
{
addedge(i,N+j,1,dis[a[i-2]][cars[j-1]]*walk+dis[cars[j-1]][a[i-1]]*drive);
}
}
}
}
while(spfa())end();
//cout<<flow<<endl;
return cost;
}
};