给出一个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
关注
打赏