BZOJ3931 [CQOI2015]网络吞吐量(洛谷P3171)

最短路 最大流

BZOJ题目传送门
洛谷题目传送门

刷一遍最短路,只留下那些最短路的边,然后拆点跑最大流就好了。

代码:

#include
#include
#include
#include
#include
#define N 1005
#define M 100005
#define F inline
using namespace std;
typedef long long LL;
const LL inf=1e18;
struct edg{ int nxt,to; LL d; }e[M<<1];
struct edge{ int nxt,to; LL v,f; }ed[M<<1];
struct P{ int x; LL d; };
int n,m,k,s,t,ti,h[N],h1[N],tmp[N],f[N];
LL d[N],ans;
priority_queue 

q1; queue <int> q; F bool operator < (P a,P b){ return a.d>b.d; } F char readc(){ static char buf[100000],*l=buf,*r=buf; if (l==r) r=(l=buf)+fread(buf,1,100000,stdin); return l==r?EOF:*l++; } F int _read(){ int x=0; char ch=readc(); while (!isdigit(ch)) ch=readc(); while (isdigit(ch)) x=(x<<3)+(x<<1)+(ch^48),ch=readc(); return x; } #define add(x,y,z) e[++k]=(edg){h1[x],y,z},h1[x]=k F void addedge(int x,int y,LL z){ ed[++k]=(edge){h[x],y,z},h[x]=k; ed[++k]=(edge){h[y],x,0},h[y]=k; } F void Dij(){ for (int i=1;i<=n;i++) d[i]=inf; for (q1.push((P){n,d[n]=0});!q1.empty();q1.pop()){ if (q1.top().d>d[q1.top().x]) continue; int x; for (int i=h1[x=q1.top().x],v;i;i=e[i].nxt) if (d[x]+e[i].dfor (int x=1;x<=n;x++) for (int i=h1[x],v;i;i=e[i].nxt) if (e[i].d+d[x]==d[v=e[i].to]) addedge(v+n,x,inf); } F bool bfs(){ while (!q.empty()) q.pop(); d[s]=0,f[s]=++ti,q.push(s); while (!q.empty()){ int x=q.front(); q.pop(); for (int i=h[x],v;i;i=ed[i].nxt) if (ed[i].v>ed[i].f&&f[v=ed[i].to]!=ti) d[v]=d[x]+1,f[v]=ti,q.push(v); } return f[t]==ti; } LL dfs(int x,LL rem){ if (x==t||!rem) return rem; LL sum=0; for (int &i=tmp[x];i;i=ed[i].nxt) if (d[ed[i].to]==d[x]+1){ LL p=dfs(ed[i].to,min(rem,ed[i].v-ed[i].f)); if (p) sum+=p,ed[i].f+=p,ed[i^1].f-=p,rem-=p; if (!rem) break; } return sum; } int main(){ n=_read(),m=_read(),s=1,t=n<<1; for (int i=1,x,y,z;i<=m;i++) x=_read(),y=_read(),z=_read(),add(x,y,z),add(y,x,z); k=1,Dij(); for (int i=1,x;i<=n;i++) x=_read(),addedge(i,i+n,(i!=1&&i!=n)?x:inf); while (bfs()) memcpy(tmp,h,sizeof(h)),ans+=dfs(s,inf); return printf("%lld\n",ans),0; }

你可能感兴趣的:(洛谷,BZOJ,图论---最短路,图论---网络流最大流,蒟蒻zxl的Blog专栏)