题目:

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. ajax上传文件以及使用中常见问题处理

    <script src="/scripts/ajaxfileupload.js"></script> <script src="/scrip ...

  2. Python相关机器学习

    Python机器学习库 Python的机器学习库汇总与梳理 机器学习之开源库大总结

  3. 贴一发STL源码

    int my_lower_bound(int size, long long key){    int first = 0, middle;    int half, len;    len = si ...

  4. Bootstrap历练实例:带有下拉菜单的标签和胶囊导航

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  5. XML解析(一) DOM解析

    XML解析技术主要有三种: (1)DOM(Document Object Model)文档对象模型:是 W3C 组织推荐的解析XML 的一种方式,即官方的XML解析技术. (2)SAX(Simple ...

  6. CF-1096C Polygon for the Angle

    CF-1096C Polygon for the Angle https://codeforces.com/contest/1096/problem/C 题意:给一个角度ang(1<=ang&l ...

  7. 【OS_Linux】三大文本处理工具之sed命令

    1.sed命令的简介及用法 sed:即为流编辑器,“stream editor”的缩写.他先将源文件读取到临时缓存区(也叫模式空间)中,再对满足匹配条件的各行执行sed命令.sed命令只针对缓存区中的 ...

  8. Spring容器的理解

    控制反转(IOC,Inversion Of Contorl)和依赖注入(DI,Dependency Injection) IOC是一个宽泛的概念:对某事物的控制权由A转移到B. DI是实现IOC的一种 ...

  9. 我的Python分析成长之路10

    matplot数据可视化基础 制作提供信息的可视化(有时称作绘图)是数据分析中最重要任务之一. 1.图片(画布)与子图 plt.figure :创建一张空白的图片,可以指定图片的大小.像素. figu ...

  10. Python9-函数-day9

    初识函数定义与调用 def my_len(): i = 0 for k in s1: i +=1 return i #返回值 # s = 'tim' s1 = '班主任阿娇' length =my_l ...