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 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用 ...
随机推荐
- Win7如何自定义桌面右键菜单
1 在注册表的HKEY_CLASSES_ROOT\DesktopBackground\Shell\位置,我们新建一个计算器,他的下面有一个项目command,然后这个command去打开计算器(通过分 ...
- iOS wkwebview和 uiwebview 内容图片自适应大小
- (void)webViewDidFinishLoad:(UIWebView *)webView { // 2.都有效果 NSString *js=@"var script = docum ...
- 病毒木马查杀实战第022篇:txt病毒研究
前言 反病毒爱好者们非常喜欢讨论的一个问题就是,现在什么样的病毒才算得上是主流,或者说什么样的病毒才是厉害的病毒呢?我们之前的课程所解说的都是Ring3层的病毒.所以有些朋友可能会觉得.那么Ring0 ...
- /proc/meminfo详解 = /nmon analysis --MEM
memtotal hightotal lowtotal swaptotal memfree highfree lowfree swapfree memshared cached active bigf ...
- .Net 使用的快捷键
快捷键 功能 CTRL + SHIFT + B生成解决方案 CTRL + F7 生成编译 CTRL + O 打开文件 CTRL + SHIFT + O打开项目 CTRL + SHIFT + C显示类视 ...
- 阿里云创建RAM子账号
操作步骤 创建子账号 主账号导航至 访问控制 > 用户管理 页面.如下图所示: 单击右上角的 新建用户,如下图所示: 填写弹出框的各配置项,如下图所示: 单击 确定,即可创建子账号. 允许子账号 ...
- sqlserver获取当月、年的第一天和最后一天
-- 当月第一天select dateadd(month, datediff(month, 0, getdate()), 0) -- 当月最后一天(思路:下月的第一天减去一天)select datea ...
- Atitit .jvm 虚拟机指令详细解释
Atitit .jvm 虚拟机指令详细解释 1. 一.未归类系列A1 2. 数据mov系列2 2.1. 二.const系列2 2.2. 三.push系列2 2.3. ldc系列 该系列命令负责把数值常 ...
- 网页webbrowser
http://www.codeproject.com/Articles/50544/Using-the-WebBrowser-Control-in-ASP-NET/
- ROW模式的SQL无法正常同步的问题总结
转自:http://blog.chinaunix.net/uid-20639775-id-4664792.html#_Toc29623 ROW模式的SQL无法正常同步的问题总结 一. 问题起因.... ...