题链:
http://www.lydsy.com/JudgeOnline/problem.php?id=3931
题解:
在最短路图上跑网络流,要开long long
(无奈 BZOJ AC 不了,洛谷上 wa 了一个点。改不出来了诶)
代码:
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#define INF 0x3f3f3f3f
#define ll long long
using namespace std;
struct Edge{
ll to[500000],cap[500000],nxt[500000],head[2000],ent;
void Init(){ent=2;}
void Adde(ll u,ll v,ll w){
to[ent]=v; cap[ent]=w; nxt[ent]=head[u]; head[u]=ent++;
}
ll Next(ll i,bool type){
return type?head[i]:nxt[i];
}
}E1,E2;
ll dis[2000],d[2000],cur[2000];
ll N,M,S,T;
ll idx(ll u,ll k){
return u+N*k;
}
struct cmp{
bool operator ()(ll a,ll b){
return dis[a]>dis[b];
}
};
void Dijkstra(){
static bool vis[2000];
priority_queue<ll,vector<ll>,cmp>q;
memset(dis,0x3f,sizeof(dis));
dis[1]=0; q.push(1); ll u,v,w;
while(!q.empty()){
u=q.top(); q.pop();
if(vis[u]) continue; vis[u]=1;
for(ll i=E1.Next(u,1);i;i=E1.Next(i,0)){
v=E1.to[i],w=E1.cap[i];
if(dis[v]<=dis[u]+w) continue;
dis[v]=dis[u]+w;
q.push(v);
}
}
}
bool bfs(){
queue<ll> q;
memset(d,0,sizeof(d));
d[S]=1; q.push(S); ll u,v;
while(!q.empty()){
u=q.front(); q.pop();
for(ll i=E2.Next(u,1);i;i=E2.Next(i,0)){
v=E2.to[i];
if(d[v]||!E2.cap[i]) continue;
d[v]=d[u]+1; q.push(v);
}
}
return d[T];
}
ll dfs(ll u,ll reflow){
if(u==T||!reflow) return reflow;
ll flowout=0,f,v;
for(ll &i=cur[u];i;i=E2.Next(i,0)){
v=E2.to[i];
if(d[v]!=d[u]+1) continue;
f=dfs(v,min(reflow,E2.cap[i]));
flowout+=f; E2.cap[i^1]+=f;
reflow-=f; E2.cap[i]-=f;
if(!reflow) break;
}
if(!flowout) d[u]=0;
return flowout;
}
ll Dinic(){
ll flow=0;
while(bfs()){
memcpy(cur,E2.head,sizeof(E2.head));
flow+=dfs(S,INF);
}
return flow;
}
int main()
{
E1.Init(); E2.Init();
scanf("%lld%lld",&N,&M);S=idx(1,1); T=idx(N,0);
for(ll i=1,u,v,w;i<=M;i++){
scanf("%lld%lld%lld",&u,&v,&w);
E1.Adde(u,v,w); E1.Adde(v,u,w);
}
Dijkstra();
for(ll u=1;u<=N;u++)
for(ll i=E1.Next(u,1);i;i=E1.Next(i,0)){
ll v=E1.to[i],w=E1.cap[i];
if(dis[u]+w!=dis[v]) continue;
E2.Adde(idx(u,1),idx(v,0),INF);
E2.Adde(idx(v,0),idx(u,1),0);
}
for(ll i=1,x;i<=N;i++){
scanf("%lld",&x);
E2.Adde(idx(i,0),idx(i,1),x);
E2.Adde(idx(i,1),idx(i,0),0);
}
ll ans=Dinic();
printf("%lld",ans);
return 0;
}