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

题意:节点1把所有的节点都要跑一遍,然后回到节点1,每个边智能跑一次,问最短距离
思路:流量为2的最小费用流
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<stack>
#include<cstdlib>
#include <vector>
#include<queue>
using namespace std; #define ll long long
#define llu unsigned long long
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
const int maxn = 1e4+;
const int mod = 1e9+; typedef pair<int,int>P;
struct edge{
int to,cap,cost,rev;
}; int V;
vector<edge> G[maxn];
int h[maxn];
int dist[maxn];
int prevv[maxn],preve[maxn]; void addedge(int from,int to,int cap,int cost)
{
G[from].push_back((edge){to,cap,cost,G[to].size()});
G[to].push_back((edge){from,,-cost,G[from].size() -});
} int min_cost_flow(int s,int t,int f)
{
int res =;
fill(h,h+V,);
while (f>)
{
priority_queue<P,vector<P>,greater<P> > que;
fill(dist,dist+V,INF);
dist[s] =;
que.push(P(,s));
while(!que.empty())
{
P p=que.top();
que.pop();
int v=p.second;
if(dist[v]<p.first)
continue;
for(int i=;i<G[v].size();i++) {
edge &e = G[v][i];
if(e.cap> && dist[e.to]>dist[v]+e.cost+h[v]-h[e.to])
{
dist[e.to] = dist[v] + e.cost + h[v] -h[e.to];
prevv[e.to] = v;
preve[e.to] = i;
que.push(P(dist[e.to],e.to));
}
} }
if(dist[t] == INF)
{
return -;
}
for(int v=;v<V;v++)
h[v] += dist[v];
int d=f;
for(int v=t;v!=s;v=prevv[v])
{
d=min(d,G[prevv[v]][preve[v]].cap);
}
f -= d;
res += d*h[t];
for(int v=t;v!=s;v=prevv[v])
{
edge &e = G[prevv[v]][preve[v]];
e.cap -= d;
G[v][e.rev].cap += d;
}
}
return res;
} int N,M;
int a[maxn],b[maxn],c[maxn]; void solve()
{
int s=,t=N-;
V=N;
for(int i=;i<M;i++)
{
addedge(a[i]-,b[i]-,,c[i]);
addedge(b[i]-,a[i]-,,c[i]);
}
printf("%d\n",min_cost_flow(s,t,));
} int main()
{
cin>>N>>M;
for(int i=;i<M;i++)
{
scanf("%d%d%d",&a[i],&b[i],&c[i]);
}
solve();
}

Farm Tour POJ - 2135 (最小费用流)的更多相关文章

  1. POJ - 2135最小费用流

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

  2. POJ 2135 /// 最小费用流最大流 非负花费 BellmanFord模板

    题目大意: 给定一个n个点m条边的无向图 求从点1去点n再从点n回点1的不重叠(同一条边不能走两次)的最短路 挑战P239 求去和回的两条最短路很难保证不重叠 直接当做是由1去n的两条不重叠的最短路 ...

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

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

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

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

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

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

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

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

  7. POJ Farm Tour

    Farm Tour 题目: 约翰有N块地,家在1号,而N号是个仓库.农场内有M条道路(双向的),道路i连接这ai号地和bi号地,长度为ci. 约翰希望依照从家里出发,经过若干地后达到仓库.然后再返回家 ...

  8. poj 2351 Farm Tour (最小费用最大流)

    Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17230   Accepted: 6647 Descri ...

  9. Poj(2135),MCMF,模板

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

随机推荐

  1. <Linux系统isosize指令用法>

    isosize命令:iso9660文件系统大小显示 isosize命令用于显示iso9660文件系统的大小,还文件可以使普通文件,也可以是块设备,如/dev/sr0或者/dev/sda.如果没有相关选 ...

  2. C++中的引用和指针

    引用和指针有何区别?何时只能使用指针而不能使用引用?    引用是一个别名,不能为 NULL 值,不能被重新分配:指针是一个存放地址的变量.当需要对变量重新赋以另外的地址或赋值为 NULL 时只能使用 ...

  3. Unity C# Sting.Format的学习

    String.Format (String, Object) 将指定的 String 中的格式项替换为指定的 Object 实例的值的文本等效项. String.Format (String, Obj ...

  4. MySQL JOIN | 联结

    联结是利用SQL的SELECT能执行的最重要的操作.为了提高存储的有效性和避免数据冗余,往往会将有关联的数据存储在好几张表中,那么怎样用一条SELECT语句就能检索出这些数据呢? 答案是JOIN(联结 ...

  5. The eighth day

    time n(名词):时间:次,时代,时刻: vt(及物动词):为...安排时间:测定...的时间:调准(机械的速度): vi(不及物动词):合拍,和谐,打拍子 files (原型是fly) vi(不 ...

  6. Javascript Events

    事件通常与函数配合使用,这样就可以通过发生的事件来驱动函数执行. 事件句柄 html4.0的新特性之一是有能力使html事件触发浏览器中的动作action,比如当用户点击某个html元素时启动一段Ja ...

  7. 使用FusionCharts创建可更新数据的JavaScript图表

    先创建一个简单的图表,然后改变它的数据(请参见下面的代码).图表最初据显示8月份的销售数据,当用户点击按钮时改为显示9月份的销售数据.每个月都有单独的XML文件,代码如下: <html> ...

  8. silverlight数据绑定模式TwoWay,OneWay,OneTime的研究

    asp.net开发中,数据绑定是一个很简单的概念,控件与数据绑定后,控件可以自动把数据按一定的形式显示出来.(当然控件上的值改变后,可以通过提交页面表单,同时后台服务端代码接收新值更新数据) silv ...

  9. php-7.1.11编译选项配置

    ./configure \ --prefix=/usr/local/php-7.1.11 \ --with-config-file-path=/usr/local/php7.1.11/etc \ -- ...

  10. 配置Python环境变量

    虽然是老问题了,现在安装都自动配置环境变量. 这里,我是在VS2017中安装的Python3.6,但是没有自动配置好环境变量. 配置Python环境变量 打开[此电脑]—[属性]—[高级系统设置]—[ ...