Farm Tour(最小费用最大流模板)
Farm Tour
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 18150 | Accepted: 7023 |
Description
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
* Lines 2..M+1: Three space-separated integers that define a path: The starting field, the end field, and the path's length.
Output
Sample Input
4 5
1 2 1
2 3 1
3 4 1
1 3 2
2 4 2
Sample Output
6
Source
//# include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
# define eps 1e-
# define INF 0x3f3f3f3f
# define pi acos(-1.0)
# define MXN
# define MXM struct Edge{
int from, to, flow, cost, cap;
}edges[MXM*]; //有向边数*2 struct MCMF{
int n, m, idx; //点,边,边数
int flow, cost;
vector<int> G[MXN]; //记录边
int inq[MXN]; //BFS用
int dis[MXN]; //层次
int pre[MXN]; //上一条弧
int adf[MXN]; //增量 void Init(){
idx=;
for (int i=;i<=n+;i++) G[i].clear(); //有附加点时要注意
} void Addedge(int u,int v,int cost,int cap){
edges[idx++] = (Edge){u,v,,cost,cap};
edges[idx++] = (Edge){v,u,,-cost,};
G[u].push_back(idx-);
G[v].push_back(idx-);
} int Bellman(int s, int t)
{
memset(dis,0x3f,sizeof(dis)); //有附加点时要注意
memset(inq,,sizeof(inq));
dis[s]=, inq[s]=, adf[s]=INF;
queue<int> Q;
Q.push(s);
while (!Q.empty())
{
int u = Q.front(); Q.pop();
inq[u]=;
for (int i=;i<(int)G[u].size();i++)
{
Edge &e = edges[G[u][i]];
if (dis[e.to] > dis[u] + e.cost && e.cap > e.flow)
{
dis[e.to] = dis[u] + e.cost;
adf[e.to] = min(adf[u], e.cap-e.flow);
pre[e.to] = G[u][i];
if (!inq[e.to])
{
Q.push(e.to);
inq[e.to]=;
}
}
}
}
if (dis[t]==INF) return false; flow+=adf[t];
cost+=adf[t]*dis[t];
int x=t;
while(x!=s)
{
edges[pre[x]].flow+=adf[t];
edges[pre[x]^].flow-=adf[t];
x=edges[pre[x]].from;
}
return true;
} int MinCost(int s,int t)
{
flow = , cost = ;
while(Bellman(s, t));
return cost;
}
}F; int main()
{
scanf("%d%d",&F.n,&F.m);
F.Init();
for (int i=;i<=F.m;i++)
{
int u,v,cost;
scanf("%d%d%d",&u,&v,&cost);
F.Addedge(u,v,cost,);
F.Addedge(v,u,cost,);
}
F.Addedge(, , , );
F.Addedge(F.n, F.n+, , );
printf("%d\n",F.MinCost(,F.n+));
return ;
}
Farm Tour(最小费用最大流模板)的更多相关文章
- TZOJ 1513 Farm Tour(最小费用最大流)
描述 When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 &l ...
- POJ2135 Farm Tour —— 最小费用最大流
题目链接:http://poj.org/problem?id=2135 Farm Tour Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- poj 2351 Farm Tour (最小费用最大流)
Farm Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17230 Accepted: 6647 Descri ...
- poj 2135 Farm Tour 最小费用最大流建图跑最短路
题目链接 题意:无向图有N(N <= 1000)个节点,M(M <= 10000)条边:从节点1走到节点N再从N走回来,图中不能走同一条边,且图中可能出现重边,问最短距离之和为多少? 思路 ...
- POJ 2135 Farm Tour [最小费用最大流]
题意: 有n个点和m条边,让你从1出发到n再从n回到1,不要求所有点都要经过,但是每条边只能走一次.边是无向边. 问最短的行走距离多少. 一开始看这题还没搞费用流,后来搞了搞再回来看,想了想建图不是很 ...
- [poj] 1235 Farm Tour || 最小费用最大流
原题 费用流板子题. 费用流与最大流的区别就是把bfs改为spfa,dfs时把按deep搜索改成按最短路搜索即可 #include<cstdio> #include<queue> ...
- 图论算法-最小费用最大流模板【EK;Dinic】
图论算法-最小费用最大流模板[EK;Dinic] EK模板 const int inf=1000000000; int n,m,s,t; struct node{int v,w,c;}; vector ...
- HDU3376 最小费用最大流 模板2
Matrix Again Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)To ...
- 洛谷P3381 最小费用最大流模板
https://www.luogu.org/problem/P3381 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用 ...
随机推荐
- ETL技术入门之ETL初认识
ETL是什么 ETL是Extract Transform Load三个英文单词的缩写 中文意思就是抽取.转换.载入.说到ETL就必须提到数据仓库. 先说下背景知识: 信息是现代企业的重要资源,是企业运 ...
- win10 为了对电脑进行保护,已经阻止此应用 解决方法
win10 为了对电脑进行保护,已经阻止此应用 解决方法 正确方法: ."win+x"进入控制面板,选择安全性与维护,在左侧更改windows smartscreen筛选器 ...
- c++解释--百度百科
c++ C++是在C语言的基础上开发的一种面向对象编程语言,应用广泛:C++支持多种编程范式 --面向对象编程.泛型编程和过程化编程.最新正式标准C++于2014年8月18日公布.[1] 其编程领域 ...
- json.Decoder vs json.Unmarshal
128down voteaccepted It really depends on what your input is. If you look at the implementation of t ...
- 移动端H5实现图片上传
移动端H5实现图片上传 https://segmentfault.com/a/1190000010034177
- CentOS7下利用init.d启动脚本实现tomcat开机自启动
在之前的博文中已经对CentOS7下通过tomcat进行WEB系统的发布进行了介绍,今天将利用init.d启动脚本,将服务脚本加入到开机启动服务队列,实现tomcat服务的开机启动. 1. 环境准备 ...
- iOS_绘制带删除线的Label
效果图例如以下: 一个带删除线的文本标签,继承自UILabel 自绘代码过程例如以下: 1,重写控件的drawRect方法 2,首先得到上下文对象 3,设置颜色,并指定是填充(Fill)模式还是笔刷( ...
- Aurora学习笔记连载一:仿真平台搭建
由于公司项目需要,需要学习Aurora协议,才有了这样的连载学习笔记,也算是对自己学习的一份记录吧. 对于Aurora是什么,大家自行百度. 当然,Kevin也在此先提醒大家,本套学习笔记不是你想学就 ...
- Html打印需要内容块(Js实现)
首先在head里面加入下面一段js代码: function preview(oper) { ) { bdhtml = window.document.body.innerHTML; //获取当前页的h ...
- SpringMVC请求使用@PathVariable获取文件名称并且文件名中存在.导致路径被截取的问题
在SpringMVC中,当使用@pathVariable通过Get请求获取路径名称时,如果路径名称上存在小数点,则获取不到小数点后面的内容,会被Spring截取. 比如我获取某一文件,路径是local ...