Farm Tour POJ - 2135

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

题意很简单,说思路;

设源点为s,汇点为t;

然后建图的时候,s->1   容量为2,费用为0;

n->t,容量为2,费用为0;

这样就能从s出发,到t,然后再走不重复的路径的再回到s;

由于s->1,和n->t这两条路径费用为0,所以不影响最后结果。

这样直接跑一边最大费用最小流的模板就行了。

详见代码:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;
const int MAXX=;
const int INF=0x3f3f3f3f; struct node
{
int to;
int next;
int cap;
int flow;
int cost;
}edge[MAXX]; int head[MAXX],tol;
int pre[MAXX],dis[MAXX];
bool vis[MAXX];
int N; void init(int n)
{
N=n;
tol=;
memset(head,-,sizeof(head));
} void addedge(int u,int v,int cap,int cost)
{
edge[tol].to=v;
edge[tol].cap=cap;
edge[tol].cost=cost;
edge[tol].flow=;
edge[tol].next=head[u];
head[u]=tol++;
edge[tol].to=u;
edge[tol].cap=;
edge[tol].cost=-cost;
edge[tol].flow=;
edge[tol].next=head[v];
head[v]=tol++;
} bool SPFA(int s,int t)
{
queue<int> q;
for(int i=;i<N;i++)
{
dis[i]=INF;
vis[i]=;
pre[i]=-;
}
dis[s]=;
vis[s]=;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=;
for(int i=head[u];i!=-;i=edge[i].next)
{
int v=edge[i].to;
if(edge[i].cap>edge[i].flow&&dis[v]>dis[u]+edge[i].cost)
{
dis[v]=dis[u]+edge[i].cost;
pre[v]=i;
if(!vis[v])
{
vis[v]=;
q.push(v);
}
}
}
}
if(pre[t]==-)return ;
return ;
} int minCostMaxFlow(int s,int t)
{
int cost=;
while(SPFA(s,t))
{
int minn=INF;
for(int i=pre[t];i!=-;i=pre[edge[i^].to])
{
if(minn>edge[i].cap-edge[i].flow)
minn=edge[i].cap-edge[i].flow;
}
for(int i=pre[t];i!=-;i=pre[edge[i^].to])
{
edge[i].flow+=minn;
edge[i^].flow-=minn;
cost+=edge[i].cost*minn;
}
}
return cost;
}
int main()
{ int n,m;
while(~scanf("%d%d",&n,&m))
{
int u,v,g;
init(n+);
for(int i=;i<m;i++)
{ scanf("%d%d%d",&u,&v,&g);
addedge(u,v,,g);
addedge(v,u,,g);
}
addedge(,,,);
addedge(n,n+,,);
printf("%d\n",minCostMaxFlow(,n+));
}
return ;
}

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

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

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

  2. POJ 2135 Farm Tour (费用流)

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

  3. poj 2135 Farm Tour 最小费最大流

    inf开太小错了好久--下次还是要用0x7fffffff #include<stdio.h> #include<string.h> #include<vector> ...

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

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

  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 2135 Farm Tour(最小费用最大流)

    Description When FJ's friends visit him on the farm, he likes to show them around. His farm comprise ...

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

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

  9. POJ-2135 Farm Tour---最小费用最大流模板题(构图)

    题目链接: https://vjudge.net/problem/POJ-2135 题目大意: 主人公要从1号走到第N号点,再重N号点走回1号点,同时每条路只能走一次. 这是一个无向图.输入数据第一行 ...

  10. POJ 2135 Farm Tour [最小费用最大流]

    题意: 有n个点和m条边,让你从1出发到n再从n回到1,不要求所有点都要经过,但是每条边只能走一次.边是无向边. 问最短的行走距离多少. 一开始看这题还没搞费用流,后来搞了搞再回来看,想了想建图不是很 ...

随机推荐

  1. 已知目标qps跟并发用户数20,压测平均响应时间实例

    Jmeter性能测试案例(一) 转:https://blog.csdn.net/lovesoo/article/details/78579547 测试需求:测试20个用户访问网站在负载达到30QPS时 ...

  2. [转]Android程序框架设计

    这篇文章主要内容来自于之前我讲的一个PPT文档,现在将其整理如下.欢迎指正.以下的内容都是来自于我自身的经验,欢迎大家多提自己的建议. 1.一些概念 模式的定义: 每个模式都描述了一个在我们的环境中不 ...

  3. Java程序执行超时——Future接口介绍

    在Java中,如果需要设定代码执行的最长时间,即超时,可以用Java线程池ExecutorService类配合Future接口来实现. Future接口是Java标准API的一部分,在java.uti ...

  4. 为何Google这类巨头会认为敏捷开发原则是废话?

    [编者按]这是一个来自Quora的问题.Rocket程序员Jasmine Adamson在文中表达了敏捷开发原则是废话的观点,他觉得现实生活中没有什么人会推崇这些原则来工作,不过他们仍然在说其所做的是 ...

  5. SQL service 自动解决依赖包 验证

    依赖关系解决 ============================================================================================= ...

  6. 特征选择--->卡方选择器

    特征选择(Feature Selection)指的是在特征向量中选择出那些“优秀”的特征,组成新的.更“精简”的特征向量的过程.它在高维数据分析中十分常用,可以剔除掉“冗余”和“无关”的特征,提升学习 ...

  7. php settype()和gettype()

    gettype()是获得变量的类型,settype()函数用来配置或转换变量类型.成功返回 true 值,其它情形返回 false 值.参数 var 为原来的变量名,参数 type 为下列的类型之一: ...

  8. U3D版本控制设置 Force Text优劣

    git的忽略列表 .gitignore Library/ Temp/ .vs/ *.csproj *.sln Edit > Project Settings > Editor 下把meta ...

  9. 浅析SpringDataJpa继承结构

    一.SpringDataJpa的含义: SpringDataJpa: 是Spring基于ORM框架.JPA规范封装的一套JPA应用框架,是SpringData中的一个子模块,可让开发者用极简的代码即可 ...

  10. Java初级进阶中高级工程师必备技能

    很多人学了javase以为自己学的已经很OK了,但是其实javase里边有很多的知识点是你不知道的,不管你找的是哪里的javase的视频,大多数是不会讲这些东西,而这些东西你平时业务又不会主动去接触, ...