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. python-paramiko初体验

    什么pexpect.pxshll在paramiko面前都是浮云,重要的是paramiko支持windows. 小试牛刀 import paramiko paramiko.util.log_to_fil ...

  2. Codeforces Round #271 (Div. 2) D.Flowers DP

    D. Flowers   We saw the little game Marmot made for Mole's lunch. Now it's Marmot's dinner time and, ...

  3. jenkins对接gitlab和git

    1 需要的插件 jenkins的git插件和jenkins的gitlab插件. 2 对接gitlab 在系统配置中,随便起一个连接的名字,设置url,可以直接用ip地址,端口号默认是80,不需要写明. ...

  4. QT信号槽与Delphi事件的对比

    最近学QT,对信号槽机制感到有点新鲜: QObject::connect(slider, SIGNAL(valueChanged(int)), lcd, SLOT(display(int))); 自己 ...

  5. Android-通过SlidingPaneLayout高仿微信6.2最新版手势滑动返回(一)

    近期更新了微信版本号到6.2.发现里面有个很好的体验,就是在第二个页面Activity能手势向右滑动返回,在手势滑动的过程中能看到第一个页面,这样的体验很赞,这里高仿了一下. 这里使用的是v4包里面的 ...

  6. AAC的AudioSpecificConfig细节

    AAC格式里有个复杂的AudioSpecificConfig, 在FLV格式里称为AAC sequence header.在正式播放ADTS AAC数据包之前,需要用AudioSpecificConf ...

  7. JSP-Runoob:JSP 表单处理

    ylbtech-JSP-Runoob:JSP 表单处理 1.返回顶部 1. JSP 表单处理 我们在浏览网页的时候,经常需要向服务器提交信息,并让后台程序处理.浏览器中使用 GET 和 POST 方法 ...

  8. P2533 [AHOI2012]信号塔

    传送门 据说是一个叫做随机增量法的东西 枚举\(i\),如果不在圆中将它设为圆心 枚举\(j\),如果不在圆中将\((i,j)\)成为新的圆的直径 枚举\(k\),如果不在圆中让\(i,j,k\)组成 ...

  9. 简单理解jsonp原理

    对于javascript程序员来说,发送ajax请求获取后台数据然后把数据和模板拼接成字符串渲染回DOM实现无刷新更新页面这样的操作可谓是轻车熟路.但众所周知,ajax有一个不好,就是不能跨域传输数据 ...

  10. C# 截取字符串——

    string  strID ="NODE_aSDFghsdfgyuhjidfgh_45678" //得到_ 中间的数 int index = strID.IndexOf(" ...