题目链接:http://poj.org/problem?id=2135

Farm Tour
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 14821   Accepted: 5657

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

 
代码几乎是找的,太难了,我以后就用模板吧,不过还是要把模板理解清楚。
题意:不走重边,1->n->1的最少步数。
分析:见注释。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue> using namespace std;
int sumFlow;
const int MAXN = ;
const int MAXM = ;
#define INF 0x3f3f3f3f struct Edge
{
int u;
int v;
int cap;
int cost;
int next;
} edge[MAXM<<]; int NE;
int head[MAXN], dist[MAXN], pre[MAXN];
bool vis[MAXN]; void addedge(int u,int v,int cap,int cost)
{
edge[NE].u=u;
edge[NE].v=v;
edge[NE].cap=cap;
edge[NE].cost=cost;
edge[NE].next=head[u];
head[u]=NE++; edge[NE].u=v; ///反的容量图
edge[NE].v=u;
edge[NE].cap=;
edge[NE].cost=-cost;
edge[NE].next=head[v];
head[v]=NE++;
} bool SPFA(int s,int t,int n)
{
int i,u,v;
queue<int>Q;
memset(vis,false,sizeof(vis));
memset(pre,-,sizeof(pre));
for(i=; i<=n; i++)
dist[i]=INF;
vis[s]=true;
dist[s]=;
Q.push(s);
while(!Q.empty())
{
u=Q.front();
Q.pop();
vis[u]=false;
for(i=head[u]; i!=-; i=edge[i].next)
{
v=edge[i].v;
if(edge[i].cap&&dist[v]>dist[u]+edge[i].cost)
{
dist[v]=dist[u]+edge[i].cost;
pre[v]=i;
if(!vis[v])
{
Q.push(v);
vis[v]=true;
}
}
}
}
if(dist[t]==INF)
return false;
return true;
} int MCMF(int s,int t,int n)
{
int flow=; /// 总流量
int minflow,mincost;
mincost=;
while(SPFA(s,t,n))
{
minflow=INF+; ///容量瓶颈
for(int i=pre[t]; i!=-; i=pre[edge[i].u])
if(edge[i].cap<minflow)
minflow=edge[i].cap;
flow+=minflow;
for(int i=pre[t]; i!=-; i=pre[edge[i].u])
{
edge[i].cap-=minflow;
edge[i^].cap+=minflow; ///更新残余网络,^1处理,因为0,是正边,1是反边,做^1,就能找到反边
}
mincost+=dist[t]*minflow; ///这里和书上有点不一样,就是相当于一个合并同类项了,dist[t]到汇点的最少花费*瓶颈容量
}
sumFlow=flow; /// 最大流
return mincost;
} int main()
{
int n,m;
int u,v,c;
while(~scanf("%d%d",&n,&m))
{
NE=;
memset(head,-,sizeof(head));
int S=;
int T=n+;
while(m--)
{
scanf("%d%d%d",&u,&v,&c);
addedge(u,v,,c);
addedge(v,u,,c); ///建反图,这样在SPFA里面就能回来了,
}
addedge(S,,,);
addedge(n,T,,);
int ans=MCMF(S,T,T+);
printf("%d\n",ans);
}
return ;
}
 

Poj(2135),MCMF,模板的更多相关文章

  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. HIT 2715 - Matrix3 - [最小费用最大流][数组模拟邻接表MCMF模板]

    题目链接:http://acm.hit.edu.cn/hoj/problem/view?id=2715 Time limit : 5 sec Memory limit : 64 M Zhouguyue ...

  4. Poj 2187 凸包模板求解

    Poj 2187 凸包模板求解 传送门 由于整个点数是50000,而求凸包后的点也不会很多,因此直接套凸包之后两重循环即可求解 #include <queue> #include < ...

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

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

  6. POJ 2195 - Going Home - [最小费用最大流][MCMF模板]

    题目链接:http://poj.org/problem?id=2195 Time Limit: 1000MS Memory Limit: 65536K Description On a grid ma ...

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

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

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

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

  9. POJ - 2135最小费用流

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

随机推荐

  1. C++动态内存分配

    C++动态内存分配1.堆内存分配 :C/C++定义了4个内存区间:代码区,全局变量与静态变量区,局部变量区即栈区,动态存储区,即堆(heap)区或自由存储区(free store). 堆的概念:通常定 ...

  2. [转] git 常用命令

    查看.添加.提交.删除.找回,重置修改文件 git help <command> # 显示command的help git show # 显示某次提交的内容 git show $id gi ...

  3. VS2010程序打包

    今天,小白就来给各位做个打包的新手教程,此文仅是为了记录自己的学习过程与方便其他初次接触的打包的朋友们总结一下,希望大家能够受用.废话不多说,下面我们就来讲解下打包工程.首先,在项目中添加一个安装项目 ...

  4. 变形--扭曲 skew()

    变形--扭曲 skew() 扭曲skew()函数能够让元素倾斜显示.它可以将一个对象以其中心位置围绕着X轴和Y轴按照一定的角度倾斜.这与rotate()函数的旋转不同,rotate()函数只是旋转,而 ...

  5. paper 93:OpenCV学习笔记大集锦

    整理了我所了解的有关OpenCV的学习笔记.原理分析.使用例程等相关的博文.排序不分先后,随机整理的.如果有好的资源,也欢迎介绍和分享. 1:OpenCV学习笔记 作者:CSDN数量:55篇博文网址: ...

  6. 夺命雷公狗---微信开发54----微信js-sdk接口开发(1)之快速入门

    js-sdk基本介绍 除去服务号的九大接口外,微信提供了JS-SDK接口,所谓JS-SDK接口也就是在网页中使用javascript来更改网页设置, (比如隐藏右上角的菜单)获取用户状态(比如地理位置 ...

  7. vscode icon in elementary os

    chmod +x /home/shenfeng/vscode/code /home/shenfeng/vscode/code sudo ln -s /home/shenfeng/vscode/code ...

  8. 想学习一下CSS函数

    好像原来都是用前后端代码实现的功能,如今CSS3已经吸纳为标准,使用简单的选择器就可以实现了.

  9. WebService优点和缺点小结(转)

      一.什么是WebService? 实际上,WebService的主要目标是跨平台的可互操作性.为了达到这一目标,WebService完全基于XML(可扩展标记语言).XSD (XMLSchema) ...

  10. [置顶] lua 进阶3--lua文件中调用C++函数

    前面讲了一下,C++读取lua文件中的变量,包括一维表.二维表这些,这节讲一下如何在lua文件中去调用C++函数 C++代码如下 #include <stdio.h> extern &qu ...