Farm Tour
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10840   Accepted: 4011

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

Source

 
最小费用流
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue> using namespace std; const int MAX_N = ;
const int INF = 1e9;
struct Edge {int from, to, cap, flow, cost; };
int N,M;
vector<Edge> edges;
vector<int> G[MAX_N];
int dist[MAX_N],prevv[MAX_N],preve[MAX_N];
bool inq[MAX_N]; void add_edge(int from, int to, int cap, int cost) {
edges.push_back(Edge {from, to, cap, , cost});
edges.push_back(Edge {to, from, , , -cost});
int m = edges.size();
G[from].push_back(m - );
G[to].push_back(m - );
} int min_cost_flow(int s, int t, int f) {
int res = ;
while(f > ) {
fill(dist, dist + N, INF);
dist[s] = ;
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 && dist[e.to] > dist[u] + e.cost) {
dist[e.to] = dist[u] + e.cost;
prevv[e.to] = u;
preve[e.to] = i;
if(!inq[e.to]) {
inq[e.to] = ;
q.push(e.to);
}
}
} }
if(dist[t] == INF) {
return -;
} int d = f;
for (int v = t; v != s; v = prevv[v]) {
Edge& e = edges[ G[ prevv[v] ][preve[v]] ];
d = min(d,e.cap - e.flow);
} //printf("d = %d dis = %d\n",d,dist[t]);
f -= d;
res += d * dist[t]; for (int v = t; v != s; v = prevv[v]) {
Edge& e = edges[ G[prevv[v]][ preve[v] ] ];
e.flow += d;
edges[ G[prevv[v]][ preve[v] ] ^ ].flow -= d;
} } return res;
} int main()
{
//freopen("sw.in","r",stdin); scanf("%d%d",&N,&M);
for(int i = ; i <= M; ++i) {
int a,b,cost;
scanf("%d%d%d",&a,&b,&cost);
add_edge(a - ,b - ,,cost);
add_edge(b - ,a - ,,cost);
} printf("%d\n",min_cost_flow(,N - ,));
//cout << "Hello world!" << endl;
return ;
}

POJ 2135的更多相关文章

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

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

  2. POJ 2135 Farm Tour (网络流,最小费用最大流)

    POJ 2135 Farm Tour (网络流,最小费用最大流) Description When FJ's friends visit him on the farm, he likes to sh ...

  3. poj 2135 Farm Tour 【无向图最小费用最大流】

    题目:id=2135" target="_blank">poj 2135 Farm Tour 题意:给出一个无向图,问从 1 点到 n 点然后又回到一点总共的最短路 ...

  4. Poj(2135),MCMF,模板

    题目链接:http://poj.org/problem?id=2135 Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  5. POJ 2135 Farm Tour (费用流)

    [题目链接] http://poj.org/problem?id=2135 [题目大意] 有一张无向图,求从1到n然后又回来的最短路 同一条路只能走一次 [题解] 题目等价于求从1到n的两条路,使得两 ...

  6. POJ - 2135最小费用流

    题目链接:http://poj.org/problem?id=2135 今天学习最小费用流.模板手敲了一遍. 产生了一个新的问题:对于一条无向边,这样修改了正向边容量后,反向边不用管吗? 后来想了想, ...

  7. 网络流(最小费用最大流):POJ 2135 Farm Tour

    Farm Tour Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: ...

  8. POJ 2135.Farm Tour 消负圈法最小费用最大流

    Evacuation Plan Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4914   Accepted: 1284   ...

  9. POJ 2135 Farm Tour 最小费用流

    两条路不能有重边,既每条边的容量是1.求流量为2的最小费用即可. //#pragma comment(linker, "/STACK:1024000000,1024000000") ...

  10. 网络流 poj 2135

    n个点 m条边 给m条边 求1->n n->1 最小花费,每条边最多走一次 两个最短路显然不行 会影响另外一条 #include<stdio.h> #include<al ...

随机推荐

  1. Linq操作

    Linq使用Group By 1 1.简单形式: var q = from p in db.Products group p by p.CategoryID into g select g; 语句描述 ...

  2. opengl基础学习专题 (三) 多边形绘制的几种样式

    题外话 聪明人之所以不会成功,是由于他们缺乏坚韧的毅力. ——艾萨克·牛顿(1643年1月4日—1727年3月31日)英国 也许可以理解为 想更深一步的时候,坚持,努力和聪明缺一不可. 挺直腰杆在此向 ...

  3. ZLG_GUI配置与函数介绍

    http://www.docin.com/p-825479457.html ZLG_GUI配置与函数介绍

  4. 自定义Drawable

    本文由 伯乐在线 - treesouth 翻译,toolate 校稿.未经许可,禁止转载! 英文出处:ryanharter.com.欢迎加入翻译小组. 我们看过一些博客文章,讲述了为什么要适时地使用自 ...

  5. 34.pad designer警告

    1.Drill hole size is equal or larger than smallest pad size. Pad will be drilled away 原因:钻孔直径太大,直接把p ...

  6. mysql卸载注意事项

    由于数据库软件十分的复杂,不管是Mysql还是sqlserver安装都有很多配置要选择. 假若你第一次安装数据库失败,然后又想卸载,又再次安装,这时可能由于你第一次的卸载不完全,会导致你第二次安装时出 ...

  7. 微软职位内部推荐-Software Engineer II-News

    微软近期Open的职位: News is a critical areas for integration of mobile and services, one of the top priorit ...

  8. Android -- NDK开发入门

    第一步,建立一个普通的Android项目HelloNDK,然后在与src同一级的目录下新建一个jni目录: 第二步,在jni目录下新建一个hello_ndk.c文件,代码如下: #include &l ...

  9. 典型用户 persona

    persona 典型用户 1.姓名:王涛 2.年龄:22 3.收入:基本无收入 4.代表用户在市场上的比例和重要性:王涛为铁道学生.本软件的用户主要是学生和老师,尤其是广大的铁大学子,所以此典型用户的 ...

  10. text-overflow 与 word-wrap:设置使用一个省略标记...标示对象内文本的溢出。

    text-overflow 与 word-wrap text-overflow用来设置是否使用一个省略标记(...)标示对象内文本的溢出. 语法: 但是text-overflow只是用来说明文字溢出时 ...