Luogu 3381(最小费用流)
来源:互联网 发布:美股行情 交易软件 编辑:程序博客网 时间:2024/06/05 02:44
传送门
模板题。最小费用最大流,要求同时输出最大流以及最小费用,复杂度有点玄学,每增广一次,下一次SPFA的复杂度又会降低。所以这种题暂时只能这么处理:看出来有后效性的相互约束关系的就考虑用费用流解决。
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<queue>using namespace std;const int N=5004,M=50004,INF=0x3f3f3f3f;int n,m,source,sink;int head[N],etot=0,dis[N],pre[N];bool inq[N];struct EDGE {int u,v,nxt,c,r;}e[M<<1];inline int read() {int x=0;char c=getchar();while (c<'0'||c>'9') c=getchar();while (c>='0'&&c<='9') x=(x<<3)+(x<<1)+c-'0',c=getchar();return x;}inline void adde(int u,int v,int r,int c) {e[etot].nxt=head[u],e[etot].u=u,e[etot].v=v,e[etot].r=r,e[etot].c=c,head[u]=etot++;e[etot].nxt=head[v],e[etot].u=v,e[etot].v=u,e[etot].r=0,e[etot].c=-c,head[v]=etot++;}inline bool SPFA() {queue<int > q;memset(dis,INF,sizeof(dis));memset(inq,false,sizeof(inq));pre[source]=-1,dis[source]=0,inq[source]=true;q.push(source);while (!q.empty()) {int p=q.front();q.pop();inq[p]=false;for (int i=head[p];~i;i=e[i].nxt) {int v=e[i].v;if (e[i].r>0&&dis[v]>dis[p]+e[i].c) {dis[v]=dis[p]+e[i].c;pre[v]=i;if (!inq[v]) q.push(v),inq[v]=true;}}}return dis[sink]^INF;}inline void MCMF() {int maxflow=0,mincost=0;while (SPFA()) {int temp=INF;for (int i=pre[sink];~i;i=pre[e[i].u]) temp=min(temp,e[i].r);maxflow+=temp,mincost+=temp*dis[sink];for (int i=pre[sink];~i;i=pre[e[i].u]) e[i].r-=temp,e[i^1].r+=temp;}printf("%d %d\n",maxflow,mincost);}int main() {//freopen("P3381.in","r",stdin);memset(head,-1,sizeof(head));n=read(),m=read(),source=read(),sink=read();for (register int i=0;i<m;++i) {int u=read(),v=read(),r=read(),c=read();adde(u,v,r,c);}MCMF();return 0;}
阅读全文
0 0
- Luogu 3381(最小费用流)
- 最小(大)费用流
- HDU3395(最小费用流)
- hdu4411(最小费用流)
- hdu3526(最小费用流)
- poj3422(*最小费用流)
- hdu4411(#最小费用流)
- poj3680(*最小费用流)
- hdu4780(*最小费用流)
- hdu4322(*最小费用流)
- 最小费用最大流/费用流zkw算法模板(洛谷3381)
- 费用流模板 (最大最小费用流)
- 【洛谷】3381 【模板】最小费用最大流
- 最小费用最小流uva10746
- poj 2159(最小费用最大流)
- POJ 2516(最小费用最大流)
- Transportation 最小费用流 hdu3667 (拆边)
- poj 2195 (最小费用最大流)
- ListView改为recycleView之后的问题
- kylin Segments overlap问题
- es6学习过程(一)
- Python高级编程-如何对字符串进行左,右,居中对齐?
- 将数组{0,1,2,3,4,5}用FileOutputStream流输出到F盘dest.txt文件中
- Luogu 3381(最小费用流)
- Wireshark抓取VIP视频(博客学习)
- springboot控制层生成图片验证码
- 浮点数精度控制方式总结(含mysql和java)
- c#泛型集合
- HDU 2553 N皇后问题(经典的回溯问题)
- javaBean验证框架(7)—使用@valid实现对象关联验证
- java 自定义异常
- 免费的https申请以及教程