[CQOI2015]网络吞吐量
这题好傻啊
就是按照题目说的先跑一遍最短路,之后建出一张最短路\(DAG\)在上面拆点最大流就没了
代码
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define LL long long
#define re register
#define int long long
#define inf 99999999999
#define maxn 1005
inline int read() {
char c=getchar();int x=0;while(c<'0'||c>'9') c=getchar();
while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+c-48,c=getchar();return x;
}
struct E{int v,nxt,f;}e[500005];
int n,num=1,m,S,T,ans;
int head[maxn],cur[maxn],d[maxn],a[maxn];
inline void add(int x,int y,int z) {e[++num].v=y;e[num].nxt=head[x];head[x]=num;e[num].f=z;}
inline void C(int x,int y,int z) {add(x,y,z),add(y,x,0);}
inline int BFS() {
std::queue<int> q;
for(re int i=S;i<=T;i++) cur[i]=head[i],d[i]=0;
d[S]=1,q.push(S);
while(!q.empty()) {
int k=q.front();q.pop();
for(re int i=head[k];i;i=e[i].nxt)
if(e[i].f&&!d[e[i].v]) d[e[i].v]=d[k]+1,q.push(e[i].v);
}
return d[T];
}
int dfs(int x,int now) {
if(x==T||!now) return now;
int flow=0,ff;
for(re int& i=cur[x];i;i=e[i].nxt)
if(e[i].f&&d[e[i].v]==d[x]+1) {
ff=dfs(e[i].v,min(e[i].f,now));
if(ff<=0) continue;
e[i].f-=ff,e[i^1].f+=ff,now-=ff,flow+=ff;
if(!now) break;
}
return flow;
}
namespace Dijkstra {
struct E{int v,nxt,w;}e[300005];
int head[maxn],f[maxn];LL d[maxn];
int num;
#define mp std::make_pair
typedef std::pair<int,LL> pii;
std::priority_queue<pii,std::vector<pii>,std::greater<pii> > q;
inline void add(int x,int y,int z) {e[++num].v=y;e[num].nxt=head[x];head[x]=num;e[num].w=z;}
inline void init() {
n=read(),m=read();int x,y,z;
for(re int i=1;i<=m;i++) x=read(),y=read(),z=read(),add(x,y,z),add(y,x,z);
for(re int i=1;i<=n;i++) d[i]=inf,f[i]=0;
d[1]=0,q.push(mp(0,1));
while(!q.empty()) {
int k=q.top().second;q.pop();
if(f[k]) continue;f[k]=1;
for(re int i=head[k];i;i=e[i].nxt)
if(d[e[i].v]>d[k]+e[i].w) d[e[i].v]=d[k]+e[i].w,q.push(mp(d[e[i].v],e[i].v));
}
for(re int i=1;i<=n;i++)
for(re int j=head[i];j;j=e[j].nxt)
if(d[i]+e[j].w==d[e[j].v]) C(i+n,e[j].v,inf);
}
}
signed main() {
Dijkstra::init();
for(re int i=1;i<=n;i++) a[i]=read();
for(re int i=2;i<n;i++) C(i,i+n,a[i]);C(1,1+n,inf),C(n,n+n,inf);
S=1,T=n+n;
while(BFS()) ans+=dfs(S,inf);
printf("%lld\n",ans);
return 0;
}