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. java中异常处理finally和return语句的执行顺序

    finally代码块的语句在return之前一定会得到执行 如果try块中有return语句,finally代码块没有return语句,那么try块中的return语句在返回之前会先将要返回的值保存, ...

  2. SQLite -创建表

    SQLite -创建表 SQLite CREATE TABLE语句用于创建一个新表在任何给定的数据库.创建一个基本表包括表命名和定义其列,每列的数据类型 语法: CREATE TABLE语句的基本语法 ...

  3. 2015 AlBaath Collegiate Programming Contest(2月14日训练赛)

    A (By ggg): 题意:一个人还有x秒到红绿灯,这个红绿灯有g秒绿灯,y秒黄 灯,r秒红灯,问你到红绿灯的时候是什么灯.值得注意的是绿 灯变黄灯时,第g秒是黄灯了. B (By Anxdada) ...

  4. core下的routelink

    core mvc中 routelink返回和 framework mvc中返回的不一样,core中返回 IHtmlContent, 而 fw 中返回 MvcHtmlString 在写分页方法中用到了r ...

  5. vscode vue template 下 style 的样式自动提示 #bug 这个搞完vue语法esLint就又不好使了,ERR

    网上都是 "*.vue": "vue",改成"*.vue": "html" 就ok了   "files.ass ...

  6. python day two

    今日内容: 1.常用数据类型及内置方法 2.文件处理 3.函数 列表类型: 定义: 在[]内,可以存放多个任意类型的值,并以逗号隔开. 一般用于存放学生的爱好,课堂的周期等等... 优先掌握的操作: ...

  7. Java ArrayList中去掉相同的元素并保留相同元素中的最后一个

    实现思路:将list对象循环两次,拿外层数据和里面的数据对比,一样的删除外层(外层元素肯定比内存的靠前),如果一样的话,删除外层数据,这样最后输出外层数据的list,结果就能保证唯一性,并且保留了后面 ...

  8. [BZOJ3211]:花神游历各国(小清新线段树)

    题目传送门 题目描述: 花神喜欢步行游历各国,顺便虐爆各地竞赛.花神有一条游览路线,它是线型的,也就是说,所有游历国家呈一条线的形状排列,花神对每个国家都有一个喜欢程度(当然花神并不一定喜欢所有国家) ...

  9. poj2368 Buttons

    题目描述 题解: 非常简单的巴什博弈问题. 简单来说保证$L+1$是$K$的因数即可. 决策是,先手取$x$个,后手就取$L+1-x$个. 那个$L>=2$真的很坑. 代码: #include& ...

  10. spring踩坑

    org.springframework.web.util.NestedServletException: Request processing failed; nested exception is ...