题目链接: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. XML工程配置文件的读写

    TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译,使用TinyXML进行C++ XML解析,使用简单,容易上手.这个解析库的模型通过解析XML文件, ...

  2. 01分数规划zoj2676(最优比例,最小割集+二分)

    ZOJ Problem Set - 2676         Network Wars Time Limit: 5 Seconds      Memory Limit: 32768 KB      S ...

  3. [Reprint]C++普通函数指针与成员函数指针实例解析

    这篇文章主要介绍了C++普通函数指针与成员函数指针,很重要的知识点,需要的朋友可以参考下   C++的函数指针(function pointer)是通过指向函数的指针间接调用函数.相信很多人对指向一般 ...

  4. Ejb: remote调用

    用的是wildfly 9.0.2 一:在myeclipse中新建wildfly 9.0.2的server(如何新建去网上搜) 二:修改wildfly server的argument(在run conf ...

  5. PHP内核探索:哈希碰撞攻击是什么?

    最近哈希表碰撞攻击(Hashtable collisions as DOS attack)的话题不断被提起,各种语言纷纷中招.本文结合PHP内核源码,聊一聊这种攻击的原理及实现. 哈希表碰撞攻击的基本 ...

  6. Verilog篇(一)

    Verilog在行为级建模时常用到的一些函数,变量等. 1:$random(seed),每次根据seed的值产生一个32位的有符号数,seed的数据类型必须是寄存器(reg),整形(integer), ...

  7. session与cookie的讲解

    session_start();//开启session http,无状态性 记录状态SESSION COOKIE SESSION :存储在服务端(器)的:每个人存一份:可以存储任意类型的数据:默认过期 ...

  8. 台电幻彩u盘拆解

  9. jQuery基础之让出$,与其他库共存

    在某些情况下,可能有必要在同一个页面中使用多个JS库,由于很多库都使用$标识符(因为他简短方便),因此就需要一种方式来避免名称冲突. 为解决这个问题,jQuery提供了一个名叫.noConflict( ...

  10. dumpbin使用

    声明一点:Win7系统,安装的是VS2010 dumpbin.exe位于C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin目录下. 初 ...