1. 程式人生 > >[luoguP2766] 最長遞增子序列問題(最大流)

[luoguP2766] 最長遞增子序列問題(最大流)

close spl 方法 emp 路徑 pid code display div

傳送門

題解來自網絡流24題:

【問題分析】

第一問時LIS,動態規劃求解,第二問和第三問用網絡最大流解決。

【建模方法】

首先動態規劃求出F[i],表示以第i位為開頭的最長上升序列的長度,求出最長上升序列長度K。

1、把序列每位i拆成兩個點<i.a>和<i.b>,從<i.a>到<i.b>連接一條容量為1的有向邊。

2、建立附加源S和匯T,如果序列第i位有F[i]=K,從S到<i.a>連接一條容量為1的有向邊。

3、如果F[i]=1,從<i.b>到T連接一條容量為1的有向邊。

4、如果j>i且A[i] < A[j]且F[j]+1=F[i],從<i.b>到<j.a>連接一條容量為1的有向邊。

求網絡最大流,就是第二問的結果。把邊(<1.a>,<1.b>)(<N.a>,<N.b>)(S,<1.a>)(<N.b>,T)這四條邊的容量修改為無窮大,再求一次網絡最大流,就是第三問結果。

【建模分析】

上述建模方法是應用了一種分層圖的思想,把圖每個頂點i按照F[i]的不同分為了若幹層,這樣圖中從S出發到T的任何一條路徑都是一個滿足條件的最長上升子序列。

由於序列中每個點要不可重復地取出,需要把每個點拆分成兩個點。單位網絡的最大流就是增廣路的條數,所以最大流量就是第二問結果。

第三問特殊地要求x1和xn可以重復使用,只需取消這兩個點相關邊的流量限制,求網絡最大流即可。

還有這個題題意有些問題,不是遞增,是不遞減。

還有我這個題沒有拆點在洛谷和codevs上都過了,究竟需不需要拆點啊。。

——代碼(木有拆點)

技術分享
  1 #include <queue>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <iostream>
  5 #define N 1010
  6 #define M 3000001
  7 #define max(x, y) ((x) > (y) ? (x) : (y))
  8
#define min(x, y) ((x) < (y) ? (x) : (y)) 9 10 int n, ans, cnt, s, t, sum; 11 int a[N], f[N]; 12 int head[N], to[M], val[M], next[M], dis[N], cur[N]; 13 14 inline int read() 15 { 16 int x = 0, f = 1; 17 char ch = getchar(); 18 for(; !isdigit(ch); ch = getchar()) if(ch == -) f = -1; 19 for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - 0; 20 return x * f; 21 } 22 23 inline void add(int x, int y, int z) 24 { 25 to[cnt] = y; 26 val[cnt] = z; 27 next[cnt] = head[x]; 28 head[x] = cnt++; 29 } 30 31 inline bool bfs() 32 { 33 int i, u, v; 34 std::queue <int> q; 35 memset(dis, -1, sizeof(dis)); 36 q.push(s); 37 dis[s] = 0; 38 while(!q.empty()) 39 { 40 u = q.front(), q.pop(); 41 for(i = head[u]; i ^ -1; i = next[i]) 42 { 43 v = to[i]; 44 if(val[i] && dis[v] == -1) 45 { 46 dis[v] = dis[u] + 1; 47 if(v == t) return 1; 48 q.push(v); 49 } 50 } 51 } 52 return 0; 53 } 54 55 inline int dfs(int u, int maxflow) 56 { 57 if(u == t) return maxflow; 58 int i, v, d, ret = 0; 59 for(i = cur[u]; i ^ -1; i = next[i]) 60 { 61 v = to[i]; 62 if(val[i] && dis[v] == dis[u] + 1) 63 { 64 d = dfs(v, min(val[i], maxflow)); 65 ret += d; 66 cur[u] = i; 67 val[i] -= d; 68 val[i ^ 1] += d; 69 if(ret == maxflow) return ret; 70 } 71 } 72 return ret; 73 } 74 75 inline void clear() 76 { 77 int i, j; 78 sum = cnt = 0; 79 memset(head, -1, sizeof(head)); 80 for(i = 1; i <= n; i++) 81 { 82 if(f[i] == 1) add(s, i, 1), add(i, s, 0); 83 if(f[i] == ans) add(i, t, 1), add(t, i, 0); 84 } 85 for(i = 1; i <= n; i++) 86 for(j = 1; j < i; j++) 87 if(a[j] <= a[i] && f[j] + 1 == f[i]) 88 add(j, i, 1), add(i, j, 0); 89 } 90 91 int main() 92 { 93 int i, j, x; 94 n = read(); 95 s = 0, t = n + 1; 96 for(i = 1; i <= n; i++) 97 { 98 a[i] = read(); 99 x = 0; 100 for(j = 1; j < i; j++) 101 if(a[j] <= a[i]) 102 x = max(x, f[j]); 103 f[i] = x + 1; 104 ans = max(ans, f[i]); 105 } 106 printf("%d\n", ans); 107 clear(); 108 while(bfs()) 109 { 110 for(i = s; i <= t; i++) cur[i] = head[i]; 111 sum += dfs(s, 1e9); 112 } 113 printf("%d\n", sum); 114 clear(); 115 add(s, 1, 1e9), add(1, s, 0); 116 if(f[n] == ans) add(n, t, 1e9), add(t, n, 0); 117 while(bfs()) 118 { 119 for(i = s; i <= t; i++) cur[i] = head[i]; 120 sum += dfs(s, 1e9); 121 } 122 printf("%d\n", sum); 123 return 0; 124 }
View Code

[luoguP2766] 最長遞增子序列問題(最大流)