链接:

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4533

题意:

给出一个v(3≤v≤1000)个点e(3≤e≤10000)条边的有向加权图,
求1~v的两条不相交(除了起点和终点外没有公共点)的路径,使得权和最小。

分析:

把2到v-1的每个结点i拆成i和i'两个结点,中间连一条容量为1,费用为0的边,然后求1到v的流量为2的最小费用流即可。
本题的拆点法是解决结点容量的通用方法。

代码:

 #include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
using namespace std; /// 结点下标从0开始,注意maxn
struct MCMF {
static const int maxn = * + ;
static const int INF = 0x3f3f3f3f;
struct Edge {
int from, to, cap, flow, cost;
}; 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 flow = , cost = ;
//while(BellmanFord(s, t, flow, cost));
while(flow < flow_limit && BellmanFord(s, t, flow_limit, flow, cost));
return cost;
}
}; MCMF mc; int main() {
int v, e;
while(~scanf("%d%d", &v, &e)) {
mc.init(v*-);
for(int i = ; i < v-; i++) mc.AddEdge(i, i+v-, , );
for(int a, b, c, i = ; i < e; i++) {
scanf("%d%d%d", &a, &b, &c);
a = (a == || a == v) ? a- : a+v-;
b--;
mc.AddEdge(a, b, , c);
}
printf("%d\n", mc.MincostMaxflow(, v-, ));
}
return ;
}

最小费用最大流模板:

 /// 结点下标从0开始,注意maxn
struct MCMF {
static const int maxn = 1e3 + ;
static const int INF = 0x3f3f3f3f;
struct Edge {
int from, to, cap, flow, cost;
}; 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, 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 = , cost = ;
while(BellmanFord(s, t, flow, cost));
//while(flow < flow_limit && BellmanFord(s, t, flow_limit, flow, cost));
return cost;
}
};

UVa 1658 - Admiral(最小费用最大流 + 拆点)的更多相关文章

  1. UVa 1658 Admiral(最小费用最大流)

    拆点费用流 --------------------------------------------------------------------- #include<cstdio> # ...

  2. BZOJ-1070 修车 最小费用最大流+拆点+略坑建图

    1070: [SCOI2007]修车 Time Limit: 1 Sec Memory Limit: 162 MB Submit: 3624 Solved: 1452 [Submit][Status] ...

  3. BZOJ-1877 晨跑 最小费用最大流+拆点

    其实我是不想做这种水题的QWQ,没办法,剧情需要 1877: [SDOI2009]晨跑 Time Limit: 4 Sec Memory Limit: 64 MB Submit: 1704 Solve ...

  4. BZOJ-2324 营救皮卡丘 最小费用可行流+拆下界+Floyd预处理

    准备一周多的期末,各种爆炸,回来后状态下滑巨快...调了一晚上+80%下午 2324: [ZJOI2011]营救皮卡丘 Time Limit: 10 Sec Memory Limit: 256 MB ...

  5. BZOJ-1927 星际竞速 最小费用最大流+拆点+不坑建图

    1927: [Sdoi2010]星际竞速 Time Limit: 20 Sec Memory Limit: 259 MB Submit: 1593 Solved: 967 [Submit][Statu ...

  6. hdu 4494 Teamwork 最小费用最大流

    Teamwork Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4494 ...

  7. bzoj 1877 [SDOI2009]晨跑(最小费用最大流)

    Description Elaxia最近迷恋上了空手道,他为自己设定了一套健身计划,比如俯卧撑.仰卧起坐等 等,不过到目前为止,他坚持下来的只有晨跑. 现在给出一张学校附近的地图,这张地图中包含N个十 ...

  8. UVA 1658 海军上将(拆点法+最小费用限制流)

    海军上将 紫书P375 这题我觉得有2个难点: 一是拆点,要有足够的想法才能把这题用网络流建模,并且知道如何拆点. 二是最小费用限制流,最小费用最大流我们都会,但如果限制流必须为一个值呢?比如这题限制 ...

  9. UVA - 1658 Admiral (最小费用最大流)

    最短路对应费用,路径数量对应流量.为限制点经过次数,拆点为边.跑一次流量为2的最小费用最大流. 最小费用最大流和最大流EK算法是十分相似的,只是把找增广路的部分换成了求费用的最短路. #include ...

随机推荐

  1. Centos7 ftp服务器搭建

    1.使用yum安装ftp服务端: yum install -y vsftpd 2.使用yum安装ftp客户端: yum install -y ftp.x86_64 3.开启ftp服务设置开机启动并查看 ...

  2. GridView 基本使用

    项目中实例一 <asp:GridView ID="gvBatchReceive" runat="server" AutoGenerateColumns=& ...

  3. 使用sqlcmd进行MS-dos方式查询

    在windows选择‘运行’vista需要以管理员身份运行,打开命令提示符窗口 要连接到sql server服务器,必须指定服务器名称,安装命名实例中的,还必须指定实例名.默认情况下,sqlcmd使用 ...

  4. 移动端Push推送

    移动端Push推送 移动端开发逃不掉要做推送,这里给出服务端一种省时省力的解决方案. iOS:PushSharp.Apple.苹果有自己的推送服务,我们按照规则推送数据就好.这里我选取PushShar ...

  5. redis(8)集群简介

    一.集群 互联网每天都会产生大量的数据,单实例已经不能满足需求.但是如果依赖于硬件成本的提升,那就不是所有人能够负担的起的. 集群这个时候出现,一定程度上解决了这个问题.它通过互联网,将多个单实例连接 ...

  6. aspose words做插入压缩后图片到Word文档中

    最近用aspose words做导出Word的功能,发现图片的导出有点难受,一开始是这样写的 Document doc = new Document("D:\\Template.docx&q ...

  7. [模拟回调] demo1模拟用字符串调用js函数 demo2模拟springmvc controller回调页面js函数

    demo1. 模拟用字符串调用js 函数 function dataQuery() { var strFun = "testCallBack"; var strParam = &q ...

  8. Spring_Spring与IoC_第一个程序

    一.IoC IoC是一种概念,是一种思想,指将传统上由程序代码直接操控的对象调用权交给容器,通过容器来实现对象的装配和管理.控制反转是对对象控制权的转移,从程序代码本身反转到外部容器. 当前IoC比较 ...

  9. linux 安装php扩展swoole redis

    本文讲的是已经有redis.so 和swoole.so文件的情况 我的环境是xampp php的扩展目录为 /opt/lampp/lib/php/extensions/no-debug-non-zts ...

  10. 将ojdbc 添加到maven

    去oracle官网下载jar包 然后在jar包所在目录输入maven命令 mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdb ...