TZOJ 1513 Farm Tour(最小费用最大流)
描述
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.
输入
* 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.
输出
A single line containing the length of the shortest tour.
样例输入
4 5
1 2 1
2 3 1
3 4 1
1 3 2
2 4 2
样例输出
6
题意
N个点M条路,M行每行u,v,w,计算从1到N回到1,所有边只能走一次求最短路,保证有解
题解
每条边流量为1,花费为w,设源点S=1,汇点T=n+1流量为2,花费为0
求S到T的最小费用最大流
代码
#include<bits/stdc++.h>
using namespace std; const int N=1e4+;
const int M=1e5+;
const int INF=0x3f3f3f3f; int FIR[N],TO[M],CAP[M],FLOW[M],COST[M],NEXT[M],tote;
int pre[N],dist[N],q[];
bool vis[N];
int n,m,S,T;
void init()
{
tote=;
memset(FIR,-,sizeof(FIR));
}
void add(int u,int v,int cap,int cost)
{
TO[tote]=v;
CAP[tote]=cap;
FLOW[tote]=;
COST[tote]=cost;
NEXT[tote]=FIR[u];
FIR[u]=tote++; TO[tote]=u;
CAP[tote]=;
FLOW[tote]=;
COST[tote]=-cost;
NEXT[tote]=FIR[v];
FIR[v]=tote++;
}
bool SPFA(int s, int t)
{
memset(dist,INF,sizeof(dist));
memset(vis,false,sizeof(vis));
memset(pre,-,sizeof(pre));
dist[s] = ;vis[s]=true;q[]=s;
int head=,tail=;
while(head!=tail)
{
int u=q[++head];vis[u]=false;
for(int v=FIR[u];v!=-;v=NEXT[v])
{
if(dist[TO[v]]>dist[u]+COST[v]&&CAP[v]>FLOW[v])
{
dist[TO[v]]=dist[u]+COST[v];
pre[TO[v]]=v;
if(!vis[TO[v]])
{
vis[TO[v]] = true;
q[++tail]=TO[v];
}
}
}
}
return pre[t]!=-;
}
void MCMF(int s, int t, int &cost, int &flow)
{
flow=cost=;
while(SPFA(s,t))
{
int Min=INF;
for(int v=pre[t];v!=-;v=pre[TO[v^]])
Min=min(Min, CAP[v]-FLOW[v]);
for(int v=pre[t];v!=-;v=pre[TO[v^]])
{
FLOW[v]+=Min;FLOW[v^]-=Min;
cost+=COST[v]*Min;
}
flow+=Min;
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
for(int i=,u,v,w;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
add(u,v,,w);
add(v,u,,w);
}
S=,T=n+;
add(n,T,,);
int cost,flow;
MCMF(S,T,cost,flow);
printf("%d\n",cost);
}
return ;
}
TZOJ 1513 Farm Tour(最小费用最大流)的更多相关文章
- Farm Tour(最小费用最大流模板)
Farm Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18150 Accepted: 7023 Descri ...
- 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> ...
- hdu 1853 Cyclic Tour 最小费用最大流
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1853 There are N cities in our country, and M one-way ...
- 网络流(最小费用最大流):POJ 2135 Farm Tour
Farm Tour Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: ...
- POJ 2135 Farm Tour (网络流,最小费用最大流)
POJ 2135 Farm Tour (网络流,最小费用最大流) Description When FJ's friends visit him on the farm, he likes to sh ...
随机推荐
- 通过指定的 url 去网络或者文件服务器下载文件到本地某个文件夹
/** * 从网络Url中下载文件 * @param urlStr 指定的url * @param fileName 下载文件到本地的名字 * @param savePath 本地保存下载文件的路径 ...
- python 阿狸的进阶之路(4)
装饰器 #1.开放封闭原则:对扩展开放,对修改是封闭#2.装饰器:装饰它人的,器指的是任意可调用对象,现在的场景装饰器->函数,被装饰的对象也是->函数#原则:1.不修改被装饰对象的源代码 ...
- jquery 设计的扩展---初级
1. 写一个构造函数G,调用G 时,返回G上的fn 对象的init() 的实例 2.设置G.fn 的指向,使用G.fn 与G.prototype指向同一个对象 2.1 重写G.prototype 对象 ...
- openvpn 批量生成用户脚本
#/bin/bash for user in "$@" do echo "新增用户:$user" if [ -d "/etc/openvpn/clie ...
- RH_KABI_RESERVE的使用
struct mm_struct { .......... #if defined(__GENKSYMS__) || !defined(CONFIG_SPAPR_TCE_IOMMU) /* We're ...
- jenkins+docker+docker-compose完整发版流程
首先搭建jenkins+maven+nexus这一套自动化打包工具,并配置好相应配置,这里就不再赘述了. 其次,搭建好docker集群和私有仓库,以及安装好docker-compose工具,配置好相应 ...
- jQuery链式语法演示
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Android 深入浅出 - Android系统启动过程
Activity的类继承关系及跟踪Activity的启动 Android系统启动过程 https://study.163.com/course/courseLearn.htm?courseId=213 ...
- 手机移动端web前端常见问题整理
移动端常见问题及解决方案 一.meta基础知识 H5页面窗口自动调整到设备宽度,并禁止用户缩放页面 <meta name="viewport" content="w ...
- SQL Server 生成 数据字典 / 数据库文档
1. 工具生成 2.SQL语句生成 参考地址:http://blog.csdn.net/qq289523052/article/details/22174721 1.在 表 上右键 - 扩展属性 - ...