洛谷P3381
来源:互联网 发布:淘宝卖家怎么设置淘宝客推广 编辑:程序博客网 时间:2024/04/30 04:59
原题链接
题意简述
模板题啦~
题解
每次都以费用作为边权求一下最短路,然后沿着最短路增广。
Code
//【模板】最小费用最大流#include <cstdio>#include <cstring>#include <algorithm>using namespace std;inline char gc(){ static char now[1<<16],*S,*T; if(S==T) {T=(S=now)+fread(now,1,1<<16,stdin); if(S==T) return EOF;} return *S++;}inline int read(){ int x=0,f=1; char ch=gc(); while(ch<'0'||'9'<ch) {if(ch=='-') f=-1; ch=gc();} while('0'<=ch&&ch<='9') x=x*10+ch-'0',ch=gc(); return x*f;}int const N=5e3+10;int const M=5e4+10;int const INF=0x3F3F3F3F;int n,m,s,t;int cnt,h[N];struct edge{int v,c,w,nxt;} ed[M<<1];void edAdd(int u,int v,int c,int w){ ++cnt; ed[cnt].v=v,ed[cnt].c=c,ed[cnt].w=w; ed[cnt].nxt=h[u],h[u]=cnt; ++cnt; ed[cnt].v=u,ed[cnt].c=0,ed[cnt].w=-w; ed[cnt].nxt=h[v],h[v]=cnt;}int dst[N],pre[N]; int q[N],op,cl; bool in[N];bool SPFA(){ op=cl=0; memset(dst,0x3F,sizeof dst); memset(pre,0,sizeof pre); dst[s]=0; in[q[++cl%N]=s]=true; while(op<cl) { int u=q[++op%N]; in[u]=false; for(int i=h[u];i;i=ed[i].nxt) { int v=ed[i].v,w=ed[i].w; if(dst[u]+w<dst[v] && ed[i].c) { pre[v]=i; dst[v]=dst[u]+w; if(!in[v]) in[q[++cl%N]=v]=true; } } } return dst[t]<INF;}int main(){ n=read(),m=read(); s=read(),t=read(); cnt=1; for(int i=1;i<=m;i++) { int u=read(),v=read(),c=read(),w=read(); edAdd(u,v,c,w); } int flow=0,cost=0; while(SPFA()) { int fl=INF,u=t; while(pre[u]) fl=min(fl,ed[pre[u]].c),u=ed[pre[u]^1].v; flow+=fl; cost+=fl*dst[t]; u=t; while(pre[u]) ed[pre[u]].c-=fl,ed[pre[u]^1].c+=fl,u=ed[pre[u]^1].v; } printf("%d %d\n",flow,cost); return 0;}
阅读全文
0 0
- 洛谷P3381
- 洛谷P3381【模板】最小费用最大流
- 网络流费用流入门(洛谷P3381)
- 【模板】最小费用最大流(增广路)(模板题:洛谷P3381)
- 费用流——洛谷P3381 【模板】最小费用最大流
- 洛谷
- 洛谷
- 洛谷
- 洛谷 中位数
- 洛谷p1373
- 洛谷 P1579
- 洛谷P1313
- 洛谷1028
- 洛谷P1373
- 洛谷P1040
- 洛谷P1039
- 洛谷P1341
- 洛谷P1262
- C++primer学习 12.18
- 深度学习环境搭建
- mybatis-xml配置
- putty连不上linux,提示:Network error:Connection refused
- Error running app: Instant Run requires 'Tools | Android | Enable ADB integration' to be enabled.
- 洛谷P3381
- UltraEdit 批量修改多列 和多字符串修改方式
- 快速开通微信门店小程序,使用公众号门店小程序
- 支付宝支付-提现到个人支付宝
- GAN系列学习(2)——前生今世
- java 基本语法
- php cookie和session的代码操作
- 云服务器 ECS Linux 系统安装图形化桌面
- Java算法面试题(007) 蒙提霍尔问题(三门问题)