[网络流]Farm Tour(费用流

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

代码:
  1 #include<iostream>
  2 #include<queue>
  3 #include<cstdio>
  4 #include<cstring>
  5 using namespace std;
  6 int total;
  7 const int MAXN = 1010;
  8 const int INF = 1000000000;
  9 struct Edge
 10 {
 11     int u, v, cap, cost;
 12     int next;
 13 } edge[40010];
 14 int edgenum;
 15 int head[MAXN], dist[MAXN], pre[MAXN];
 16 bool vis[MAXN];
 17 void init()
 18 {
 19     edgenum = 0;
 20     memset(head, -1, sizeof(head));
 21 }
 22 void addedge(int u, int v, int cap, int cost)
 23 {
 24     edge[edgenum].u = u;
 25     edge[edgenum].v = v;
 26     edge[edgenum].cap = cap;
 27     edge[edgenum].cost = cost;
 28     edge[edgenum].next = head[u];
 29     head[u] = edgenum++;
 30     edge[edgenum].u = v;
 31     edge[edgenum].v = u;
 32     edge[edgenum].cap = 0;
 33     edge[edgenum].cost = -cost;
 34     edge[edgenum].next = head[v];
 35     head[v] = edgenum++;
 36 }
 37 bool spfa(int s, int t, int n)//找到一条增广路
 38 {
 39     int i, u, v;
 40     queue <int> qu;
 41     memset(vis, false, sizeof(vis));
 42     memset(pre, -1, sizeof(pre));
 43     for(i = 0; i <= n; i++) dist[i] = INF;
 44     vis[s] = true;
 45     dist[s] = 0;
 46     qu.push(s);
 47     while(!qu.empty())
 48     {
 49         u = qu.front();
 50         qu.pop();
 51         vis[u] = false;
 52         for(i = head[u]; i != -1; i = edge[i].next)
 53         {
 54             v = edge[i].v;
 55             if(edge[i].cap && dist[v] > dist[u] + edge[i].cost)
 56             {
 57                 dist[v] = dist[u] + edge[i].cost;
 58                 pre[v] = i;
 59                 if(!vis[v])
 60                 {
 61                     qu.push(v);
 62                     vis[v] = true;
 63                 }
 64             }
 65         }
 66     }
 67     if(dist[t] == INF) return false;
 68     return true;
 69 }
 70 int min_cost_max_flow(int s, int t, int n)
 71 {
 72     int flow = 0; // 总流量
 73     int i, minflow, mincost;
 74     mincost = 0;
 75     while(spfa(s, t, n))
 76     {
 77         minflow = INF + 1;
 78         for(i = pre[t]; i != -1; i = pre[edge[i].u])
 79             if(edge[i].cap < minflow)
 80                 minflow = edge[i].cap;
 81         flow += minflow;
 82         for(i = pre[t]; i != -1; i = pre[edge[i].u])
 83         {
 84             edge[i].cap -= minflow;
 85             edge[i^1].cap += minflow;
 86         }
 87         mincost += dist[t] * minflow;
 88     }
 89     total = flow; // 最大流
 90     return mincost;
 91 }
 92 int main()
 93 {
 94     int n, m;
 95     int u, v, c;
 96     while (scanf("%d%d", &n, &m) != -1)
 97     {
 98         init();
 99         int s = 0;
100         int t = n + 1;
101         while (m--)
102         {
103             scanf("%d%d%d", &u, &v, &c);
104             addedge(u, v , 1 , c);
105             addedge(v, u , 1 , c);
106         }
107         addedge(s, 1, 2, 0);
108         addedge(n, t, 2, 0);
109         int ans = min_cost_max_flow(s, t, n + 2);
110         printf("%d\n", ans);
111     }
112     return 0;
113 } 

原文地址:https://www.cnblogs.com/GldHkkowo/p/8877032.html

时间: 2024-11-08 11:19:27

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

POJ 2135 Farm Tour(费用流)

POJ 2135 Farm Tour 题目链接 题意:给定一个无向图,边有权值,求从1到n再从n到1的最短路 思路:费用流,连边容量为1(注意是无向图),然后源点和1连容量2,n和汇点连容量是2 代码: #include <cstdio> #include <cstring> #include <vector> #include <queue> #include <algorithm> using namespace std; const int

网络流总结(三)费用流

关于最小费用最大流 这里的最小费用最大流是在最大流的基础上把费用最小化 Ek费用流 因为会有负边权,所以需要用Spfa求出最小费用,之后Ek一发就好 代码还是不放了吧 ZKW费用流 和Dinic几乎一样,就是在dfs的时候记一个vis数组即可,否则出0环就写比了 ZKW费用流在层数较少的时候会很快,比如无限之环这道题: Ek费用流8000ms,zkw费用流100ms 代码也咕了 T1晨跑 题目描述 Elaxia最近迷恋上了空手道,他为自己设定了一套健身计划,比如俯卧撑.仰卧起坐等 等,不过到目前

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

题目描述 一个餐厅在相继的 N 天里,每天需用的餐巾数不尽相同.假设第 iii 天需要 ri?块餐巾( i=1,2,...,N).餐厅可以购买新的餐巾,每块餐巾的费用为 p 分;或者把旧餐巾送到快洗部,洗一块需 m 天,其费用为 f 分;或者送到慢洗部,洗一块需 n 天(n>m),其费用为 sss 分(s<fs<fs<f). 每天结束时,餐厅必须决定将多少块脏的餐巾送到快洗部,多少块餐巾送到慢洗部,以及多少块保存起来延期送洗.但是每天洗好的餐巾和购买的新餐巾数之和,要满足当天的需求

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

Farm Tour Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 2135 64-bit integer IO format: %lld      Java class name: Main When FJ's friends visit him on the farm, he likes to show them around. His farm compris

POJ 2135 Farm Tour(网络流之费用流)

题目地址:POJ 2135 来回走一遍可以看成从源点到汇点走两遍.将每个点的流量设为1,就可以保证每条边不重复.然后跑一次费用流就行了.当流量到了2之后停止,输出此时的费用. #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <qu

POJ2135_Farm Tour(网络流/费用流)

解题报告 题目传送门 题意: 一个人有n个农场,他想从1到n去,有从n到1回来,要求路径最短,且没有走重复的路. 思路: 如果两次最短路感觉不行的,可以看成费用流,每一条路容量都是1,这样只要流量等于2就行了. 一次mcmf模版. #include <iostream> #include <cstring> #include <queue> #include <cstdio> #define inf 0x3f3f3f3f using namespace st

poj 2135 Farm Tour (最小费用最大流模板)

网络流的费用: 在实际应用中,与网络流有关的问题,不仅涉及流量,而且还有费用的因素.网络的每一条边(v,w)除了给定容量cap(v,w)外,还定义了一个单位流量费用cost(v,w) 最小费用最大流问题 给定网络G,要求G的一个最大用流flow,使流的总费用最小. 求解MCMF问题的算法: 最小费用最大流最常用和基本的算法我们可以称它为最小费用路算法,其思想与求最大流的增广路算法类似,不断在残流网络中寻找从源s到汇t的最小费用路,即残流网络中从s到t的以费用为权的最短路,然后沿最小费用路增流,直

POJ 2135 Farm Tour &amp;&amp; HDU 2686 Matrix &amp;&amp; HDU 3376 Matrix Again 费用流求来回最短路

累了就要写题解,最近总是被虐到没脾气. 来回最短路问题貌似也可以用DP来搞,不过拿费用流还是很方便的. 可以转化成求满流为2 的最小花费.一般做法为拆点,对于 i 拆为2*i 和 2*i+1,然后连一条流量为1(花费根据题意来定) 的边来控制每个点只能通过一次. 额外添加source和sink来控制满流为2. 代码都雷同,以HDU3376为例. #include <algorithm> #include <iostream> #include <cstring> #in

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

题目:poj 2135 Farm Tour 题意:给出一个无向图,问从 1 点到 n 点然后又回到一点总共的最短路. 分析:这个题目不读仔细的话可能会当做最短路来做,最短路求出来的不一定是最优的,他是两条分别最短,但不一定是和最短. 我们可以用费用流来很轻易的解决,建边容量为1,费用为边权,然后源点s连 1 ,费用0 ,容量 2 ,n点连接汇点,容量2,费用0,,就可以了. 注意这个题目是无向图,所以要建双向边. AC代码: #include <iostream> #include <a