博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CFGYM 2013-2014 CT S01E03 D题 费用流模版题
阅读量:6894 次
发布时间:2019-06-27

本文共 1977 字,大约阅读时间需要 6 分钟。

题意:

n行, a房间的气球,b房间的气球

i行需要的气球,与a房的距离,b房的距离

求最小距离

 

#include 
#include
#include
#include
#include
#include
#include
#include
#define N 2000#define M 10100#define inf 107374182#define ll intusing namespace std;inline ll Min(ll a,ll b){return a>b?b:a;}inline ll Max(ll a,ll b){return a>b?a:b;}int n;//双向边,注意RE 注意这个模版是 相同起末点的边 合并而不是去重struct Edge{ int from, to, flow, cap, nex, cost;}edge[M*2];int head[N], edgenum;//2个要初始化-1和0void addedge(int u,int v,int cap,int cost){//网络流要加反向弧 Edge E={u, v, 0, cap, head[u], cost}; edge[edgenum]=E; head[u]=edgenum++; Edge E2={v, u, 0, 0, head[v], -cost}; //这里的cap若是单向边要为0 edge[edgenum]=E2; head[v]=edgenum++;}int D[N], P[N], A[N];bool inq[N];bool BellmanFord(int s, int t, int &flow, int &cost){ for(int i=0;i<=n+4;i++) D[i]= inf; memset(inq, 0, sizeof(inq)); D[s]=0; inq[s]=1; P[s]=0; A[s]=inf; queue
Q; Q.push( s ); while( !Q.empty()){ int u = Q.front(); Q.pop(); inq[u]=0; for(int i=head[u]; i!=-1; i=edge[i].nex){ Edge &E = edge[i]; if(E.cap > E.flow && D[E.to] > D[u] +E.cost){ D[E.to] = D[u] + E.cost ; P[E.to] = i; A[E.to] = Min(A[u], E.cap - E.flow); if(!inq[E.to]) Q.push(E.to) , inq[E.to] = 1; } } } if(D[t] == inf) return false; flow += A[t]; cost += D[t] * A[t]; int u = t; while(u != s){ edge[P[u]].flow += A[t]; edge[P[u]^1].flow -= A[t]; u = edge[P[u]].from; } return true;}int Mincost(int s,int t){ int flow = 0, cost = 0; while(BellmanFord(0, n+3, flow, cost)); return cost;}int main(){ int i,disa,disb,flow,maxa,maxb; while( scanf("%d %d %d",&n, &maxa, &maxb), n+maxb+maxa){ memset(head,-1,sizeof(head)); edgenum=0; addedge(n+1, n+3, maxa, 0); addedge(n+2, n+3, maxb, 0); for(i=1;i<=n;i++){ scanf("%d %d %d",&flow,&disa,&disb); addedge(0, i, flow, 0); addedge(i, n+1, flow, disa); addedge(i, n+2, flow, disb); } printf("%d\n",Mincost(0,n+3) ); } return 0;}/*3 15 3510 20 1010 10 3010 40 105 5 01 10 10001 10 10001 10 10000 10 10001 10 10000 0 0ans:30040*/

转载地址:http://gukdl.baihongyu.com/

你可能感兴趣的文章
上下文属性监听
查看>>
【小白的CFD之旅】10 敲门实例
查看>>
POI文件导出至EXCEL,并弹出下载框
查看>>
iOS 使用正则表达式库RegexKitLite的问题
查看>>
C#使用MemoryStream类读写内存
查看>>
MySQL内存使用-线程独享
查看>>
JDBC连接MySQL数据库及演示样例
查看>>
【WP8.1开发】基于应用的联系人存储
查看>>
AI新时代-教你使用python+Opencv完成人脸解锁(附源码)
查看>>
MongoDB ( 三 )高级_状态返回和安全
查看>>
基于 Netty 的可插拔业务通信协议的实现「1」协议描述及基本消息对象设计
查看>>
NodeJS介绍以及开发微信公众号Example
查看>>
新时代前端的自我修养—2017 D2主题分享记录及我的思考
查看>>
java并发编程学习14--CompletableFuture(一)
查看>>
ES6语法之Symbol
查看>>
取周期性字符串中的其中一个
查看>>
d3.js ----面积图表
查看>>
Zepto这样操作元素属性
查看>>
30-seconds-code——Object
查看>>
pyspark底层浅析
查看>>