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. Java日志格式应该是占位符还是字符串拼接

    背景 ​ 上次在群中,有个群友说自己把所有项目中,所有使用占位符打印日志的方式都修改成为了字符串拼接的方式,因为他曾经看了一篇文章,说字符串拼接的形式比占位符形式的性能更好,这个话题引起了大家的广泛讨 ...

  2. Spring和springMVC父子容器的关系

    部分转载自:https://www.cnblogs.com/ljdblog/p/7461854.html springMVC容器和Spring容器 为什么一定要在web.xml中配置spring的li ...

  3. 洛谷 P1281 书的复制

    书的复制 Code: #include <iostream> #include <cstdio> #include <cstring> using namespac ...

  4. Django--对表的操作

    一丶多表创建 1.创建模型 实例:我们来假定下面这些概念,字段和关系 作者模型:一个作者有姓名和年龄. 作者详细模型:把作者的详情放到详情表,包含生日,手机号,家庭住址等信息.作者详情模型和作者模型之 ...

  5. vue+element-ui实现cookie登录

    //效果 //login.vue <template> <div> <el-form :model="ruleForm" :rules="r ...

  6. 未整理js

    函数+对象=方法 方法是动作 有参数的函数=实例 使用new关键字和函数来创建一个实例 var p =new Point(1,1)//平面几何的点 表示遍历的语句样子: for(var i =0; i ...

  7. System Center Configuration Manager 2016 域准备篇(Part4)

    步骤4.创建系统管理容器 注意:在Active Directory域控制器服务器(AD01)上以本地管理员身份执行以下操作 有关您为何这样做的详细信息,请参阅https://docs.microsof ...

  8. 【BZOJ2006】[NOI2010] 超级钢琴(堆+RMQ)

    点此看题面 大致题意: 要你求出区间和前\(k\)大的区间的区间和之和,其中每个区间的大小在\(L\)与\(R\)之间. 堆+\(RMQ\) 这道题目,我们可以先对\(1\sim n\)中的每一个\( ...

  9. 运维如何延续自己的职业生涯--萧田国2017年GOPS深圳站演讲内容

    正如 萧田国在2017年GOPS深圳站演讲所提及的,运维职业生涯规划,应该是T字型. 关于指导原则,正如腾讯好友@赵建春所言: 如果一个领域不能做到TOP,那就是一种伤害. 运维在编程.开发领域,能做 ...

  10. 破解 D-H 协议

    756: 破解 D-H 协议 时间限制: 1 Sec  内存限制: 128 MB提交: 78  解决: 18[提交] [状态] [讨论版] [命题人:admin] 题目描述 Diffie-Hellma ...