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 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用 ...
随机推荐
- C# 0-1背包问题
0-1背包问题 0-1背包问题基本思想: p[i,j]表示在前面i个物品总价值为j时的价值最大值.str[i, j]表示在前面i个物品总价值为j时的价值最大值时的物品重量串. i=0 或者j=0时: ...
- ThreadLocal的简单使用(读书笔记)
从ThreadLocal的名字上可以看到,这是一个线程局部变量,也就是说,只有当前线程可以访问,既然是只有当前线程可以访问的数据,自然是线程安全的. public class ThreadL ...
- Django的信号机制详解
Django提供一种信号机制.其实就是观察者模式,又叫发布-订阅(Publish/Subscribe) .当发生一些动作的时候,发出信号,然后监听了这个信号的函数就会执行. Django内置了一些信号 ...
- CSAPP:异常控制流
在一般的情况下,处理器处理的指令序列是相邻的(顺序执行). 异常控制流提供了指令的跳转,它一部分是由硬件实现的,一部分是由操作系统实现的. 异常处理 在系统启动时,操作系统分配和初始化一张称为异常表的 ...
- IOS 多播委托(GCDMulticastDelegate)
原文:http://www.cnblogs.com/dagehaoshuang/p/4043264.html 在IOS中为了实现回调一般有如下几个方法: delegate 通知中心 block KVO ...
- http Referrer-Policy
Referrer-Policy: no-referrer Referrer-Policy: no-referrer-when-downgrade Referrer-Policy: origin Ref ...
- Mybatis(二):Mybatis的映射文件sqlmapper详解
MyBatis 真正的力量是在映射语句中.这里是奇迹发生的地方.对于所有的力量,SQL 映射的 XML 文件是相当的简单.当然如果你将它们和对等功能的 JDBC 代码来比较,你会发现映射文件节省了大约 ...
- Atitit.js的键盘按键事件捆绑and事件调度
Atitit.js的键盘按键事件捆绑and事件调度 1. Best的方法还是 objEvtMap[ id+evt ]=function(evt,element) 2. Event bind funct ...
- Vivado 与 Modelsim 联合仿真
1 编译库 用命令行 用vivado工具 vivado 有很多 IP核的接口 已经与 ISE的核 不太一样了,比如fir ,接口就是这样的: fir_lp fir_lp_ip( .aclk ( ...
- jpeg错误打印结构
struct jpeg_error_mgr { /* Error exit handler: does not return to caller */ JMETHOD(void, error_exit ...