Poj 2135 farm tour (billing flow)

Source: Internet
Author: User
Poj 2135 farm tour

Question Link

Given an undirected graph, the edge has the right to calculate the shortest path from 1 to n and then from N to 1.

Idea: The cost flow, the edge capacity is 1 (note that it is an undirected graph), then the source point and 1 are connected with 2 capacity, and the connection capacity of N and sink point is 2

Code:

#include <cstdio>#include <cstring>#include <vector>#include <queue>#include <algorithm>using namespace std;const int MAXNODE = 1005;const int MAXEDGE = 40005;typedef long long Type;const Type INF = 0x3f3f3f3f3f3f3f;struct Edge {int u, v;Type cap, flow, cost;Edge() {}Edge(int u, int v, Type cap, Type flow, Type cost) {this->u = u;this->v = v;this->cap = cap;this->flow = flow;this->cost = cost;}};struct MCFC {int n, m, s, t;Edge edges[MAXEDGE];int first[MAXNODE];int next[MAXEDGE];int inq[MAXNODE];Type d[MAXNODE];int p[MAXNODE];Type a[MAXNODE];void init(int n) {this->n = n;memset(first, -1, sizeof(first));m = 0;}void add_Edge(int u, int v, Type cap, Type cost) {edges[m] = Edge(u, v, cap, 0, cost);next[m] = first[u];first[u] = m++;edges[m] = Edge(v, u, 0, 0, -cost);next[m] = first[v];first[v] = m++;}bool bellmanford(int s, int t, Type &flow, Type &cost) {for (int i = 0; i < n; i++) d[i] = INF;memset(inq, false, sizeof(inq));d[s] = 0; inq[s] = true; p[s] = s; a[s] = INF;queue<int> Q;Q.push(s);while (!Q.empty()) {int u = Q.front(); Q.pop();inq[u] = false;for (int i = first[u]; i != -1; i = next[i]) {Edge& e = edges[i];if (e.cap > e.flow && d[e.v] > d[u] + e.cost) {d[e.v] = d[u] + e.cost;p[e.v] = i;a[e.v] = min(a[u], e.cap - e.flow);if (!inq[e.v]) {Q.push(e.v); inq[e.v] = true;}}}}if (d[t] == INF) return false;flow += a[t];cost += d[t] * a[t];int u = t;while (u != s) {edges[p[u]].flow += a[t];edges[p[u]^1].flow -= a[t];u = edges[p[u]].u;}return true;}Type Mincost(int s, int t) {Type flow = 0, cost = 0;while (bellmanford(s, t, flow, cost));return cost;}} gao;typedef long long ll;int n, m;int main() {while (~scanf("%d%d", &n, &m)) {gao.init(n + 2);int u, v;ll w;while (m--) {scanf("%d%d%lld", &u, &v, &w);gao.add_Edge(u, v, 1, w);gao.add_Edge(v, u, 1, w);}gao.add_Edge(0, 1, 2, 0);gao.add_Edge(n, n + 1, 2, 0);printf("%lld\n", gao.Mincost(0, n + 1));}return 0;}


Poj 2135 farm tour (billing flow)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.