题目:

Farm Tour
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 16898   Accepted: 6543

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,费用为边的长度,新建源点 s 向 1 连一条容量 2 费用0 的边,新建汇点 t,N 向 t 连一条容量为 2 费用为 0 的边,则题目就转化为求从 s 到 t 的最小费用最大流问题。

代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<ctime>
#include<cctype>
#include<algorithm>
#include<queue>
using namespace std;
const int N=;
const int M=;
const int inf=0x3f3f3f3f;
queue<int> que;
int n,m,ans=;
int first[],next[],go[],rest[],cost[],dis[],tot=;
bool visit[],work[];
int src,des;
void combin(int u,int v,int r,int w)
{
next[++tot]=first[u],first[u]=tot,go[tot]=v,rest[tot]=r,cost[tot]=w;
next[++tot]=first[v],first[v]=tot,go[tot]=u,rest[tot]=,cost[tot]=-w;
}
void init(int n,int m)
{
src=,des=n+;
for(int i=;i<=m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
combin(u,v,,w);
combin(v,u,,w);
}
combin(src,,,);
combin(n,des,,);
}
bool spfa()
{
memset(dis,inf,sizeof(dis));
memset(work,false,sizeof(work));
int u;
que.push(src),dis[src]=;
while(!que.empty())
{
u=que.front(),que.pop();
visit[u]=false;
for(int e=first[u];e;e=next[e])
{
int v=go[e];
if(rest[e]&&dis[u]+cost[e]<dis[v])
{
dis[v]=dis[u]+cost[e];
if(!visit[v])
{
que.push(v);
visit[v]=true;
}
}
}
}
return dis[des]<inf;
}
int dinic(int u,int flow)
{
if(u==des)
{
ans+=flow*dis[des];
return flow;
}
work[u]=true;
int res=,temp,v,e;
for(e=first[u];e;e=next[e])
{
if(!work[v=go[e]]&&rest[e]&&dis[v]==dis[u]+cost[e])
{
temp=dinic(v,min(rest[e],flow-res));
if(temp)
{
rest[e]-=temp,rest[e^]+=temp;
res+=temp;
if(res==flow) break;
}
}
}
return res;
}
int maxflow()
{
while(spfa()) dinic(src,inf);
return ans;
}
int main()
{
//freopen("a.in","r",stdin);
scanf("%d%d",&n,&m);
init(n,m);
cout<<maxflow()<<endl;
return ;
}

算法复习——费用流模板(poj2135)的更多相关文章

  1. 二分图带权匹配 KM算法与费用流模型建立

    [二分图带权匹配与最佳匹配] 什么是二分图的带权匹配?二分图的带权匹配就是求出一个匹配集合,使得集合中边的权值之和最大或最小.而二分图的最佳匹配则一定为完备匹配,在此基础上,才要求匹配的边权值之和最大 ...

  2. HDU2686 费用流 模板

    Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  3. HDU 6611 K Subsequence(Dijkstra优化费用流 模板)题解

    题意: 有\(n\)个数\(a_1\cdots a_n\),现要你给出\(k\)个不相交的非降子序列,使得和最大. 思路: 费用流建图,每个点拆点,费用为\(-a[i]\),然后和源点连边,和后面非降 ...

  4. 初识费用流 模板(spfa+slf优化) 餐巾计划问题

    今天学习了最小费用最大流,是网络流算法之一.可以对于一个每条边有一个容量和一个费用(即每单位流的消耗)的图指定一个源点和汇点,求在从源点到汇点的流量最大的前提下的最小费用. 这里讲一种最基础也是最好掌 ...

  5. hdu1533 费用流模板

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

  6. BZOJ3291Alice与能源计划——匈牙利算法+模拟费用流

    题目描述 在梦境中,Alice来到了火星.不知为何,转眼间Alice被任命为火星能源部长,并立刻面临着一个严峻的考验.为 了方便,我们可以将火星抽象成平面,并建立平面直角坐标系.火星上一共有N个居民点 ...

  7. 费用流模板(带权二分图匹配)——hdu1533

    /* 带权二分图匹配 用费用流求,增加源点s 和 汇点t */ #include<bits/stdc++.h> using namespace std; #define maxn 1000 ...

  8. [BZOJ1937][SHOI2004]Mst最小生成树(KM算法,最大费用流)

    1937: [Shoi2004]Mst 最小生成树 Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 802  Solved: 344[Submit][Sta ...

  9. BZOJ2557[Poi2011]Programming Contest——匈牙利算法+模拟费用流

    题目描述 Bartie and his friends compete in the Team Programming Contest. There are n contestants on each ...

随机推荐

  1. Redis学习笔记(一)五种数据类型

    1.字符串(String) 基本操作:SET(设置).GET(获取).DEL(删除)其他操作传送门 root@localhost:~# redis-cli > set msg hello OK ...

  2. Sqlserver 2012 Always on技术

    使用了Sqlserver 2012 Always on技术后,假如采用的配置是默认配置,会出现Primary server CPU很高的情况发生,比如默认配置如下: 需要自定义来解决这个问题. 我们先 ...

  3. POJ 2152 Fire (树形DP,经典)

    题意:给定一棵n个节点的树,要在某些点上建设消防站,使得所有点都能够通过某个消防站解决消防问题,但是每个点的建站费用不同,能够保证该点安全的消防站的距离上限也不同.给定每个点的建站费用以及最远的消防站 ...

  4. 图片,二进制,oracle数据库

    图片在oracle数据库中一般以二进制存在,存储类型是blob,然而clob类型一般存储的是大于4000的字符,不能用来存储图像这样的二进制内容,下面展示一下实现图像,二进制,oracle 数据库的应 ...

  5. winform中让显示的图片覆盖到父窗体保持父窗体的不可选中的状态,且任务栏中不会显示子窗体的任务选项

    要求:为父窗体添加一个类似于加载等待的功能,当窗体点击备份时弹出且覆盖掉窗体 问题一产生:当为弹窗添加控件时,form.show();导致窗体卡死,控件变得透明化; 问题一分析:当窗体show();之 ...

  6. JavaScript异步仿同步(控制流)的实现

    在前端开发中尤其是在nodejs开发中经常会遇到这样的场景(以ajax为例):有3个(或者更多个)Ajax请求,并且第2个请求依赖于第1个,第3个请求依赖于第2个,那我们可能就会在发第一个Ajax后回 ...

  7. java反序列化字节转字符串工具

    https://github.com/NickstaDB/SerializationDumper SerializationDumper-v1.1.jar 用法 : java -jar Seriali ...

  8. html输入框去除记忆功能

    自动完成功能,只需把AUTOCOMPLETE设为off即可,如: 整个表单禁止自动完成 HTML code <FORM method=post action="submit.asp&q ...

  9. JS设置组合快捷键

    为提升用户体验,想要在web页面中通过组合快捷键调出用户帮助页面,具体实现思路是监听keyup事件,在相应的处理函数中进行逻辑编写,代码如下 $(document).keyup(function (e ...

  10. CF-1100 E Andrew and Taxi

    CF-1100E Andrew and Taxi https://codeforces.com/contest/1100/problem/E 知识点: 二分 判断图中是否有环 题意: 一个有向图,每边 ...