t开成n结果cur赋值的时候也只赋值到t令人智熄
【题目分析】
好吧我承认这个错误真的呵呵。。。。。。。。
题目有那~~~~~么长,然后画画图这道题就基本看出正解了,再一看数据范围,n<=500简直良心,好了,网络流没得跑了。
因为按最短路进行传递,所以网络流的建图肯定是在最短路的基础上,所以先进行一次SPFA。
考虑一条路如果加入网络流的图,那么这条路一定是在最短路上,dfs一次即可。
然后考虑拆点限制流量(一开始sb的写成了边权结果还跑过了样例然后一交立马咕咕),最后跑最大流即可。
PS:此题还有一个坑点,就是INF要设的很大很大,否则咕咕。
【代码~】
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MAXN=1e3+10;
const LL MAXM=4e5+10;
const LL INF=0x3f3f3f3f3f;
LL n,m,cnt,s,t;
LL head[MAXN],cur[MAXN],depth[MAXN],val[MAXN];
LL nxt[MAXM],to[MAXM],w[MAXM];
LL cnt1,head1[MAXN];
LL nxt1[MAXM],to1[MAXM],w1[MAXM];
LL dis[MAXN],vis[MAXN];
LL Read()
{
LL i=0,f=1;
char c;
for(c=getchar();(c>'9'||c<'0')&&c!='-';c=getchar());
if(c=='-')
f=-1,c=getchar();
for(;c>='0'&&c<='9';c=getchar())
i=(i<<3)+(i<<1)+c-'0';
return i*f;
}
void sc(LL x)
{
if(x>=10)
sc(x/10);
putchar(x%10+48);
}
void Add1(LL x,LL y,LL z)
{
cnt1++;
nxt1[cnt1]=head1[x];
head1[x]=cnt1;
to1[cnt1]=y;
w1[cnt1]=z;
}
void add1(LL x,LL y,LL z)
{
Add1(x,y,z);
Add1(y,x,z);
}
void SPFA()
{
queue<LL> q;
memset(dis,0x3f3f3f3f,sizeof(dis));
dis[s]=0;
q.push(s);
while(!q.empty())
{
LL u=q.front();
q.pop();
vis[u]=0;
for(LL i=head1[u];i!=-1;i=nxt1[i])
{
LL v=to1[i];
if(dis[v]>dis[u]+w1[i])
{
dis[v]=dis[u]+w1[i];
if(!vis[v])
{
vis[v]=1;
q.push(v);
}
}
}
}
}
void Add(LL x,LL y,LL z)
{
nxt[cnt]=head[x];
head[x]=cnt;
to[cnt]=y;
w[cnt]=z;
cnt++;
}
void add(LL x,LL y,LL z)
{
Add(x,y,z);
Add(y,x,0);
}
bool bfs()
{
queue<LL> q;
memset(depth,0,sizeof(depth));
depth[s]=1;
q.push(s);
while(!q.empty())
{
LL u=q.front();
q.pop();
for(LL i=head[u];i!=-1;i=nxt[i])
{
LL v=to[i];
if(!depth[v]&&w[i])
{
depth[v]=depth[u]+1;
q.push(v);
}
}
}
if(depth[t]==0)
return false;
return true;
}
LL dfs(LL u,LL dist)
{
if(u==t)
return dist;
for(LL &i=cur[u];i!=-1;i=nxt[i])
{
LL v=to[i];
if(depth[v]==depth[u]+1&&w[i])
{
LL di=dfs(v,min(dist,w[i]));
if(di>0)
{
w[i]-=di;
w[i^1]+=di;
return di;
}
}
}
return 0;
}
LL dinic()
{
LL ans=0;
while(bfs())
{
for(LL i=s;i<=t+n;++i)
cur[i]=head[i];
while(LL d=dfs(s,INF))
ans+=d;
}
return ans;
}
void buildgraph(LL u,LL fa)
{
for(LL i=head1[u];i!=-1;i=nxt1[i])
{
LL v=to1[i];
if(v==fa)
continue;
if(dis[v]==dis[u]+w1[i])
{
add(u+n,v,INF);
buildgraph(v,u);
}
}
}
int main()
{
memset(head1,-1,sizeof(head1));
memset(head,-1,sizeof(head));
n=Read(),m=Read();
s=1,t=n;
for(LL i=1;i<=m;++i)
{
LL x=Read(),y=Read(),z=Read();
add1(x,y,z);
}
for(LL i=1;i<=n;++i)
val[i]=Read();
SPFA();
buildgraph(1,-1);
add(1,n+1,INF);
for(LL i=2;i<n;++i)
add(i,i+n,val[i]);
sc(dinic());
return 0;
}