const int maxn = 110;
const int maxm = 5010;
struct Edge
{
int to, next, c, f;
}edge[maxm*10];
int head[maxn],dis[maxn],load[maxn],p[maxn];
int n;
bool flag[maxn];
bool spfa(int start,int end)
{
int que[maxn*10],qout,qin;
memset(flag, 0, sizeof(flag));
memset(load, -1, sizeof(load));
memset(p, -1, sizeof(p));
for(int i = 0;i <= n;i ++)
dis[i] = inf;
qin = qout = 0;
que[qin++] = start;
dis[start] = 0;
flag[start] = true;
while(qin != qout)
{
int e = que[qout++];
flag[e] = false;
for(int i = head[e];i != -1;i = edge[i].next)
{
if(edge[i].c)
{
int ne = egde[i].to;
if(dis[ne] > dis[e] + edge[i].f)
{
dis[ne] = dis[e] + edge[i].f;
p[ne] = e;
load[ne] = i;
if(!flag[ne])
{
flag[ne] = true;
que[qin++] = ne;
}
}
}
}
}
if(dis[end] == inf)
return false;
return true;
}
int min_cost_flow(int start,int end)
{
int u,mn;
int ans_flow = 0, ans_cost = 0;
while(spfa(start, end))
{
u = end;
mn = inf;
while(p[u] != -1)
{
mn = min(egde[lode[u]].c, mn);
u = p[u];
}
u = end;
while(p[u] != -1)
{
edge[load[u]].c -= mn;
edge[load[u]^1].c += mn;
u = p[u];
}
ans_cost += dis[end]*mn;
ans_flow += mn;
}
return ans_cost;
}
连续增广路算法
最新推荐文章于 2024-07-14 12:17:20 发布