uva 1658 Admiral - 费用流
vjudge传送门[here]
题目大意:给一个有(3≤v≤1000)个点e(3≤e≤10000)条边的有向加权图,求1~v的两条不相交(除了起点和终点外没有公共点)的路径,使权值和最小。
正解是吧2到v-1的每个点拆成两个点,中间连一条容量为1,费用为0的边,然后求1到v的流量为2的最小费用流就行了。
Code
/**
* Uva
* Problem#1658
* Accepted
*/
#include<iostream>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<cstdlib>
#include<fstream>
#include<sstream>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
typedef bool boolean;
#define INF 0xfffffff
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
template<typename T>
inline boolean readInteger(T& u){
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-' && ~x);
if(x == -){
return false;
}
if(x == '-'){
x = getchar();
aFlag = -;
}
for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
ungetc(x, stdin);
u *= aFlag;
return true;
} ///map template starts
typedef class Edge{
public:
int end;
int next;
int cap;
int flow;
int cost;
Edge(const int end = , const int next = , const int cap = , const int flow = , const int cost = ):end(end), next(next), cap(cap), flow(flow), cost(cost){}
}Edge; typedef class MapManager{
public:
int ce;
int *h;
Edge *edge;
MapManager(){}
MapManager(int points, int limit):ce(){
h = new int[(const int)(points + )];
edge = new Edge[(const int)(limit + )];
memset(h, , sizeof(int) * (points + ));
}
inline void addEdge(int from, int end, int cap, int flow, int cost){
edge[++ce] = Edge(end, h[from], cap, flow, cost);
h[from] = ce;
}
inline void addDoubleEdge(int from, int end, int cap, int cost){
addEdge(from, end, cap, , cost);
addEdge(end, from, cap, cap, -cost);
}
Edge& operator [](int pos){
return edge[pos];
}
inline int reverse(int pos){
return (pos & ) ? (pos + ) : (pos - );
}
inline void clean(){
delete[] h;
delete[] edge;
ce = ;
}
}MapManager;
#define m_begin(g, i) (g).h[(i)]
/// map template ends int n, m;
MapManager g; inline boolean init(){
if(!readInteger(n)) return false;
readInteger(m);
g = MapManager(n * , m * + n * );
for(int i = , a, b, w; i <= m; i++){
readInteger(a);
readInteger(b);
readInteger(w);
g.addDoubleEdge(a + n, b, , w);
}
for(int i = ; i <= n; i++){ //拆点
g.addDoubleEdge(i, i + n, , );
}
return true;
} int* dis;
boolean* visited;
int* last;
int* laste;
int* mflow;
int cost;
int s, t;
queue<int> que;
int sizee; inline void spfa(){
memset(dis, 0x7f, sizeof(int) * (sizee));
memset(visited, false, sizeof(boolean) * (sizee));
que.push(s);
last[s] = ;
dis[s] = ;
laste[s] = ;
mflow[s] = INF;
visited[s] = true;
while(!que.empty()){
int e = que.front();
que.pop();
visited[e] = false;
for(int i = m_begin(g, e); i != ; i = g[i].next){
int &eu = g[i].end;
if(dis[e] + g[i].cost < dis[eu] && g[i].flow < g[i].cap){
dis[eu] = dis[e] + g[i].cost;
last[eu] = e;
laste[eu] = i;
mflow[eu] = min(g[i].cap - g[i].flow, mflow[e]);
if(!visited[eu] && eu != t){
que.push(eu);
visited[eu] = true;
}
}
}
}
for(int i = t; i != s; i = last[i]){
g[laste[i]].flow += mflow[t];
g[g.reverse(laste[i])].flow -= mflow[t];
cost += mflow[t] * g[laste[i]].cost;
}
} inline void minCostFlow(){
s = + n, t = n, sizee = * n + ;
dis = new int[(const int)(sizee)];
visited = new boolean[(const int)(sizee)];
last = new int[(const int)(sizee)];
laste = new int[(const int)(sizee)];
mflow = new int[(const int)(sizee)];
spfa();
spfa();
} inline void solve(){
cost = ;
minCostFlow();
printf("%d\n", cost);
g.clean();
} int main(){
while(init()){
solve();
}
return ;
}
uva 1658 Admiral - 费用流的更多相关文章
- uva 1658 Admiral (最小费最大流)
uva 1658 Admiral 题目大意:在图中找出两条没有交集的线路,要求这两条线路的费用最小. 解题思路:还是拆点建图的问题. 首先每一个点都要拆成两个点.比如a点拆成a->a'.起点和终 ...
- UVa 1658 Admiral(最小费用最大流)
拆点费用流 --------------------------------------------------------------------- #include<cstdio> # ...
- UVALive - 6266 Admiral 费用流
UVALive - 6266 Admiral 题意:找两条完全不相交不重复的路使得权值和最小. 思路:比赛的时候时间都卡在D题了,没有仔细的想这题,其实还是很简单的,将每个点拆开,连一条容量为1,费用 ...
- UVa 1658 - Admiral(最小费用最大流 + 拆点)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA - 1658 Admiral (最小费用最大流)
最短路对应费用,路径数量对应流量.为限制点经过次数,拆点为边.跑一次流量为2的最小费用最大流. 最小费用最大流和最大流EK算法是十分相似的,只是把找增广路的部分换成了求费用的最短路. #include ...
- uva 1658 Admiral 【 最小费用最大流 】
拆点,每个点拆成 i,i' 在i 和i‘之间连一条费用为0,容量为1的边,就可以保证每个点只经过一次 特殊的点,1和n之间,,,n和2*n之间连一条费用为0,容量为2的边,可以求出两条路径 #incl ...
- uva 1658(最小费用最大流)
题意:一个带权有向图,求起点到终点的两条路径权值之和最小,且两条路径没有公共点(除起点,终点): 分析:拆点法,将u拆成u和u',u-u'容量为1,费用为0,这样就能保证每个点只用一次,起点s-s'容 ...
- UVA - 1658 Admiral
3. C - Admiral 题意:给定v(3<=v<=1000)个节点,e(3<=e<=10000)条边的又向加权图,求1->v的两条不相交的路径,使得权和最小. 思路 ...
- UVA 1658 Admiral 海上将军(最小费用流,拆点)
题意: 一个有v个点的有向图,要从点1到点v需要找两条路径,两路径不可经过同一个点(除了1和v点).求这两条路径的最小费用(保证有解). 分析: 难在建图,其他套模板. 此图给的是超级复杂图,两个点之 ...
随机推荐
- python面向对象高级:@property
@property 把方法『变成』了属性,广泛应用在类的定义中,可以让调用者写出简短的代码,同时保证对参数进行必要的检查,这样,程序运行时就减少了出错的可能性. 最大的作用就是既能检查参数,又可以用类 ...
- What is Gensim?
Gensim是一款开源的第三方Python工具包,用于从原始的非结构化的文本中,无监督地学习到文本隐层的主题向量表达.它支持包括TF-IDF,LSA,LDA,和word2vec在内的多种主题模型算法, ...
- FW 构建OpenStack的高可用性(HA,High Availability)
原文地址:http://blog.csdn.net/hilyoo/article/details/7704280 1.CAP理论 1) CAP 理论给出了3个基本要素: 一致性 ( Consisten ...
- iOS多线程编程之GCD介绍(转载)
一.简单介绍 1.什么是GCD? 全称是Grand Central Dispatch,可译为“牛逼的中枢调度器” 纯C语言,提供了非常多强大的函数 2.GCD的优势 GCD是苹果公司为多核的并行运算提 ...
- TensorFlow基础1:reduce_sum()函数和reduce_mean()函数
https://blog.csdn.net/chengshuhao1991/article/details/78545723 在计算损失时,通常会用到reduce_sum()函数来进行求和,但是在使用 ...
- QtCreator 可以通过 Clang-Tidy 和 CLazy 对你的代码进行静态检查
QtCreator 可以通过 Clang-Tidy 和 CLazy 对你的代码进行静态检查 打开你的工程,点击Analyze -> Clang-Tidy and CLazy 选择你想分析的 cp ...
- RSA加密常用的填充方式 以及 常见错误
一.RSA加密常用的填充方式 1.RSA_PKCS1_PADDING 输入:比 RSA modulus 短至少11个字节.如果输入的明文过长,必须切割,然后填充 输出:和modulus一样长 根据这个 ...
- redis5.0主从配置
1.下载 wget http://download.redis.io/releases/redis-5.0.3.tar.gz .tar.gz cd redis- make make test //检查 ...
- Spark将计算结果写入到Mysql中
今天主要来谈谈如何将Spark计算的结果写入到Mysql或者其他的关系型数据库里面.其实方式也很简单,代码如下: package scala import java.sql.{DriverManage ...
- PAT 1030 Travel Plan[图论][难]
1030 Travel Plan (30)(30 分) A traveler's map gives the distances between cities along the highways, ...