题意:给定一个图,求1-n的两条不相交的路线,并且权值和最小。

析:最小费用流,把每个结点都拆成两个点,中间连一条容量为1的边,然后一个作为入点,另一个是出点。最后跑两次最小费用流就行了。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-5;
const int maxn = 2000 + 10;
const int mod = 1e6;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
struct Edge{
int from, to, cap, flow;
LL cost;
}; struct MCMF{
int n, m;
vector<Edge> edges;
vector<int> G[maxn];
int inq[maxn];
LL d[maxn];
int p[maxn];
int a[maxn]; void init(int n){
this->n = n;
for(int i = 0; i < n; ++i) G[i].clear();
edges.clear();
} void addEdge(int from, int to, int cap, LL cost){
edges.push_back((Edge){from, to, cap, 0, cost});
edges.push_back((Edge){to, from, 0, 0, -cost});
m = edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
} bool BellmanFord(int s, int t, int &flow, LL &cost){
for(int i = 0; i < n; ++i) d[i] = INF;
memset(inq, 0, sizeof inq);
d[s] = 0; inq[s] = 1; p[s] = 0; a[s] = INF; queue<int> q;
q.push(s);
while(!q.empty()){
int u = q.front(); q.pop();
inq[u] = 0;
for(int i = 0; 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] = 1;
}
}
}
if(d[t] == INF) return false;
flow += a[t];
cost += d[t] * a[t];
int u = t;
while(u != s){
edges[p[u]].flow += a[t];
edges[p[u]^1].flow -= a[t];
u = edges[p[u]].from;
}
return true;
} LL minCost(int s, int t){
int flow = 0;
LL cost = 0;
while(BellmanFord(s, t, flow, cost));
return cost;
}
};
MCMF mcmf; int main(){
while(scanf("%d %d", &n, &m) == 2){
int u, v, val;
mcmf.init(n+n+1);
for(int i = 0; i < m; ++i){
scanf("%d %d %d", &u, &v, &val);
mcmf.addEdge(u+n, v, 1, val);
}
for(int i = 2; i < n; ++i)
mcmf.addEdge(i, i+n, 1, 0);
mcmf.addEdge(1, 1+n, 2, 0);
mcmf.addEdge(n, n+n, 2, 0);
printf("%lld\n", mcmf.minCost(1, n+n) + mcmf.minCost(1, n+n));
}
return 0;
}

UVa 1658 Admiral (最小费用流)的更多相关文章

  1. uva 1658 Admiral (最小费最大流)

    uva 1658 Admiral 题目大意:在图中找出两条没有交集的线路,要求这两条线路的费用最小. 解题思路:还是拆点建图的问题. 首先每一个点都要拆成两个点.比如a点拆成a->a'.起点和终 ...

  2. UVA 1658 Admiral 海上将军(最小费用流,拆点)

    题意: 一个有v个点的有向图,要从点1到点v需要找两条路径,两路径不可经过同一个点(除了1和v点).求这两条路径的最小费用(保证有解). 分析: 难在建图,其他套模板. 此图给的是超级复杂图,两个点之 ...

  3. uva 1658 Admiral - 费用流

    vjudge传送门[here] 题目大意:给一个有(3≤v≤1000)个点e(3≤e≤10000)条边的有向加权图,求1~v的两条不相交(除了起点和终点外没有公共点)的路径,使权值和最小. 正解是吧2 ...

  4. UVa 1658 - Admiral(最小费用最大流 + 拆点)

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

  5. UVA - 1658 Admiral

    3. C - Admiral 题意:给定v(3<=v<=1000)个节点,e(3<=e<=10000)条边的又向加权图,求1->v的两条不相交的路径,使得权和最小. 思路 ...

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

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

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

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

  8. uva 1658 Admiral 【 最小费用最大流 】

    拆点,每个点拆成 i,i' 在i 和i‘之间连一条费用为0,容量为1的边,就可以保证每个点只经过一次 特殊的点,1和n之间,,,n和2*n之间连一条费用为0,容量为2的边,可以求出两条路径 #incl ...

  9. UVa 1658 (拆点法 最小费用流) Admiral

    题意: 给出一个有向带权图,求从起点到终点的两条不相交路径使得权值和最小. 分析: 第一次听到“拆点法”这个名词. 把除起点和终点以外的点拆成两个点i和i',然后在这两点之间连一条容量为1,费用为0的 ...

随机推荐

  1. 《好好说话》zz

    最近,<奇葩说>闹出来了一些不愉快. 在半决赛中,姜思达惜败,愤怒的粉丝把矛头指向那场比赛的其他人.最终,马薇薇.黄执中和网友们吵起来了. 这件事本不算大事,毕竟娱乐业就是这个样子.刚刚好 ...

  2. nodejs websocket 聊天应用

    前端一直是一块充满惊喜的土地,不仅是那些富有创造性的页面,还有那些惊赞的效果及不断推出的新技术.像node.js这样的后端开拓者直接将前端人员的能力扩大到了后端.瞬间就有了一统天下的感觉,来往穿梭于前 ...

  3. Selenium学习(二)入门小例子

    1)打开百度页面 2)输入“hello” 3)点击百度一下按钮 from selenium import webdriver url = "http://" + "www ...

  4. Oracle SQL_杂记

    1. 查询当前用户角色,当前用户下的表权限以及所有用户表权限 desc user_role_privs;select * from user_role_privs; desc user_tab_pri ...

  5. php中屏蔽date的错误

    php中添加  date_default_timezone_set('asia/shanghai'); 可以屏蔽 <?php echo date('Y-m-d',$row3['time']); ...

  6. 搭建mysql主从集群的步骤

    前提条件是:须要在linux上安装4个mysql数据库,都须要配置完对应的信息. 须要搭建: mysql 01: 主数据库  master                  mysql 02 :   ...

  7. iframe调用父页面js函数 方法 元素

    在一个页面中添加iframe,但是有时需要与父页面进行通信,传递参数. 网上总结有以下方法: 一.iframe标签中   src属性传参 <iframe src="test.jsp?i ...

  8. IP address could not be resolved: Name or service not known

    [root@test ~]# /usr/local/mysql/bin/mysqld2018-08-05T07:00:33.647509Z 0 [Warning] [MY-011070] [Serve ...

  9. JavaProject和IProject

    由 IProject 项目得到 Java 项目的方式: IJavaProject javaPoject = JavaCore.create(IProject); 由 IJavaProject 得到 I ...

  10. 剑指Offer:重建二叉树【7】

    剑指Offer:重建二叉树[7] 题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5 ...