日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

最小费用最大流模版

發布時間:2023/11/30 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 最小费用最大流模版 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#include <iostream> #include <cstring> #include <cstdio> #include <queue> #include <algorithm>using namespace std;const int MAXN=10100; const int MAXM=40010; const int INF=0x3f3f3f3f;struct Edge      //cost代表單位流量流過該邊時,所需的費用大小 {int to,next,cap,flow,cost; }edge[MAXM];int head[MAXN],tol; int pre[MAXN],dis[MAXN]; bool vis[MAXN]; int N;void init(int n)    //N代表點的個數 {N=n;tol=0;memset(head,-1,sizeof(head)); }void addEdge(int u,int v,int cap,int cost)     {edge[tol].to=v;edge[tol].cap=cap;edge[tol].cost=cost;edge[tol].flow=0;edge[tol].next=head[u];head[u]=tol++;edge[tol].to=u;edge[tol].cap=0;edge[tol].cost=-cost;edge[tol].flow=0;edge[tol].next=head[v];head[v]=tol++; }bool spfa(int s,int t)    //計算最短路 {queue<int>q;for(int i=0;i<N;i++){dis[i]=INF;vis[i]=false;pre[i]=-1;}dis[s]=0;vis[s]=true;q.push(s);while(!q.empty()){int u=q.front();q.pop();vis[u]=false;for(int i=head[u];i!=-1;i=edge[i].next){int v=edge[i].to;if(edge[i].cap>edge[i].flow&&dis[v]>dis[u]+edge[i].cost){dis[v]=dis[u]+edge[i].cost;pre[v]=i;if(!vis[v]){vis[v]=true;q.push(v);}}}}if(pre[t]==-1)return false;elsereturn true; }int minCostMaxflow(int s,int t,int &cost)    //s代表源點,t代表匯點,cost是最后的總費用,函數返回值是最大流量值,注意點的編號要求從0到N-1 {int flow=0;cost=0;while(spfa(s,t)){int Min=INF;for(int i=pre[t];i!=-1;i=pre[edge[i^1].to]){if(Min>edge[i].cap-edge[i].flow)Min=edge[i].cap-edge[i].flow;}for(int i=pre[t];i!=-1;i=pre[edge[i^1].to]){edge[i].flow += Min;edge[i^1].flow -= Min;cost += edge[i].cost*Min;}flow+=Min;}return flow; }

?

轉載于:https://www.cnblogs.com/wsruning/p/5720761.html

總結

以上是生活随笔為你收集整理的最小费用最大流模版的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。