POJ 2135 最小费用最大流
Farm Tour
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18961 Accepted: 7326 Description
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.
Input
* 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.
Output
A single line containing the length of the shortest tour.Sample Input
4 5
1 2 1
2 3 1
3 4 1
1 3 2
2 4 2Sample Output
6
题意:有n个点,m条边,无向图,问你1->n->1的最短路径是多少。FJ带朋友参观,想经可能多看几个地方,所以每条边只能走一次。
思路:把路径长度作为费用,流量为1,这样每条路就只能走一次了。自己建源点s,汇点t。s->1流量2,n->t流量2,因为这两条边要走两次。然后直接费用流。水题,改改板子就过了,学会费用流后的第二题。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue> using namespace std;
const int MAXN = ;
const int MAXM = ;
const int INF = 0x7FFFFFFF; int n, m, first[MAXN], s, t, sign; int max_flow, min_cost; struct Edge {
int to, cap, cost, next;
} edge[MAXM * ]; inline void init() {
for(int i = ; i <= n + ; i++ ) {
first[i] = -;
}
sign = ;
} inline void add_edge(int u, int v, int cap, int cost) {
edge[sign].to = v, edge[sign].cap = cap, edge[sign].cost = cost;
edge[sign].next = first[u], first[u] = sign ++;
edge[sign].to = u, edge[sign].cap = , edge[sign].cost = -cost;
edge[sign].next = first[v], first[v] = sign ++;
} int dist[MAXN], inq[MAXN], pre[MAXN], incf[MAXN]; bool spfa(int s, int t) {
for(int i = ; i <= n + ; i++ ) {
dist[i] = INF, inq[i] = ;
}
queue<int>que;
que.push(s), inq[s] = , dist[s] = ;
incf[s] = 0x3f3f3f3f;
while(!que.empty()) {
int now = que.front();
que.pop();
inq[now] = ;
for(int i = first[now]; ~i; i = edge[i].next) {
int to = edge[i].to, cap = edge[i].cap, cost = edge[i].cost;
if(cap > && dist[to] > dist[now] + cost) {
dist[to] = dist[now] + cost;
incf[to] = min(incf[now], cap);
pre[to] = i;
if(!inq[to]) {
que.push(to);
inq[to] = ;
}
}
}
}
return dist[t] != INF;
} void update(int s, int t) {
int x = t;
while(x != s) {
int pos = pre[x];
edge[pos].cap -= incf[t];
edge[pos ^ ].cap += incf[t];
x = edge[pos ^ ].to;
}
max_flow += incf[t];
min_cost += dist[t] * incf[t];
} void minCostMaxFlow(int s, int t) {
while(spfa(s, t)) {
update(s, t);
}
} int main()
{
while(~scanf("%d %d", &n, &m)) {
s = , t = n + ;
init();
for(int i = ; i <= m; i++ ) {
int u, v, cap, cost;
scanf("%d %d %d", &u, &v, &cost);
add_edge(u, v, , cost);
add_edge(v, u, , cost);
}
add_edge(s, , , );
add_edge(n, t, , );
max_flow = min_cost = ;
minCostMaxFlow(s, t);
printf("%d\n", min_cost);
} return ;
}
POJ 2135 最小费用最大流的更多相关文章
- POJ 2135 最小费用最大流 入门题
		
Farm Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19207 Accepted: 7441 Descri ...
 - poj   2135最小费用最大流
		
最小费用最大流问题是经济学和管理学中的一类典型问题.在一个网络中每段路径都有"容量"和"费用"两个限制的条件下,此类问题的研究试图寻找出:流量从A到B,如何选择 ...
 - poj 3422(最小费用最大流)
		
题目链接:http://poj.org/problem?id=3422 思路:求从起点到终点走k次获得的最大值,最小费用最大流的应用:将点权转化为边权,需要拆点,边容量为1,费用为该点的点权,表示该点 ...
 - POJ 2516 最小费用最大流
		
每一种货物都是独立的,分成k次最小费用最大流即可! 1: /** 2: 因为e ==0 所以 pe[v] pe[v]^1 是两条相对应的边 3: E[pe[v]].c -= aug; E[pe[v]^ ...
 - POJ - 2195   最小费用最大流
		
题意:每个人到每个房子一一对应,费用为曼哈顿距离,求最小的费用 题解:单源点汇点最小费用最大流,每个人和房子对于建边 #include<map> #include<set> # ...
 - poj 2195   最小费用最大流模板
		
/*Source Code Problem: 2195 User: HEU_daoguang Memory: 1172K Time: 94MS Language: G++ Result: Accept ...
 - poj 3680(最小费用最大流)
		
题目链接:http://poj.org/problem?id=3680 思路:因为N<=200,而区间范围为[1,100000],因此需要离散化,去重,然后就是建图了相连两点连边,容量为k,费用 ...
 - POJ 2315 最小费用最大流
		
从1走到N然后从N走回来的最短路程是多少? 转换为费用流来建模. 1: /** 2: 因为e ==0 所以 pe[v] pe[v]^1 是两条相对应的边 3: E[pe[v]].c -= aug; E ...
 - POJ 2135 Farm Tour (最小费用最大流模板)
		
题目大意: 给你一个n个农场,有m条道路,起点是1号农场,终点是n号农场,现在要求从1走到n,再从n走到1,要求不走重复路径,求最短路径长度. 算法讨论: 最小费用最大流.我们可以这样建模:既然要求不 ...
 
随机推荐
- CSS简介及基本知识
			
(CSS)cascading style sheets:层叠样式表.级联式样式表,简称:样式表. Sheets :就是一个样式文件,它的扩展名为.css Style:外观,个性化 样式表的位置 为了学 ...
 - Vue框架
			
Vue框架 环境: windows python3.6.2 Vue的cdn: <script src="https://cdn.jsdelivr.net/npm/vue"&g ...
 - spark2.1:flatMap的用法
			
代码示例: val sample_data_combine_result=List( (0,(List(FitModel(4022,1447.92,-8.38983306721434,2.0)),1) ...
 - java内部类、接口、集合框架、泛型、工具类、实现类
			
.t1 { background-color: #ff8080; width: 1100px; height: 40px } 一.内部类 1.成员内部类. (1)成员内部类的实例化: 外部类名.内部类 ...
 - Glide v4版本用法探究.md
			
一基本介绍 本博客是基于Glide4.0+进行探究和学习 使用配置 用法比对 二使用配置 1. Android studio 使用项目gradle配置 dependencies { //glide c ...
 - [LeetCode] Word Abbreviation 单词缩写
			
Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations ...
 - Pycharm数据库连接错误
			
简单地总结就一句话: Python2的mysql库为:mysqldb,而Python3的为:pymysql. 当我们使用Pycharm开发项目时,首先需要下载安装相对应的数据库,以及在项目根目录下的s ...
 - 6.QT-简易计算器实现(详解)
			
界面展示 1.用户界面类设计 需要使用QWidget组件作为顶层窗口,QLineEdit组件作为输入框,QPsuhButton作为按钮 1.1 在代码里处理按键消息时,需要处理下用户输入的格式(方便逻 ...
 - 推送本地项目至Github遇到的问题以及解决办法记录
			
在把本地新项目推送至GitHub仓库时的大致流程和步骤,首先现在GitHub上面新建一个项目,复制该项目的 带.git 后缀的地址,比如 git@github.com:XXX/XXX.git 然后在本 ...
 - wows
			
[问题描述]山山最近在玩一款游戏叫战舰世界(steam 游戏太少了),他被大舰巨炮的魅力折服,于是山山开了一局游戏,这次发现目标是一艘战列舰新墨西哥级,舰桥很高,原本应该打在目标身后的圆形水域内的炮弹 ...