POJ 2135 Farm Tour and HDU 1853 Cyclic Tour (initial application of fee flow)

Source: Internet
Author: User
Tags min time limit
Farm Tour (portal)

Time limit:1000ms Memory limit:65536k
Total submissions:15477 accepted:5966 Description

When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1<=n<=1000) n (1 fields numbered 1..N 1..N, the first of which contains he house and the Nth O F which contains the big barn. A Total m (1<=m<=10000) m (1 paths, that connect, the fields in various ways. Each path connects the different fields and have a nonzero length smaller than 35,000 35,000.

To show off his farm in the best-of-the-walks, he-a-tour-that-starts at his house, potentially travels through-some fields, a nd ends at the barn. Later, he returns (potentially through some fields) the back to his house again.

He wants his tour to being as short as possible, however he doesn ' t want-walk on any given path more than once. Calculate the shortest tour possible. FJ is sure this some tour exists for any given farm. Input

Line 1 1:two space-separated integers:n N and M M.

Lines 2 2. M+1 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 Source

Usaco 2003 February Green

Test instructions

give you n n vertices, M m bars, solve the shortest path from 1 1 to n N and then from n N to 1 1, each of which is allowed to be accessed only once.

Thinking of solving problems

From 0 0 constructs an edge to 1 1, its capacity is 2 2, then constructs an n n to n+1 n+1 edge, the capacity also is 2, then the expense flow template goes one side, solves the 0 0 to n+1 n+1 the minimum expense flow.

Code

#include <cstdio> #include <cstring> #include <cstdlib> #include <queue> #include <iostream&
Gt

#include <algorithm> #include <functional> using namespace std;
typedef pair<int,int > PII;
const int MAXN = 4e4 + 5;

const int INF = 0X3F3F3F3F; struct edge{int u, V, cap, flow, cost, NXT;}

E[MAXN << 1];

int HEAD[MAXN], tot;
int PRE[MAXN], DIS[MAXN];

BOOL VIS[MAXN];
    void Edge_init () {tot = 0;
Memset (Head,-1, sizeof (head));
    } void Add_edge (int u, int v, int cap, int cost) {e[tot].u = u;
    E[TOT].V = v;
    E[tot].cap = cap;
    E[tot].flow = 0;
    E[tot].cost = Cost;
    E[TOT].NXT = Head[u];


    Head[u] = tot + +;
    e[tot].u = v;
    E[TOT].V = u;
    E[tot].cap = 0;
    E[tot].flow = 0;
    E[tot].cost =-cost;
    E[TOT].NXT = Head[v];
HEAD[V] = tot + +;
    } bool SPFA (int start, int end) {queue<int>q;
    Memset (Vis, false, sizeof (VIS));
    memset (Pre,-1, sizeof (pre)); memset (Dis, 0x3f, sizeof (DIS));
    Dis[start] = 0;
    Vis[start] = true;
    Q.push (start);
        while (!q.empty ()) {int u = q.front ();
        Q.pop ();
        Vis[u] = false;
            for (int i = head[u]; ~i;i = e[i].nxt) {int v = E[I].V;
                if (E[i].cap-e[i].flow && dis[v] > Dis[u] + e[i].cost) {Dis[v] = Dis[u] + e[i].cost;
                PRE[V] = i;
                    if (!vis[v]) {Vis[v] = true;
                Q.push (v);
    }}}} if (pre[end] = = 1) return false;
return true;
    } PII min_flow (int s,int t) {int flow = 0;
    int cost = 0;
        while (SPFA (s, t)) {int Min = INF; for (int i = Pre[t];~i;i = pre[e[i].u]) {if (min > E[i].cap-e[i].flow) {min = e[i].cap-e[
            I].flow;
            }} for (int i = pre[t]; ~i;i = pre[e[i].u]) {e[i].flow + = Min;
            e[i ^ 1].flow-= Min; Cost + = E[i].cost * Min;
    } flow + = Min;
} return Make_pair (flow, cost);

} int N, M;
        int main () {while (~scanf ("%d%d", &n, &m)) {int A, b, C;
        Edge_init ();
            for (int i = 0;i < M;i + +) {scanf ("%d%d%d", &a, &b, &c);
            Add_edge (A, B, 1, c);
        Add_edge (b, A, 1, c);
        } add_edge (0, 1, 2, 0);
        Add_edge (n, n + 1, 2, 0);
        PII AA = Min_flow (0, N + 1);
    printf ("%d\n", Aa.second);
} return 0;
 }
Cyclic Tour (portal)

Time limit:1000/1000 MS (java/others

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.