给出一个v(3≤v≤1000)个点e(3≤e≤10000)条边的有向加权图,求1~v的两条不相交(除了起点和终点外没有公共点)的路径,使得权和最小.
分析 什么是两条不相交的边?就是让各个点的容量为1(不是只有流量)。这是怎么做到的?
就是把一个点拆成两个点,中间连一条容量为一的边,要保证拆后的两个点编号不同。然后拆完点跑一次最小费用流就可以了。但是这时候的流量是有限制的,因为只能是两条路,所以在扩展增广路的时候要加上一条这个东西。
f(flow+a[t]>flow_limit) a[t]=flow_limit-flow;
这样就能保证总的流量不会超过flow_limit了。
代码(注上边那个形式(流量限制)才是对的)
#include
#include
#include
#include
#include
#include
#define maxn 10005
#define INF 999999
using namespace std;
struct Edge{
int from,to,cap,flow,cost;
};
struct MCMF{
int n,m,s,t;
vector edges;vector G[maxn];
int inq[maxn],d[maxn],a[maxn],p[maxn];
void init(int n){
this->n=n;
for(int i=0;i2) a[t]=2;
flow+=a[t];
cost+=a[t]*d[t];
for(int u=t;u!=s;u=edges[p[u]].from){
edges[p[u]].flow+=a[t];
edges[p[u]^1].flow-=a[t];
}
return true;
}
int Mincostflow(int s,int t,int &cost){
int flow=0; cost=0;
while(flow
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?