题目链接  

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. js常用的数组方法

    1.创建数组的基本方法:  1.1 空数组  var obj=new Array();                 1.2 指定长度数组  var obj=new Array(size);     ...

  2. String、StringBuffer、StringBulider之间的联系和区别

    首先,我们大概总体的解释一下这三者的区别和联系 String的值是不可变的,这就导致每次对String的操作都会生成新的String对象,不仅效率低下,而且大量浪费有限的内存空间. StringBuf ...

  3. windows系统设置虚拟机开机自启并运行虚拟系统

    简述 很多用windows系统电脑开发的童鞋,会在自己电脑上装一个虚拟机,然后在装一个linux系统当作服务器来使用. 但每次电脑开机都要去重启一下虚拟机电源,实在是不划算.下面博主教大家在windo ...

  4. python Flask

    python Flask Flask是一个基于Python开发并且依赖jinja2模板和Werkzeug WSGI服务的一个微型框架,对于Werkzeug本质是Socket服务端,其用于接收http请 ...

  5. 微信小程序开发-IP地址查询-例子

    微信小程序开发  小程序搜索框  IP地址查询  搜索查询  样例 微信小程序 开发 参考   https://mp.weixin.qq.com/debug/wxadoc/dev/component/ ...

  6. packer的基本使用

    工具的产生,一定是为了解决某些痛点,那么痛点是? 你们在工作中是不是经常用到各种云?aliyun, aws, digitalOcean and so on? 你们的规模不大不小,经常去云平台上点一点, ...

  7. glut 深度测试无不起作用问题解决

    OpenGL中使用glEnable(GL_DEPTH_TEST)后深度测试没有起作用,发现深度缓冲没有创建.glut库在兼容模式(GL_COMPATIBILITY_PROFILE)下displaymo ...

  8. mysql事务,视图,权限管理,索引,存储引擎(胖胖老师)

    1: 视图什么是视图    视图是一个虚拟表, 它的内容来源于查询的实表, 本身没有真正的数据;视图的作用    对于复杂的查询时,每次查询时都需要编写一些重复的查询代码让编写sql的效率低下, 为了 ...

  9. ●POJ 2794 Double Patience

    题链: http://poj.org/problem?id=2794题解: 状压DP,概率 9元组表示每一堆还剩几张牌.可以用5进制状压,共5^9=1953124个状态. 令P(S)表示S这个状态被取 ...

  10. 【LSGDOJ 1408】邮局

    题目描述 一些村庄被建在一条笔直的高边公路边上.我们用一条坐标轴来描述这条高边公路,每一个村庄的坐标都是整数.没有两个村庄坐标相同.两个村庄问的距离,定义为它们坐标值差的绝对值. 我们需要在一些村庄建 ...