UVa 1658 海军上将(最小费用最大流)
https://vjudge.net/problem/UVA-1658
题意:
给出一个v个点e条边的有向加权图,求1~v的两条不相交(除了起点和终点外公共点)的路径,使得权和最小。
思路:
把2到v-1的每个点拆分为两个节点,容量为1,也就是只可以用一次,费用为0,然后求1到v的流量为2的最小费用流。
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
using namespace std; const int maxn = + ;
const int INF = 0x3f3f3f3f; typedef long long LL; 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];
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, LL & 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;
flow += a[t];
cost += (LL)d[t] * (LL)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;
} void MincostMaxdflow(int s, int t, int limit, LL & cost){
int flow = ; cost = ;
while (BellmanFord(s, t, flow, cost) && flow < limit);
//return flow;
}
}t; int main()
{
//freopen("D:\\input.txt", "r", stdin);
int n, m;
while (~scanf("%d%d", &n, &m))
{
t.init( * n - );
for (int i = ; i <= n - ; i++)
{
t.AddEdge(i - , n - + i, , );
}
for (int i = ; i<m; i++)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
v--;
if (u != && u != n) u += n - ;
else u--;
t.AddEdge(u, v, , w);
}
LL cost;
t.MincostMaxdflow(, n - , , cost);
printf("%lld\n", cost);
}
return ;
}
UVa 1658 海军上将(最小费用最大流)的更多相关文章
- UVa 1658 Admiral(最小费用最大流)
拆点费用流 --------------------------------------------------------------------- #include<cstdio> # ...
- UVA 1658 海军上将(拆点法+最小费用限制流)
海军上将 紫书P375 这题我觉得有2个难点: 一是拆点,要有足够的想法才能把这题用网络流建模,并且知道如何拆点. 二是最小费用限制流,最小费用最大流我们都会,但如果限制流必须为一个值呢?比如这题限制 ...
- UVa 1658 - Admiral(最小费用最大流 + 拆点)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA - 1658 Admiral (最小费用最大流)
最短路对应费用,路径数量对应流量.为限制点经过次数,拆点为边.跑一次流量为2的最小费用最大流. 最小费用最大流和最大流EK算法是十分相似的,只是把找增广路的部分换成了求费用的最短路. #include ...
- UVa 10806 Dijkstra,Dijkstra(最小费用最大流)
裸的费用流.往返就相当于从起点走两条路到终点. 按题意建图,将距离设为费用,流量设为1.然后增加2个点,一个连向节点1,流量=2,费用=0;结点n连一条同样的弧,然后求解最小费用最大流.当且仅当最大流 ...
- UVA 10806 最小费用最大流
终于可以写这道题的题解了,昨天下午纠结我一下下午,晚上才照着人家的题解敲出来,今天上午又干坐着想了两个小时,才弄明白这个问题. 题意很简单,给出一个无向图,要求从1 到 n最短路两次,但是两次不允许经 ...
- [板子]最小费用最大流(Dijkstra增广)
最小费用最大流板子,没有压行.利用重标号让边权非负,用Dijkstra进行增广,在理论和实际上都比SPFA增广快得多.教程略去.转载请随意. #include <cstdio> #incl ...
- bzoj1927最小费用最大流
其实本来打算做最小费用最大流的题目前先来点模板题的,,,结果看到这道题二话不说(之前打太多了)敲了一个dinic,快写完了发现不对 我当时就这表情→ =_=你TM逗我 刚要删突然感觉dinic的模 ...
- ACM/ICPC 之 卡卡的矩阵旅行-最小费用最大流(可做模板)(POJ3422)
将每个点拆分成原点A与伪点B,A->B有两条单向路(邻接表实现时需要建立一条反向的空边,并保证环路费用和为0),一条残留容量为1,费用为本身的负值(便于计算最短路),另一条残留容量+∞,费用为0 ...
- HDU5900 QSC and Master(区间DP + 最小费用最大流)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5900 Description Every school has some legends, ...
随机推荐
- [USB] Windows USB/DVD Download Tool
此工具为微软官方U盘启动盘制作工具 Windows USB/DVD Download Tool 说明:https://www.microsoft.com/en-us/download/windows- ...
- This function has none of DETERMINISTIC, NO SQL
错误信息: [Err] 1418 - This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declara ...
- Thinkphp的cookie的怎么玩?
在使用COOKIE的时候,首先要对COOKIE进行加密,加密方式采用:异位或的方式进行加密: // 异位或加密 1是加密 0 是解密 function encrytion($value,$type=0 ...
- 310实验室(七)OptimizationTemplateLibrary
利用泛型编程思想,C++模板. 首先定义变量或者重新typedef variables: 模板中的变量:_TRandom.double _TReal._TProblem::TDecision _TD ...
- chinese-typesetting:更好的中文文案排版
欢迎指正.GitHub 地址:https://github.com/jxlwqq/chinese-typesetting 更好的中文文案排版 统一中文文案.排版的相关用法,降低团队成员之间的沟通成本, ...
- loadNibNamed:(NSString *)name owner:(nullable id)owner options:(nullable NSDictionary *)options用法
1.name xib的名字 owner当前类对象 options初始参数 实际应用: NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@&quo ...
- centos 特殊权限 各种搜索命令 lsattr ,chattr,suid,sgid,sbit,file,type是否是内置命令,stat文件属性 ,whereis,locate,find,ln 内部命令和外部命令 第五节课
centos 特殊权限 各种搜索命令 lsattr ,chattr,suid,sgid,sbit,file,type是否是内置命令,stat文件属性 ,whereis,locate,find,ln ...
- Spring源码解析(三)BeanDefinition的载入、解析和注册
通过上一篇源码的分析已经完成了BeanDefinition资源文件的定位,本篇继续分析BeanDefinition资源文件的载入和解析. AbstractBeanDefinitionReader的lo ...
- python技巧总结之set、日志、rsa加密
一.日志模块logging模块调用 1.日志模块使用原理 #!/usr/bin/python # -*- coding:utf-8 -*- import logging # 方式一: "&q ...
- 028-touch命令
1.创建空文件.可以创建一个空文件,也可以批量创建空文件. 2.更改文件/目录的访问时间,如果文件存在就更改访问时间,不存在就创建.# touch -a 3.更改文件的访问时间和修改时间.如果文件存在 ...