首页 技术 正文
技术 2022年11月7日
0 收藏 336 点赞 398 浏览 1808 个字

题意:

给出一个有向带权图,求从起点到终点的两条不相交路径使得权值和最小。

分析:

第一次听到“拆点法”这个名词。

把除起点和终点以外的点拆成两个点i和i’,然后在这两点之间连一条容量为1,费用为0的边。这样就保证了每个点最多经过一次。

其他有向边的容量也是1

然后求从起点到终点的流量为2(这样就保证了是两条路径)的最小费用流。

 #include <bits/stdc++.h> using namespace std; const int maxn =  + ;
const int INF = ; struct Edge
{
int from, to, cap, flow, cost;
Edge(int u, int v, int c, int f, int w): from(u), to(v), cap(c), flow(f), cost(w) {}
}; struct MCMF
{
int n, m;
vector<Edge> edges;
vector<int> G[maxn];
int inq[maxn]; //是否在队列中
int d[maxn]; //Bellman-Ford
int p[maxn]; //上一条弧
int a[maxn]; //可改进量 void Init(int n)
{
this->n = n;
for(int i = ; i < n; ++i) G[i].clear();
edges.clear();
} void AddEdge(int from, int to, int cap, int cost)
{
edges.push_back(Edge(from, to, cap, , cost));
edges.push_back(Edge(to, from, , , -cost));
m = edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
} bool BellmanFord(int s, int t, int flow_limit, int& flow, int& cost)
{
for(int i = ; i < n; ++i) d[i] = INF;
memset(inq, , sizeof(inq));
d[s] = ; inq[s] = ; p[s] = ; a[s] = INF; queue<int> Q;
Q.push(s);
while(!Q.empty())
{
int u = Q.front(); Q.pop();
inq[u] = ;
for(int i = ; i < G[u].size(); ++i)
{
Edge& e = edges[G[u][i]];
if(e.cap > e.flow && d[e.to] > d[u] + e.cost)
{
d[e.to] = d[u] + e.cost;
p[e.to] = G[u][i];
a[e.to] = min(a[u], e.cap - e.flow);
if(!inq[e.to]) { Q.push(e.to); inq[e.to] = ; }
}
}
}
if(d[t] == INF) return false;
if(flow + a[t] > flow_limit) a[t] = flow_limit - flow;
flow += a[t];
cost += d[t] * a[t];
for(int u = t; u != s; u = edges[p[u]].from)
{
edges[p[u]].flow += a[t];
edges[p[u]^].flow -= a[t];
}
return true;
} int MincostMaxflow(int s, int t, int flow_limit, int& cost)
{
int flow = ; cost = ;
while(flow < flow_limit && BellmanFord(s, t, flow_limit, flow, cost));
return flow;
}
}g; int main()
{
//freopen("in.txt", "r", stdin); int n, m;
while(scanf("%d%d", &n, &m) == && n)
{
g.Init(n*-);
//2~n-1 i和i'的编号分别为1~n-2 n~2n-3
for(int i = ; i <= n-; ++i) g.AddEdge(i-, n-+i, , );
for(int i = ; i < m; ++i)
{ //连接a'->b
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
if(a != && a != n) a += n-; else a--;
b--;
g.AddEdge(a, b, , c);
}
int cost;
g.MincostMaxflow(, n-, , cost);
printf("%d\n", cost);
} return ;
}

代码君

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,083
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,558
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,407
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,180
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,817
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,900