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. 【J2EE】Hibernate

    Hibernate是面向Java环境的对象/关系数据库映射工具,管理Java应用和数据库之间的映射关系,提供数据查询和获取数据的方法,可以大幅减少使用JDBC处理数据持久化的时间. 使用Eclipse ...

  2. EMVTag系列12《卡片内部风险管理数据》

    Ø  9F53    连续脱机交易限制数(国际-货币) L: 1 -C(有条件):如果执行国际-货币频度检查 PBOC专有数据元. 不使用指定应用货币的连续脱机交易次数最大数,超过后交易请求联机 模板 ...

  3. 分布式缓存Memcached

       分布式缓存服务器,既然用到数据缓存很明显就是想高效性的获取数据,大容量的存储数据.为了可以缓存大量的数据以及可以高效获取数据,那么分布式缓存数据库就要解决数据可以水平线性扩展,这样可以扩大数据容 ...

  4. <转载>编程珠玑-位排序(bitsort)

    转载:http://www.cnblogs.com/shuaiwhu/archive/2011/05/29/2065039.html  维护版权   在<编程珠玑>一书上,有一题是将一堆不 ...

  5. windows phone版的一个儿教app

    昨天下午看见一个园友写的一篇关于儿教的api,看了也就两三个接口,所以数据处理应该不会太复杂,主要是界面的效果,要求可能比较高.于是我就重新自己写了一个app,实现很简单,花的时间比较多的地方应该是在 ...

  6. mongoDB 3.0 安全权限访问控制

    MongoDB3.0权限,啥都不说了,谷歌百度出来的全是错的.先安装好盲沟,简单的没法说. 首先,不使用 —auth 参数,启动 mongoDB: mongodb-linux-i686-3.0.0/b ...

  7. iOS 进阶 第二十二天(0603)

    0603 block\运行时 block block的本质是一个指向结构体的指针. 运行时 要分析clang命令反编译出来的c++代码,就要把一些小括号删掉来分析.因为这些小括号一般都是类型强转. o ...

  8. python关于字典的使用方法

    #-*- coding:utf-8 -*-#Author:gxli#定义字典id_db={ 233333199211222342:{ 'name':'xiaoa', 'age':23, 'addr': ...

  9. 微软职位内部推荐-SDEII

    微软近期Open的职位: Software Engineer II for Customer Experience (Level 62+) Location: Suzhou Contact Perso ...

  10. android 开发怎么让程序生成的图片文件不会被系统扫描到

    我们在写应用的时候,可能会保存很多图片,大的小的,仅仅是我们的应用中会用到,处于种种原因不希望用户看到,我是觉着如果被用户看到了,就失去了我的应用的那一层神秘的面纱,用户是米有闲情逸致去打开你一层层的 ...