[Network flow 24 questions] Maximum increment subsequence (maximum disjoint path---network Max stream)

Source: Internet
Author: User

731. [Network flow 24] Maximum increment subsequence ★★★☆   input file: alis.in   output file: Alis.out   Simple Contrast time limit: 1 s   memory limit: mb«problem Description: Given positive integer sequence x1,..., Xn (1) Calculates the length of its longest increment sub-sequence s. (2) Calculates the maximum number of increments of s that can be removed from a given sequence. (3) If X1 and xn are allowed to be used more than once in the extracted sequence, the maximum number of increments of s can be removed from a given sequence. «Programming tasks: Designing an effective algorithm to complete (1) (2) (3) The computational tasks presented. «Data entry: input data provided by file alis.in. The 1th line of the file has 1 positive integer n (n<=500) representing the length of the given sequence. The next 1 rows have n positive integers x1,..., xn. «Result output: When the program finishes running, the solution for Task (1) (2) (3) is output to file alis.out. The 1th line is the length of the longest increment of the subsequence s. The 2nd line is the number of increments of the desired length of s. Line 3rd is the number of increments of s that are allowed to be removed when X1 and xn are used multiple times in the extracted sequence. Sample output file for input file example alis.in43 6 2 5alis.out223

Algorithm discussion:

First find Dp[i], which represents the length of the longest non-descending subsequence of 1 ... (It was strictly incremental, but the data went down, and it didn't come down.)
Then we can know the longest non-descending subsequence of length K, which is the answer to the first question.
Next take each I split, split into <i, a> <i, b>
For each I,
If there is dp[i] = K, then insert (<I,B>, T, 1) edge.
If there is dp[i] = 1, then insert (S, <i, A>, 1) edges.
Then insert (<i, a>, <i, B>, 1) on the side.
For each pair I < J,
If dp[j] = = Dp[i] + 1 && a[i] <= a[j], then insert (<i, b>, <j, A>, 1) side.
The biggest flow out of the run is the answer to the second question.
For the third question, we simply change the second question to <1> <n> all the capacity of the edge mode to the INF.
Huang said this is the idea of a hierarchical graph, and then the capacity of the edge is the limit of the number of times to take,
Since the third question in X1 xn can be taken multiple times, so the capacity limit into the INF, equivalent to the cancellation of the number of times limit.
Another small detail is that if the flow in the third question is > n, the output is n.
This is because the longest ascending sequence can be constructed infinitely with X1 and xn.
Always feel that this question where is strange.

Code:

#include <cstdlib> #include <iostream> #include <algorithm> #include <cstdio> #include < Cstring> #include <vector>using namespace Std;const int N = $ + 5;const int oo = 0x3f3f3f3f;struct Edge {int fr OM, to, cap, flow; Edge (int u = 0, int v = 0, int c = 0, int f = 0): From (U), to (v), Cap (c), Flow (f) {}};struct dinic {int nn, MM, s, T;int D Is[n], cur[n], que[n * 10];bool vis[n];vector <Edge> edges;vector <int> g[n];void Clear () {for (int i = 0; I &l T;= nn; + + i) g[i].clear (); Edges.clear ();} void Add (int from, int to, int caps) {Edges.push_back (from, to, Cap, 0)), Edges.push_back (Edge (to, from, 0, 0)); mm = ed Ges.size (); G[from].push_back (mm-2); G[to].push_back (mm-1);} BOOL BFs () {int head = 1, tail = 1;memset (Vis, false, sizeof Vis);d is[s] = 0; Que[head] = s; vis[s] = True;while (Head < = tail) {int x = que[head];for (int i = 0; i < (signed) g[x].size (); + + i) {Edge &e = edges[g[x][i]];if (!vis[e.to] & amp;& e.cap > E.flow) {dis[e. to] = Dis[x] + 1;vis[e.to] = true;que[++ tail] = e.to;}} + + head;} return vis[t];} int dfs (int x, int a) {if (x = = T | | a = = 0) return A;int FLW = 0, f;for (int &i = cur[x]; i < (signed) g[x].size (); + + i) {Edge &e = edges[g[x][i]];if (dis[e.to] = = Dis[x] + 1 && (f = dfs (e.to, Min (A, e.cap-e.flow))) > 0) {E.flow + = f; edges[g[x][i] ^ 1].flow-= F;a-F; FLW + = F;if (!a) break;} return FLW;} int Maxflow (int s, int t) {this->s = s; this->t = T;int FLW = 0;while (BFS ()) {memset (cur, 0, sizeof cur); FLW + = DFS ( s, oo);} return FLW;}} Net;int N, S, T;int A[n], dp[n]; #define Stone_int Main () {#ifndef stone_freopen ("alis.in", "R", stdin); Freopen ("Alis.out" , "W", stdout); #endifscanf ("%d", &n); S = 0; T = (n << 1) | 1; NET.NN = t;for (int i = 1; I <= n; + + i) scanf ("%d", &a[i]);DP [n] = 1;for (int i = 1; I <= n; + + i) {Dp[i] = 1;for (int j = 1; j < I; + + j) if (A[i] >= a[j]) dp[i] = max (Dp[i], dp[j] + 1);} int result = 0;for (int i = 1; I <= n; + + i)result = Max (result, dp[i]); for (int i = 1; I <= n; + + i) {if (dp[i] = = result) Net.add (i + N, T, 1); if (dp[i] = = 1) net.a DD (S, I, 1); Net.add (i, i + N, 1);} for (int i = 1; I <= N, + + i) for (int j = i + 1; j <= N; + + j) if (j! = i) if (dp[j] = = Dp[i] + 1 && a[i] &lt ; = A[j]) net.add (i + N, j, 1);p rintf ("%d\n%d\n", result, Net.maxflow (S, T)); Net.clear (); for (int i = 1; I <= n; + + i) { int v = 1;if (i = = 1 | | i = = N) v = oo;if (dp[i] = = result) Net.add (i + N, T, V), if (dp[i] = = 1) net.add (S, I, v); Net.add (I, i + N, v);} for (int i = 1; I <= N, + + i) for (int j = i + 1; j <= N; + + j) if (j! = i) if (dp[j] = Dp[i] + 1 && a[i] < = A[j]) net.add (i + N, j, 1), int ans = net.maxflow (S, T), if (Ans > N) printf ("%d\n", N), else printf ("%d\n", ans); #ifnde F Stone_fclose (stdin); Fclose (stdout); #endifreturn 0;}

[Network flow 24 questions] Maximum increment subsequence (maximum disjoint path---network Max stream)

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.