描述

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(最小费用最大流)的更多相关文章

  1. Farm Tour(最小费用最大流模板)

    Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18150   Accepted: 7023 Descri ...

  2. POJ2135 Farm Tour —— 最小费用最大流

    题目链接:http://poj.org/problem?id=2135 Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  3. poj 2351 Farm Tour (最小费用最大流)

    Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17230   Accepted: 6647 Descri ...

  4. poj 2135 Farm Tour 最小费用最大流建图跑最短路

    题目链接 题意:无向图有N(N <= 1000)个节点,M(M <= 10000)条边:从节点1走到节点N再从N走回来,图中不能走同一条边,且图中可能出现重边,问最短距离之和为多少? 思路 ...

  5. POJ 2135 Farm Tour [最小费用最大流]

    题意: 有n个点和m条边,让你从1出发到n再从n回到1,不要求所有点都要经过,但是每条边只能走一次.边是无向边. 问最短的行走距离多少. 一开始看这题还没搞费用流,后来搞了搞再回来看,想了想建图不是很 ...

  6. [poj] 1235 Farm Tour || 最小费用最大流

    原题 费用流板子题. 费用流与最大流的区别就是把bfs改为spfa,dfs时把按deep搜索改成按最短路搜索即可 #include<cstdio> #include<queue> ...

  7. hdu 1853 Cyclic Tour 最小费用最大流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1853 There are N cities in our country, and M one-way ...

  8. 网络流(最小费用最大流):POJ 2135 Farm Tour

    Farm Tour Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: ...

  9. POJ 2135 Farm Tour (网络流,最小费用最大流)

    POJ 2135 Farm Tour (网络流,最小费用最大流) Description When FJ's friends visit him on the farm, he likes to sh ...

随机推荐

  1. confusing c++ 重写 与 重定义 记录1

    class parent { public: void f() { cout << "parent f()" << endl; } void f(int i ...

  2. css-选择器性能

    ID选择器 比如#header 类选择器 比如.promo 元素选择器 比如 div 兄弟选择器 比如 h2 + p 子选择器 比如 li > ul 后代选择器 比如 ul a 7. 通用选择器 ...

  3. 尚硅谷springboot学习21-web开发-处理静态资源

    SpringBoot对静态资源的映射规则 @ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFi ...

  4. Django添加ckeditor富文本编辑器

    源码 https://github.com/django-ckeditor/django-ckeditor 通过pip安装. pip3 install django-ckeditor pip3 ins ...

  5. jQuery图片延迟加载插件:jquery.lazyload

    ----------------------------------------------------------------------------------------------- clas ...

  6. Structs复习 通配符

    1.jar包 web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version= ...

  7. ROC,AUC,Precision,Recall,F1的介绍与计算(转)

    1. 基本概念 1.1 ROC与AUC ROC曲线和AUC常被用来评价一个二值分类器(binary classifier)的优劣,ROC曲线称为受试者工作特征曲线 (receiver operatin ...

  8. 显卡安装一直循环在登录界面——解决之-T450安装显卡驱动和cuda7.5发现的一些问题

    今天,在笔记本T450上,要装zed双目相机的驱动,需要显卡模块和cuda7.5,使用了三种方式,才成功. 1.使用 sudo ubuntu-drivers devices 来查看显卡支持驱动版本,因 ...

  9. linux 挂载磁盘

    挂在磁盘操作(还有一个300G的盘没显示出来): [root@iZgo67bo9s3uaijzqrgbaxZ ori]# df -h  Filesystem            Size  Used ...

  10. https://www.oschina.net/project/zhlist/326/scripting 开源

    1https://www.oschina.net/project/zhlist/326/scripting