Farm Tour
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 19207   Accepted: 7441

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

解析 题目是无向图 求两条最短路径的长度 ,两条路径不允许有相交边。其实就是求一条最短路,把边删掉,再找一条。其实可以转化为网络流来写。

无向图把我们建边要建两次ab,ba;ba,ab(模板可以有平行边)   把每个边的容量设为1 花费为边的长度。然后找两次增广路退出来的花费就是答案。

也可以建立超源点汇点 0,n+1   建立两条边  0—1 容量为2 花费为0   n—n+1 容量为2 花费为0   跑一边最小费用就是答案了

锦囊:

以下板子求的是最小费用最大流 如果要固定流量k 可以在增广的时候检查一下 在flow+a>=k 的时候只增广k-flow单位的流量,然后终止程序。同样有费用限制也一样。

 #include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<queue>
using namespace std;
const int maxn=1e3+,mod=1e9+,inf=0x3f3f3f3f;
struct edge
{
int to,next,cap,flow,cost;
} edge[maxn*maxn];
int head[maxn],tol;
int pre[maxn],dis[maxn];
bool vis[maxn];
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].flow=;
edge[tol].cost=cost;
edge[tol].next=head[u];
head[u]=tol++;
edge[tol].to=u;
edge[tol].cap=;
edge[tol].flow=;
edge[tol].cost=-cost;
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]=false;
pre[i]=-;
}
dis[s]=;
vis[s]=true;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=false;
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]=true;
q.push(v);
}
}
}
}
if(pre[t]==-) return false;
else return true;
}
int mincostflow(int s,int t,int &cost)
{
int flow=;
cost=;
while(spfa(s,t))
{
int Min=inf;
for(int i=pre[t]; i!=-; i=pre[edge[i^].to]) //更新流量
{
if(Min>edge[i].cap-edge[i].flow)
Min=edge[i].cap-edge[i].flow;
}
for(int i=pre[t]; i!=-; i=pre[edge[i^].to])
{
edge[i].flow+=Min;
edge[i^].flow-=Min;
cost+=edge[i].cost*Min;
}
flow+=Min;
// if(flow>=f)break; //达到想要的f直接弹出来
}
return flow;
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
init(n+);
for(int i=; i<m; i++)
{
int u,v,cap,cost;
scanf("%d%d%d",&u,&v,&cost);
addedge(u,v,,cost);
addedge(v,u,,cost);
}
addedge(,,,);
addedge(n,n+,,);
int ans,maxflow;
maxflow=mincostflow(,n+,ans);
printf("%d\n",ans);
}
}

POJ 2135 最小费用最大流 入门题的更多相关文章

  1. hdu 1533 Going Home 最小费用最大流 入门题

    Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  2. poj 2135最小费用最大流

    最小费用最大流问题是经济学和管理学中的一类典型问题.在一个网络中每段路径都有"容量"和"费用"两个限制的条件下,此类问题的研究试图寻找出:流量从A到B,如何选择 ...

  3. POJ 2135 最小费用最大流

    题目链接 Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18961   Accepted: 7326 D ...

  4. POJ2135 最小费用最大流模板题

    练练最小费用最大流 此外此题也是一经典图论题 题意:找出两条从s到t的不同的路径,距离最短. 要注意:这里是无向边,要变成两条有向边 #include <cstdio> #include ...

  5. 【网络流#2】hdu 1533 - 最小费用最大流模板题

    最小费用最大流,即MCMF(Minimum Cost Maximum Flow)问题 嗯~第一次写费用流题... 这道就是费用流的模板题,找不到更裸的题了 建图:每个m(Man)作为源点,每个H(Ho ...

  6. 2018牛客网暑期ACM多校训练营(第五场) E - room - [最小费用最大流模板题]

    题目链接:https://www.nowcoder.com/acm/contest/143/E 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...

  7. poj 3422(最小费用最大流)

    题目链接:http://poj.org/problem?id=3422 思路:求从起点到终点走k次获得的最大值,最小费用最大流的应用:将点权转化为边权,需要拆点,边容量为1,费用为该点的点权,表示该点 ...

  8. POJ 2516 最小费用最大流

    每一种货物都是独立的,分成k次最小费用最大流即可! 1: /** 2: 因为e ==0 所以 pe[v] pe[v]^1 是两条相对应的边 3: E[pe[v]].c -= aug; E[pe[v]^ ...

  9. POJ - 2195 最小费用最大流

    题意:每个人到每个房子一一对应,费用为曼哈顿距离,求最小的费用 题解:单源点汇点最小费用最大流,每个人和房子对于建边 #include<map> #include<set> # ...

随机推荐

  1. Azure ARMTemplate模板,VM扩展命令

    Azure ARM模板中,给虚拟机安装扩展脚本的命令 "resources": [ { "apiVersion": "[variables('apiV ...

  2. 一条陌生的出路【过往d心声】

    一条陌生的出路 Vashon的心声 人生就像一列车,车上总有形形色色的人穿梭往来.你也可能会在车上遇到很多你以为有缘分的人,但是车也会有停下来的时候,总会有人从人生这列车上上下下,当你下去的时候你挥挥 ...

  3. python实战教程之自动扫雷(自己存下来学习之用)

    3.python的第三方库win32api,win32gui,win32con,Pillow,numpy,opencv可通过 pip install --upgrade SomePackage 来进行 ...

  4. nginx 的编译安装及基本操作

    下载nginx [root@nginx ~]# wget http://nginx.org/download/nginx-1.14.0.tar.gz --2019-05-02 21:52:23-- h ...

  5. Spring自动注入的几种方式

    ---恢复内容开始---   @Service("accountEmailService")public class AccountEmailServiceImpl impleme ...

  6. 配置maven报错 the java_home environment variable is not defined correctly ......

    the java_home environment variable is not defined correctly This environment variable is needed to r ...

  7. 教你学会Linux/Unix下的vi文本编辑器

    vi编辑器是Unix/Linux系统管理员必须学会使用的编辑器.看了不少关于vi的资料,终于得到这个总结. 首先,记住vi编辑器的两个模式:1.命令模式 2.编辑模式. 在一个UNIX/Linux的s ...

  8. vue 父子组件的加载顺序

    一.加载渲染过程 父beforeCreate->父created->父beforeMount->子beforeCreate->子created->子beforeMount ...

  9. IO之Object流举例

    import java.io.*; public class TestObjectIO { public static void main(String args[]) throws Exceptio ...

  10. python3.x Day6 协程

    协程:#定义来自牛人alex博客协程,又称微线程,纤程.英文名Coroutine.一句话说明什么是线程:协程是一种用户态的轻量级线程.协程拥有自己的寄存器上下文和栈.协程调度切换时,将寄存器上下文和栈 ...