题目描述
题解
求出最短路径图之后拆点跑最大流。
判断一条边在最短路图上:dis[edge[i].x]+redis[edge[i].y]+edge[i].w==dis[n]或dis[edge[i].y]+redis[edge[i].x]+edge[i].w==dis[n],其中dis和redis都是单源最短路,源分别为1和n
代码
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
#define LL long long
const int max_n=505;
const int max_N=max_n*2;
const int max_m=1e5+5;
const int max_e=max_m*4;
const LL INF=1e18;
int n,m,N;LL val[max_n],dis[max_n],redis[max_n]; bool vis[max_n];
int tot,point[max_N],nxt[max_e],v[max_e];LL remain[max_e],c[max_e];
int deep[max_N],cur[max_N],last[max_N],num[max_N];
struct hp{int x,y;LL w;}edge[max_m];
queue <int> q;
LL maxflow;
inline void add(int x,int y,LL z)
{
++tot; nxt[tot]=point[x]; point[x]=tot; v[tot]=y; c[tot]=z;
++tot; nxt[tot]=point[y]; point[y]=tot; v[tot]=x; c[tot]=z;
}
inline void spfa_1(int s,int t)
{
for (int i=1;i<=n;++i) dis[i]=INF; dis[s]=0;
memset(vis,0,sizeof(vis)); vis[s]=true;
while (!q.empty()) q.pop(); q.push(s);
while (!q.empty())
{
int now=q.front(); q.pop();
vis[now]=false;
for (int i=point[now];i;i=nxt[i])
if (dis[v[i]]>dis[now]+c[i])
{
dis[v[i]]=dis[now]+c[i];
if (!vis[v[i]])
{
vis[v[i]]=true;
q.push(v[i]);
}
}
}
}
inline void spfa_2(int s,int t)
{
for (int i=1;i<=n;++i) redis[i]=INF; redis[s]=0;
memset(vis,0,sizeof(vis)); vis[s]=true;
while (!q.empty()) q.pop(); q.push(s);
while (!q.empty())
{
int now=q.front(); q.pop();
vis[now]=false;
for (int i=point[now];i;i=nxt[i])
if (redis[v[i]]>redis[now]+c[i])
{
redis[v[i]]=redis[now]+c[i];
if (!vis[v[i]])
{
vis[v[i]]=true;
q.push(v[i]);
}
}
}
}
inline void addedge(int x,int y,LL cap)
{
++tot; nxt[tot]=point[x]; point[x]=tot; v[tot]=y; remain[tot]=cap;
++tot; nxt[tot]=point[y]; point[y]=tot; v[tot]=x; remain[tot]=0;
}
inline void bfs(int t)
{
for (int i=1;i<=N;++i) deep[i]=N; deep[t]=0;
while (!q.empty()) q.pop(); q.push(t);
while (!q.empty())
{
int now=q.front(); q.pop();
for (int i=point[now];i!=-1;i=nxt[i])
if (deep[v[i]]==N&&remain[i^1])
{
deep[v[i]]=deep[now]+1;
q.push(v[i]);
}
}
}
inline LL addflow(int s,int t)
{
int now=t; LL ans=INF;
while (now!=s)
{
ans=min(ans,remain[last[now]]);
now=v[last[now]^1];
}
now=t;
while (now!=s)
{
remain[last[now]]-=ans;
remain[last[now]^1]+=ans;
now=v[last[now]^1];
}
return ans;
}
inline void isap(int s,int t)
{
bfs(t);
for (int i=1;i<=N;++i) ++num[deep[i]];
for (int i=1;i<=N;++i) cur[i]=point[i];
int now=s;
while (deep[s]<N)
{
if (now==t)
{
maxflow+=addflow(s,t);
now=s;
}
bool has_find=false;
for (int i=cur[now];i!=-1;i=nxt[i])
{
if (deep[v[i]]+1==deep[now]&&remain[i])
{
has_find=true;
cur[now]=i;
last[v[i]]=i;
now=v[i];
break;
}
}
if (!has_find)
{
int minn=N-1;
for (int i=point[now];i!=-1;i=nxt[i])
if (remain[i])
minn=min(minn,deep[v[i]]);
if (!(--num[deep[now]])) break;
num[deep[now]=minn+1]++;
cur[now]=point[now];
if (now!=s)
now=v[last[now]^1];
}
}
}
int main()
{
scanf("%d%d",&n,&m);
for (int i=1;i<=m;++i)
{
scanf("%d%d%lld",&edge[i].x,&edge[i].y,&edge[i].w);
add(edge[i].x,edge[i].y,edge[i].w);
}
for (int i=1;i<=n;++i) scanf("%lld",&val[i]);
spfa_1(1,n);
spfa_2(n,1);
tot=-1;
memset(point,-1,sizeof(point));
memset(nxt,-1,sizeof(nxt));
for (int i=1;i<=m;++i)
{
if (dis[edge[i].x]+redis[edge[i].y]+edge[i].w==dis[n])
addedge(edge[i].x+n,edge[i].y,INF);
else if (dis[edge[i].y]+redis[edge[i].x]+edge[i].w==dis[n])
addedge(edge[i].y+n,edge[i].x,INF);
}
addedge(1,1+n,INF);
addedge(n,n+n,INF);
for (int i=2;i<n;++i)
addedge(i,i+n,val[i]);
N=n*2;
isap(1,N);
printf("%lld\n",maxflow);
}
时间: 2024-11-06 12:36:31