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 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用 ...
随机推荐
- 协程基础_context系列函数
近期想看看协程,对这个的详细实现不太了解.查了下,协程最常规的做法就是基于makecontext,getcontext,swapcontext这类函数在用户空间切换用户上下文. 所以在这通过样例代码尽 ...
- spring mvc跨域设置(全局)
//--------------第一步//spring 5版本全局配置方式 @Configuration @EnableWebMvc public class SpringMvcBeans imple ...
- 用Volley-nullpointerexception
public Request(int method, String url, Response.ErrorListener listener) { mMethod = method; mUrl = u ...
- The Unix Tools Are Your Friends
The Unix Tools Are Your Friends Diomidis Spinellis IF, ON MY WAY TO EXILE ON A DESERT ISLAND, I had ...
- Spring HibernateTemplate
HibernateTemplate利用模板设计模式,可将重复的opensession getcurrentsession工作省去,只将必要操作执行即可,其它的由spring来帮我们处理. < ...
- C语言-EOF和feof()判断文件结尾的区别
今天获取一个图片内容时, fopen("aaaaaa.png", "r"), 读取完文件头就停止了, 后来模式改为 "rb" 就可以了, 特 ...
- 符合BME风格的弹窗\菜单\表格\文件上传控件
1.弹窗 2.菜单 3.表格 4.文件上传控件 笔记补充......
- arm linux c++11编译
include(CheckCXXCompilerFlag) CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11 ...
- iOS机型适配
机型变化 坐标:表示屏幕物理尺寸大小,坐标变大了,表示机器屏幕尺寸变大了: 像素:表示屏幕图片的大小,跟坐标之间有个对应关系,比如1:1或1:2等: ppi:代表屏幕物理大小到图片大小的 ...
- 打包Cocos2d-xproject为PC项目
<1>第一步,得到总体的大.exe 1.复制cocos2d-x-2.2文件下的Release.win32文件侠到桌面. 2.将项目下的Resources里的资源拷贝到Release.win ...