题目:

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. (六)maven之本地仓库

     本地仓库 ①    运行机制: 当用户在pom.xml文件中添加依赖jar包时,maven会先从本地仓库查找,如果这个jar包在本地仓库中找不到,就从中央仓库下载到本地仓库,中央仓库是maven默认 ...

  2. JavaScript的语音识别

    有没有想过给您的网站增添语音识别的功能?比如您的用户不用点鼠标,仅仅通过电脑或者手机的麦克风发布命令,比如"下拉到页面底部",或者"跳转到下一页",您的网站就会 ...

  3. ValueError: option names {'--alluredir'} already added 报错

    运行测试用例 import pytest from WXP2P_2.test_data2.login_case import logindata_error1,logindata_error2,log ...

  4. 关于highchts X时间轴比设置时间相差好几个小时的解决

    经过一番查询和研究发现,在曲线图里,x轴的UNIX时间戳是要乘以1000的(通过在线的UNIX转换,结果与原来没有乘以1000的时间戳相差甚远),不然显示的时间会有很大的误差,真是百思不得其解. 另外 ...

  5. Bootstrap CSS概览

    HTML5文档类型(<!DOCTYPE html>) Bootstrap前端框架使用了HTML5和CSS属性,为了让这些能正常工作,您需要使用HTML5文档类型(<!DOCTYPE ...

  6. java在线聊天项目0.7版 连接多个客户端问题,开启多个客户端后服务器端只接收到一个 对各种异常的补充处理

    问题的原因是 while(connected) { String str=dis.readUTF(); System.out.println(str); } 不断循环执行,一直在死循环获取socket ...

  7. 第六次作业 :使用Excel制作成绩单

  8. (转发)IOS高级开发~Runtime(一)

    IOS高级开发-Runtime(一) IOS高级开发-Runtime(二) IOS高级开发-Runtime(三) IOS高级开发-Runtime(四) 一些公用类: @interface Custom ...

  9. PAT 乙级 1013

    题目 题目地址:PAT 乙级 1013 思路 审题没把范围看清楚,没一次AC 题中m和n都表示第几个素数,范围是10000,所以查询的数组中需要的素数量至少10000,所以需要计算大概2~120000 ...

  10. [HIHO] 1050 树中的最长路

    #1050 : 树中的最长路 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上回说到,小Ho得到了一棵二叉树玩具,这个玩具是由小球和木棍连接起来的,而在拆拼它的过程中, ...