题目链接  

Farm Tour
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 18961   Accepted: 7326

Description

When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 <= N <= 1000) fields numbered 1..N, the first of which contains his house and the Nth of which contains the big barn. A total M (1 <= M <= 10000) paths that connect the fields in various ways. Each path connects two different fields and has a nonzero length smaller than 35,000.

To show off his farm in the best way, he walks a tour that starts at his house, potentially travels through some fields, and ends at the barn. Later, he returns (potentially through some fields) back to his house again.

He wants his tour to be as short as possible, however he doesn't want to walk on any given path more than once. Calculate the shortest tour possible. FJ is sure that some tour exists for any given farm.

Input

* Line 1: Two space-separated integers: N and M.

* Lines 2..M+1: Three space-separated integers that define a path: The starting field, the end field, and the path's length.

Output

A single line containing the length of the shortest tour. 

Sample Input

4 5
1 2 1
2 3 1
3 4 1
1 3 2
2 4 2

Sample Output

6

题意:有n个点,m条边,无向图,问你1->n->1的最短路径是多少。FJ带朋友参观,想经可能多看几个地方,所以每条边只能走一次。

思路:把路径长度作为费用,流量为1,这样每条路就只能走一次了。自己建源点s,汇点t。s->1流量2,n->t流量2,因为这两条边要走两次。然后直接费用流。水题,改改板子就过了,学会费用流后的第二题。

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue> using namespace std;
const int MAXN = ;
const int MAXM = ;
const int INF = 0x7FFFFFFF; int n, m, first[MAXN], s, t, sign; int max_flow, min_cost; struct Edge {
int to, cap, cost, next;
} edge[MAXM * ]; inline void init() {
for(int i = ; i <= n + ; i++ ) {
first[i] = -;
}
sign = ;
} inline void add_edge(int u, int v, int cap, int cost) {
edge[sign].to = v, edge[sign].cap = cap, edge[sign].cost = cost;
edge[sign].next = first[u], first[u] = sign ++;
edge[sign].to = u, edge[sign].cap = , edge[sign].cost = -cost;
edge[sign].next = first[v], first[v] = sign ++;
} int dist[MAXN], inq[MAXN], pre[MAXN], incf[MAXN]; bool spfa(int s, int t) {
for(int i = ; i <= n + ; i++ ) {
dist[i] = INF, inq[i] = ;
}
queue<int>que;
que.push(s), inq[s] = , dist[s] = ;
incf[s] = 0x3f3f3f3f;
while(!que.empty()) {
int now = que.front();
que.pop();
inq[now] = ;
for(int i = first[now]; ~i; i = edge[i].next) {
int to = edge[i].to, cap = edge[i].cap, cost = edge[i].cost;
if(cap > && dist[to] > dist[now] + cost) {
dist[to] = dist[now] + cost;
incf[to] = min(incf[now], cap);
pre[to] = i;
if(!inq[to]) {
que.push(to);
inq[to] = ;
}
}
}
}
return dist[t] != INF;
} void update(int s, int t) {
int x = t;
while(x != s) {
int pos = pre[x];
edge[pos].cap -= incf[t];
edge[pos ^ ].cap += incf[t];
x = edge[pos ^ ].to;
}
max_flow += incf[t];
min_cost += dist[t] * incf[t];
} void minCostMaxFlow(int s, int t) {
while(spfa(s, t)) {
update(s, t);
}
} int main()
{
while(~scanf("%d %d", &n, &m)) {
s = , t = n + ;
init();
for(int i = ; i <= m; i++ ) {
int u, v, cap, cost;
scanf("%d %d %d", &u, &v, &cost);
add_edge(u, v, , cost);
add_edge(v, u, , cost);
}
add_edge(s, , , );
add_edge(n, t, , );
max_flow = min_cost = ;
minCostMaxFlow(s, t);
printf("%d\n", min_cost);
} return ;
}

POJ 2135 最小费用最大流的更多相关文章

  1. POJ 2135 最小费用最大流 入门题

    Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19207   Accepted: 7441 Descri ...

  2. poj 2135最小费用最大流

    最小费用最大流问题是经济学和管理学中的一类典型问题.在一个网络中每段路径都有"容量"和"费用"两个限制的条件下,此类问题的研究试图寻找出:流量从A到B,如何选择 ...

  3. poj 3422(最小费用最大流)

    题目链接:http://poj.org/problem?id=3422 思路:求从起点到终点走k次获得的最大值,最小费用最大流的应用:将点权转化为边权,需要拆点,边容量为1,费用为该点的点权,表示该点 ...

  4. POJ 2516 最小费用最大流

    每一种货物都是独立的,分成k次最小费用最大流即可! 1: /** 2: 因为e ==0 所以 pe[v] pe[v]^1 是两条相对应的边 3: E[pe[v]].c -= aug; E[pe[v]^ ...

  5. POJ - 2195 最小费用最大流

    题意:每个人到每个房子一一对应,费用为曼哈顿距离,求最小的费用 题解:单源点汇点最小费用最大流,每个人和房子对于建边 #include<map> #include<set> # ...

  6. poj 2195 最小费用最大流模板

    /*Source Code Problem: 2195 User: HEU_daoguang Memory: 1172K Time: 94MS Language: G++ Result: Accept ...

  7. poj 3680(最小费用最大流)

    题目链接:http://poj.org/problem?id=3680 思路:因为N<=200,而区间范围为[1,100000],因此需要离散化,去重,然后就是建图了相连两点连边,容量为k,费用 ...

  8. POJ 2315 最小费用最大流

    从1走到N然后从N走回来的最短路程是多少? 转换为费用流来建模. 1: /** 2: 因为e ==0 所以 pe[v] pe[v]^1 是两条相对应的边 3: E[pe[v]].c -= aug; E ...

  9. POJ 2135 Farm Tour (最小费用最大流模板)

    题目大意: 给你一个n个农场,有m条道路,起点是1号农场,终点是n号农场,现在要求从1走到n,再从n走到1,要求不走重复路径,求最短路径长度. 算法讨论: 最小费用最大流.我们可以这样建模:既然要求不 ...

随机推荐

  1. Linux命令-权限

    Linux命令权限   1.新建用户natasha,uid为1000, gid为555, 备注信息为"master" 2.修改natasha用户的家目录为/Natasha 3.查看 ...

  2. switchysharp设置

    在线规则列表里面插入下面的网址:https://autoproxy-gfwlist.googlecode.com/svn/trunk/gfwlist.txt

  3. c#获取网络时间并同步本地时间

    通过TCP形式来获取NTP时间.主要代码如下: [DllImport("kernel32.dll")] private static extern bool SetLocalTim ...

  4. Frame

    Frame意为框架,是在屏幕上的一个矩形区域. Frame主要作为其他组件的框架基础,或为其他组件提供间距补充. 何时使用Frame组件呢? Frame组件主要用于在复杂的布局中奖其他组件分组,也用于 ...

  5. 一次完败的Release

    一次完败的Release 去年8月份加入一家创业公司,和原同事做VR相关的产品开发,到18年正月初七,总共release过两次,真正经理了一次从0到1的过程.第一次release产品初步成型,大概在1 ...

  6. Python进阶_mysql(1)

    什么是数据库? 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,每个数据库都有一个或多个不同的API用于创建,访问,管理,搜索和复制所保存的数据. 进入mysql (linux ...

  7. 超级好用的前端开发测试Chrome插件-WEB前端助手(FeHelper)

    WEB前端助手(FeHelper)插件概述 WEB前端助手:FeHelper是一款chrome浏览器插件.包含一些前端实用的工具,如字符串编解码.代码美化.JSON格式化查看.二维码生成器.编码规范检 ...

  8. Java 异常基础详解

    目录 1. Java 中的异常 1.1 什么是异常? 1.2 什么是异常处理? 1.2.1 异常处理的优势 1.3 Java 异常类的层次结构 1.4 异常类型 1.5 检查和未检查异常之间的区别 1 ...

  9. 0312-css样式(选择器、文本text、字体fonts、背景background)

    问题: 1.css中table{border:1px:}是定义table的样式,只有表格的外边框,不能实现<table border="1"></table> ...

  10. [LeetCode] Maximum Binary Tree 最大二叉树

    Given an integer array with no duplicates. A maximum tree building on this array is defined as follo ...