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

代码:
 #include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;
int total;
const int MAXN = ;
const int INF = ;
struct Edge
{
int u, v, cap, cost;
int next;
} edge[];
int edgenum;
int head[MAXN], dist[MAXN], pre[MAXN];
bool vis[MAXN];
void init()
{
edgenum = ;
memset(head, -, sizeof(head));
}
void addedge(int u, int v, int cap, int cost)
{
edge[edgenum].u = u;
edge[edgenum].v = v;
edge[edgenum].cap = cap;
edge[edgenum].cost = cost;
edge[edgenum].next = head[u];
head[u] = edgenum++;
edge[edgenum].u = v;
edge[edgenum].v = u;
edge[edgenum].cap = ;
edge[edgenum].cost = -cost;
edge[edgenum].next = head[v];
head[v] = edgenum++;
}
bool spfa(int s, int t, int n)//找到一条增广路
{
int i, u, v;
queue <int> qu;
memset(vis, false, sizeof(vis));
memset(pre, -, sizeof(pre));
for(i = ; i <= n; i++) dist[i] = INF;
vis[s] = true;
dist[s] = ;
qu.push(s);
while(!qu.empty())
{
u = qu.front();
qu.pop();
vis[u] = false;
for(i = head[u]; i != -; i = edge[i].next)
{
v = edge[i].v;
if(edge[i].cap && dist[v] > dist[u] + edge[i].cost)
{
dist[v] = dist[u] + edge[i].cost;
pre[v] = i;
if(!vis[v])
{
qu.push(v);
vis[v] = true;
}
}
}
}
if(dist[t] == INF) return false;
return true;
}
int min_cost_max_flow(int s, int t, int n)
{
int flow = ; // 总流量
int i, minflow, mincost;
mincost = ;
while(spfa(s, t, n))
{
minflow = INF + ;
for(i = pre[t]; i != -; i = pre[edge[i].u])
if(edge[i].cap < minflow)
minflow = edge[i].cap;
flow += minflow;
for(i = pre[t]; i != -; i = pre[edge[i].u])
{
edge[i].cap -= minflow;
edge[i^].cap += minflow;
}
mincost += dist[t] * minflow;
}
total = flow; // 最大流
return mincost;
}
int main()
{
int n, m;
int u, v, c;
while (scanf("%d%d", &n, &m) != -)
{
init();
int s = ;
int t = n + ;
while (m--)
{
scanf("%d%d%d", &u, &v, &c);
addedge(u, v , , c);
addedge(v, u , , c);
}
addedge(s, , , );
addedge(n, t, , );
int ans = min_cost_max_flow(s, t, n + );
printf("%d\n", ans);
}
return ;
}

[网络流]Farm Tour(费用流的更多相关文章

  1. poj 2135 Farm Tour 费用流

    题目链接 给一个图, N个点, m条边, 每条边有权值, 从1走到n, 然后从n走到1, 一条路不能走两次,求最短路径. 如果(u, v)之间有边, 那么加边(u, v, 1, val), (v, u ...

  2. 【luogu1251】餐巾计划问题--网络流建模,费用流

    题目描述 一个餐厅在相继的 N 天里,每天需用的餐巾数不尽相同.假设第 iii 天需要 ri​块餐巾( i=1,2,...,N).餐厅可以购买新的餐巾,每块餐巾的费用为 p 分;或者把旧餐巾送到快洗部 ...

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

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

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

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

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

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

  6. poj2135 Farm Tour(费用流)

    Description When FJ's friends visit him on the farm, he likes to show them around. His farm comprise ...

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

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

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

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

  9. poj 2135 Farm Tour 【无向图最小费用最大流】

    题目:id=2135" target="_blank">poj 2135 Farm Tour 题意:给出一个无向图,问从 1 点到 n 点然后又回到一点总共的最短路 ...

随机推荐

  1. 对Array.prototype.slice.call()方法的理解

    在看别人代码时,发现有这么个写法:[].slice.call(arguments, 0),这到底是什么意思呢? 1.基础 1)slice() 方法可从已有的数组中返回选定的元素. start:必需.规 ...

  2. property用法

    用法一 class Test(object): def __init__(self): self.__Num = 100 def setNum(self,Num): print("---se ...

  3. Sqlite之事务

    12.Sqlite事务介绍: 11.android SQLite 批量插入数据慢的解决方案 (针对于不同的android api 版本) ========== 12.Sqlite事务介绍: 应用程序初 ...

  4. 铁板纹理 铁锈Rust

    软件:Substance Designer 2017.1.2 这篇文章记录铁锈的制作方法,铁锈效果见图一 图一:铁锈Rust 铁锈的具体制作过程为: 使用BnW Spots 2(Noise)结点生成噪 ...

  5. 基于CentOS搭建个人Leanote云笔记本

    Leanote 依赖 MongoDB 作为数据存储,下面开始安装MongoDB: 1. 下载启动 MongoDB 下载 MongoDB 进入 /home 目录,并下载 MongoDB: cd /hom ...

  6. 学习 MeteoInfo二次开发教程(三)

    1.breakList的问题 ((PolygonBreak) aLS.breakList[0]).DrawFill=false; 新的类库将LegendScheme的breakList属性改为了Leg ...

  7. matlab-画一个圆

    我们可以用 李萨如图形 的思路去画一个圆,或者一个椭圆. x,y是圆心所在坐标,r是半径,nseg是边缘段数(越高,边缘越顺滑,建议100以上),S是plot的样式设置字符 function Draw ...

  8. SpringBoot 之静态资源

    boot 的默认的静态资源有多个, 由 ResourceProperties 配置了默认值: private static final String[] CLASSPATH_RESOURCE_LOCA ...

  9. android toolbar效果3

    Title居中,只有一个右边按钮 activity_main.xml: <?xml version="1.0" encoding="utf-8"?> ...

  10. python大法好——Python 面向对象

    Python 面向对象  Python从设计之初就已经是一门面向对象的语言,正因为如此,在Python中创建一个类和对象是很容易的. 面向对象技术简介 类(Class): 用来描述具有相同的属性和方法 ...