题目
思路
啊又是一道拆点,想了解大概思想可以看我的这篇博客,另外显然我们能走的边只有
d
i
s
[
x
]
+
e
[
x
]
[
y
]
=
d
i
s
[
y
]
dis[x]+e[x][y]=dis[y]
dis[x]+e[x][y]=dis[y]的边。
code:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<cmath>
#include<queue>
#include<deque>
using namespace std;
long long n,m,s,t,x,y,tot=2,head[2001],dep[2001],l,r,u[2001],r2,dis[501][501];
long long w,ans;
bool book[2001];
long long mn(long long x,long long y)
{
return (x>y?y:x);
}
struct f{
long long to,net;
long long w;
} a[100011];
void add(long long x,long long y,long long w)
{
a[tot].to=y,a[tot].w=w,a[tot].net=head[x],head[x]=tot++;
return;
}
bool bfs()
{
memset(dep,0,sizeof(dep));
l=0,r=0;
u[r++]=s;
dep[s]=1;
while (l<r)
{
r2=r;
for (long long i=l;i<r;i++)
{
for (long long j=head[u[i]];j;j=a[j].net)
{
if (a[j].w!=0&&dep[a[j].to]==0)
{
u[r2++]=a[j].to;
dep[a[j].to]=dep[u[i]]+1;
}
}
}
l=r,r=r2;
}
return dep[t];
}
long long dfs(long long d,long long in)
{
if (d==t) return in;
long long out=0;
long long uw=dep[d]+1;
for (long long j=head[d];j&∈j=a[j].net)
{
if (a[j].w==0||dep[a[j].to]!=uw) continue;
long long s=dfs(a[j].to,mn(in,a[j].w));
out+=s,in-=s;
a[j].w-=s,a[j^1].w+=s;
}
if (out==0)
{
dep[d]=0;
}
return out;
}
void spfa()
{
memset(dep,0x3f,sizeof(dep));
memset(book,0,sizeof(book));
queue<long long> u;
u.push(1);
book[1]=1,dep[1]=0;
while (u.size())
{
y=u.front();
u.pop();
book[y]=0;
for (long long j=1;j<=n;j++)
{
if (dep[y]+dis[y][j]<dep[j])
{
dep[j]=dep[y]+dis[y][j];
if (!book[j]) book[j]=1,u.push(j);
}
}
}
return;
}
int main()
{
memset(dis,0x3f,sizeof(dis));
scanf("%lld%lld",&n,&m);
s=1,t=2*n;
for (long long i=0;i<m;i++)
{
scanf("%lld%lld%lld",&x,&y,&w);
dis[x][y]=dis[y][x]=min(dis[x][y],w);
}
for (long long i=1;i<=n;i++)
{
scanf("%lld",&x);
if (i!=1&&i!=n)
{
add(i,i+n,x);
}
else
{
add(i,i+n,1e20);
}
add(i+n,i,0);
}
spfa();
for (long long i=1;i<=n;i++) for (long long j=1;j<=n;j++)
{
if (dep[i]<=dep[j]&&dep[i]+dis[i][j]==dep[j])
{
add(i+n,j,1e20);
add(j,i+n,0);
}
}
while (bfs())
{
ans+=dfs(s,1e20);
}
cout<<ans;
return 0;
}