当前位置: 代码迷 >> 综合 >> Farm Tour POJ - 2135(最小费用流)
  详细解决方案

Farm Tour POJ - 2135(最小费用流)

热度:36   发布时间:2023-12-07 00:23:03.0

Farm Tour POJ - 2135

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 2

Sample Output
6

题意:
求从1到N点不重复的两条路(往返)

题解:
易想到用两次用最短的求法不一定是 不重复 且 最短
用最短费用流,每条路长度为费用, 每条路允许的最大流量为1。
求往返两条路,即需要流量为2。
起点终点流量为2,求最小费用(用板子)

#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
#define SZ(v) (int)v.size()
#define pii pair<int,int>const int INF = 1e9;
const int MAXN = 1e4 + 10;
const int MAXM = 1e4 + 10;struct MCMF {
    //板子struct Edge {
    int v, cap, cost, rev;Edge(int v, int cap, int cost, int rev):v(v),cap(cap),cost(cost),rev(rev){
    }};int flow, cost, s, t, n;int dist[MAXN], H[MAXN], pv[MAXN], pe[MAXN];std::vector<Edge> G[MAXN];bool dijkstra() {
    std::priority_queue<pii, std::vector<pii>, std::greater<pii> > q;std::fill(dist, dist + n + 1, INF);dist[s] = 0; q.push({
    0, s});while (!q.empty()) {
    pii x = q.top(); q.pop();int &u = x.second;if (dist[u] < x.first) continue;for (int i = 0; i < SZ(G[u]); ++i) {
    Edge &e = G[u][i];int &v = e.v;pii y(dist[u] + e.cost + H[u] - H[v], v);if (e.cap > 0 && dist[v] > y.first) {
    dist[v] = y.first;pe[v] = i, pv[v] = u;q.push(y);}}}if (dist[t] == INF) return false;for (int i = 0; i <= n; ++i) H[i] += dist[i];int f = INF;for (int v = t; v != s; v = pv[v]) f = std::min(f, G[pv[v]][pe[v]].cap);flow += f;cost += f * H[t];for (int v = t; v != s; v = pv[v]) {
    Edge &e = G[pv[v]][pe[v]];e.cap -= f;G[v][e.rev].cap += f;}return true;}void solve(int s, int t) {
    this->s = s, this->t = t;flow = cost = 0;std::fill(H, H + n + 1, 0);while (dijkstra());}void init(int n) {
    this->n = n;for (int i = 0; i <= n; ++i) G[i].clear();}void add_edge(int u, int v, int cap, int cost) {
    G[u].push_back(Edge(v, cap, cost, SZ(G[v])));G[v].push_back(Edge(u, 0, -cost, SZ(G[u]) - 1));}} mcmf;int main() {
    int N, M;scanf("%d%d", &N, &M);int s = 0, t = N + 1;mcmf.init(t + 1);for(int i = 1; i <= M; i++){
    int A, B, cost;scanf("%d%d%d", &A, &B, &cost);mcmf.add_edge(A, B, 1, cost);mcmf.add_edge(B, A, 1, cost);}mcmf.add_edge(s, 1, 2, 0);mcmf.add_edge(N, t, 2, 0);mcmf.solve(s, t);printf("%d\n", mcmf.cost);
}
  相关解决方案