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. 在newegg工作的这两个月

    6月11号,接到录用通知后的第二天,来到了Newegg . 作为开发,在本职工作上 1.入职Quick Start: 两周多的入职快速指引,以了解业务,架构为目的. 因为之前一直有用思维导图的习惯,所 ...

  2. jpg图片在IE6、IE7和IE8下不显示解决办法

    坑人的IE浏览器,花了我一个小时才找到原因. 原因:IE内核不能渲染CMYK模式的jpg图片,需要转换为RGB模式. 在photoshop里点击菜单栏—图像—RGB模式就行了 引用:http://lg ...

  3. VHDL----基础知识1

    摘要: 打算分几篇,来理清VHDL的基础知识 ----------------------------------------------------------------------------- ...

  4. 7.FPGA中的同步复位与异步复位

    1.异步复位 always @ ( posedge sclk or negedge s_rst_n ) if ( !s_rst_n ) d_out <= 1'b0; else d_out < ...

  5. QT 十六进制整数变为字符串自动补0 && 十进制补零

    QString str = QString("%1").arg(outChar&0xFF,2,16,QLatin1Char('0')); int a=0001; QStri ...

  6. js 获取随机数

    返回 m 到 n 的随机整数 <script type="text/javascript"> function randomNumber(m.n){ return Ma ...

  7. MVC Controller 链接到 API Controller 以及反向链接

    MVC Controller 链接到 API Controller 以及反向链接 问题 想创建一个从 ASP.NET MVC controller 到 ASP.NET Web API controll ...

  8. NABC需求分析

    我们团队项目为7-magic,在这个七巧板项目中,我们团队的这个项目有许多的特点,我就其中的一个特点:用户可以自主的用七巧板设计自己想象出的图形,并与大家分享. N (Need 需求): 你的创意解决 ...

  9. java线程图

  10. c语言编程之二叉排序树

    二叉排序树,又称为二叉查找树.它是一颗空树,或者是具有下面的性质的二叉树: 1.若它的左子树不空,则左子树上所有节点的值均小于它的根结构的值: 2.若它的右子树不空,则右子树上所有节点的值均大于它的根 ...