Meaning of the questions:
Ideas:
【problem analysis】
Figure seeking maximum weight disjoint paths and its variants, with a cost maximum flow resolved.
[Modeling]
Rule (1)
The position of each trapezoid abstracted as two points <ia>, <ib>, establish additional source S sink T.
1, for each point connected from the i <ia> to <ib> a capacity of 1, cost of point i weights directed edges.
2, from the top of each ladder to S <ia> even a capacity of 1, 0 cost directed edge.
3, from the bottom of each trapezoidal <ib> T is connected to a capacity of 1, 0 cost directed edge.
4, for each point following two points i and j, respectively, even from a <ib> to <ja> capacity of 1, 0 cost directed edge.
Seeking the most cost maximum flow, cost flow value is the result.
Rule (2)
The position of each trapezoid seen as a point i, the establishment of additional source S sink T.
1, the S i is connected to each of a trapezoidal top capacity of 1, 0 cost directed edge.
2, from the bottom of each trapezoid T i is connected to an infinite capacity, cost 0 directed edge.
3, for each point following two points i and j, respectively, even a capacity from i to j is 1, the cost for the point i weights directed edges.
Seeking the most cost maximum flow, cost flow value is the result.
Rule (3)
The position of each trapezoid seen as a point i, the establishment of additional source S sink T.
1, the S i is connected to each of a trapezoidal top capacity of 1, 0 cost directed edge.
2, from the bottom of each trapezoid T i is connected to an infinite capacity, cost 0 directed edge.
3, for each point following two points i and j, respectively, from i to j even an infinite capacity, cost of point i weights directed edges.
Seeking the most cost maximum flow, cost flow value is the result.
[Modeling Analysis]
For rule 1, claim completely disjoint paths, i.e. each point can only be accessed once, so it is a split point, the capacity of the connection between the edge 1. Because any one path between the ST is a solution, the weight is set at the edge point of the interior of the split point cost, seek
The maximum fee is the maximum flow rate with maximum of m by road.
For the rule 2, the path may intersect the requirements, but can not overlap, may not necessarily be removed at this time point. In order to ensure that the path does not overlap on the need to limit the flow of two adjacent points 1, since each point of the top layer can only be used once, S points to the top of a flow restrictions. For just
In neighboring points on the edge, seeking the most cost maximum flow can be.
For rule 3, except for the top layer of each required path point may intersect any overlap. 2 on the basis of the rules, in addition to cancel the edge between vertices S to the top edges of all the flow restriction can.
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 typedef unsigned int uint; 5 typedef unsigned long long ull; 6 typedef long double ld; 7 typedef pair<int,int> PII; 8 typedef pair<ll,ll> Pll; 9 typedef vector<int> VI; 10 typedef vector<PII> VII; 11 typedef pair<ll,ll>P; 12 #define N 100010 13 #define M 1000000 14 #define INF 1e9 15 #define fi first 16 #define se second 17 #define MP make_pair 18 #define pb push_back 19 #define pi acos(-1) 20 #define mem(a,b) memset(a,b,sizeof(a)) 21 #define rep(i,a,b) for(int i=(int)a;i<=(int)b;i++) 22 #define per(i,a,b) for(int i=(int)a;i>=(int)b;i--) 23 #define lowbit(x) x&(-x) 24 #define Rand (rand()*(1<<16)+rand()) 25 #define id(x) ((x)<=B?(x):m-n/(x)+1) 26 #define ls p<<1 27 #define rs p<<1|1 28 29 const ll MOD=1e9+7,inv2=(MOD+1)/2; 30 double eps=1e-6; 31 int dx[4]={-1,1,0,0}; 32 int dy[4]={0,0,-1,1}; 33 34 int head[N],vet[N],len1[N],len2[N],nxt[N],dis[N],q[N],inq[N],a[50][50], 35 num[50][50][2],pre[N][2],s,S,T,tot,ans1,ans2,n,m; 36 37 38 39 int read() 40 { 41 int v=0,f=1; 42 char c=getchar(); 43 while(c<48||57<c) {if(c=='-') f=-1; c=getchar();} 44 while(48<=c&&c<=57) v=(v<<3)+v+v+c-48,c=getchar(); 45 return v*f; 46 } 47 48 void add(int a,int b,int c,int d) 49 { 50 nxt[++tot]=head[a]; 51 vet[tot]=b; 52 len1 [all] = c; 53 len2 [all] = d; 54 head [a] = all; 55 56 nxt [all ++] = head [b]; 57 veto [All] = a; 58 len1 [all] = 0 ; 59 len2 [all] = - d; 60 head [b] = all; 61 } 62 63 int spfa () 64 { 65 receives (and one , s) 66 { 67 dis [i] = - INF; 68 inq[i]=0; 69 } 70 int t=0,w=1; 71 q[1]=S; dis[S]=0; inq[S]=1; 72 while(t<w) 73 { 74 t++; int u=q[t%(s+5)]; inq[u]=0; 75 int e=head[u]; 76 while(e) 77 { 78 int v=vet[e]; 79 if(len1 [e] && dis [u] + len2 [e]> dis [v]) 80 { 81 dis [v] = dis [u] + len2 [e]; 82 pre [v] [ 0 ] = u; 83 pre [v] [ 1 ] = e; 84 if (! Inq [v]) 85 { 86 w ++; q [w% (s + 5 )] = v; inq [v] = 1 ; 87 } 88 } 89 e = nxt [e]; 90 } 91 } 92 if(dis[T]==-INF) return 0; 93 return 1; 94 } 95 96 void mcf() 97 { 98 int k=T; 99 int t=INF; 100 while(k!=S) 101 { 102 int e=pre[k][1]; 103 t=min(t,len1[e]); 104 k=pre[k][0]; 105 } 106 ans1+=t; 107 k=T; 108 while(k!=S) 109 { 110 int e=pre[k][1]; 111 len1[e]-=t; 112 len1[e^1]+=t; 113 ans2+=t*len2[e]; 114 k=pre[k][0]; 115 } 116 } 117 118 void solve1() 119 { 120 tot=1; 121 rep(i,1,s) head[i]=0; 122 rep(i,1,m) add(S,num[1][i][0],1,0); 123 rep(i,1,n-1) 124 rep(j,1,m+i-1) 125 { 126 add(num[i][j][1],num[i+1][j][0],1,0); 127 add(num[i][j][1],num[i+1][j+1][0],1,0); 128 } 129 rep(i,1,n) 130 rep(j,1,m+i-1) add(num[i][j][0],num[i][j][1],1,a[i][j]); 131 rep(i,1,n+m-1) add(num[n][i][1],T,1,0); 132 ans1=ans2=0; 133 while(spfa()) mcf(); 134 printf("%d\n",ans2); 135 } 136 137 void solve2() 138 { 139 tot=1; 140 rep(i,1,s) head[i]=0; 141 rep(i,1,m) add(S,num[1][i][0],1,0); 142 rep(i,1,n-1) 143 rep(j,1,m+i-1) 144 { 145 add(num[i][j][0],num[i+1][j][0],1,a[i][j]); 146 add(num[i][j][0],num[i+1][j+1][0],1,a[i][j]); 147 } 148 rep(i,1,n+m-1) add(num[n][i][0],T,INF,a[n][i]); 149 ans1=ans2=0; 150 while(spfa()) mcf(); 151 printf("%d\n",ans2); 152 } 153 154 void solve3() 155 { 156 tot=1; 157 rep(i,1,s) head[i]=0; 158 rep(i,1,m) add(S,num[1][i][0],1,0); 159 rep(i,1,n-1) 160 rep(j,1,m+i-1) 161 { 162 add(num[i][j][0],num[i+1][j][0],INF,a[i][j]); 163 add(num[i][j][0],num[i+1][j+1][0],INF,a[i][j]); 164 } 165 rep(i,1,n+m-1) add(num[n][i][0],T,INF,a[n][i]); 166 ans1=ans2=0; 167 while(spfa()) mcf(); 168 printf("%d\n",ans2); 169 } 170 171 int main() 172 { 173 //freopen("1.in","r",stdin); 174 m=read(),n=read(); 175 s=0; 176 rep(i,1,n) 177 rep(j,1,m+i-1) 178 { 179 a[i][j]=read(); 180 num[i][j][0]=++s; 181 num[i][j][1]=++s; 182 } 183 S=++s,T=++s; 184 solve1(); 185 solve2(); 186 solve3(); 187 return 0; 188 }