acm模版
/*
* 最小費用流 o(v * e * f)
* init: network g; g.build(v, e);
* call: g.mincost(s, t); flow=g.flow; cost=g.cost;
* 注意: spfa增廣, 實際複雜度遠遠小於o(v * e);
*/#define typef int // type of flow
#define typec int // type of dis
const typef inff = 0x3f3f3f3f; // max of flow
const typec infc = 0x3f3f3f3f; // max of dis
const
int e = 10010;
const
int n = 1010;
struct network
int mincost(int src, int sink)
d[src] = 0;
pv[src] = src;
vis[src] = 1;
for (f = 0, r = 1, que[0] = src; r != f;)
for (k = head[i]; k != -1; k = nxt[k])
}pv[pnt[k]] = i;
pe[pnt[k]] = k;}}
}if (-1 == pv[sink])
for (k = sink, mxf = inff; k != src; k = pv[k])
}flow += mxf;
cost += d[sink] * mxf;
for (k = sink; k != src; k = pv[k])
}return cost;
}void build(int v, int e)
}} g;
/*
* 最小費用流 o(v^2 * f)
* init: network g; g.build(nv, ne);
* call: g.mincost(s, t); flow=g.flow; cost=g.cost;
* 注意: 網路中弧的cost需為非負. 若存在負權, 進行如下轉化:
* 首先如果原圖有負環, 則不存在最小費用流. 那麼可以用johnson
* 重標號技術把所有邊變成正權,以後每次增廣後進行維護,演算法如下:
* 1、用bellman-ford求s到各點的距離phi;
* 2、以後每求一次最短路,設s到各點的最短距離為dis;
* for i = 1 to v do
* phi[v] += dis[v];
* 下面的**已經做了第二步,如果原圖有負權,新增第一步即可。
*/#define typef int // type of flow
#define typec int // type of cost
const typef inff = 0x3f3f3f3f; // max of flow
const typec infc = 0x3f3f3f3f; // max of cost
const
int e = 10010;
const
int n = 1010;
struct edge
int other(int p)
typef cap(int p)
typec ecost(int p)
else
if (flow > 0)
else
}void addflow(int p, typef f)
};struct network
;bool network::dijkstra()
dis[s] = 0;
prev[s] = 0;
pre[s] = -1;
memset(vis, 0, v * sizeof(int));
for (i = 1; i < v; i++)
}if (md == infc)
for (vis[u] = 1, j = (int)net[u].size() - 1; j >= 0; j--)}}
}return infc != dis[t];
}typec network::mincost(int ss, int tt)
for (i = (int)eg.size() - 1; i >= 0; i--)
while (dijkstra())
}for (c = t; c != s; c = pre[c])
flow += ex;
cost += ex * (dis[t] + phi[t]);
for (i = 0; i < v; i++)
}return cost;
}void network::build(int nv, int ne)
return ;
}
最小費用流
include include define maxn 61 define maxv maxn maxn 2 1 define maxe maxv 5 define oo 2147483647 define min a,b a b b a define maxq maxe using namespa...
最小費用流
int v 頂點數 vector g max v int dist max v 最短距離 int prev max v 最短路中前驅結點對應的點 int pree max v 最短路中前驅結點對應的邊 void addedge int from,int to,int cap,int cost 求從s...
最小費用流
單向圖 題目鏈結請點這裡 include 每次找費用的最短路,更新殘留網路圖直到找不到最短路為止 include 最大費用 權值取負值 結果取負值 include include include using namespace std const int inf 0x3f3f3f3f struct ...