Number of squares time limit: 1000 msmemory limit: 65535 kbthis problem will be judged on hrbust. Original ID: 1214
64-bit integer Io format: % LLD Java class name: Main
A square map with N * n (n <= 10) is provided. We fill in some squares with positive integers, while other squares with 0 Numbers. As shown in (see the example. The yellow and blue are the two routes respectively, and the green grids are the ones that walk together in yellow and blue ):
Someone starts from point A in the upper left corner of the graph and can walk down or right until it reaches Point B in the lower right corner. On the way he walked, he could take away the number from the square (the square after the square is removed will become a number 0 ). This person takes two steps from A.M. To a.m. and tries to find two such paths so that the sum of the obtained numbers is the largest.
.
Input has multiple groups of test data, in the following format:
The first behavior is an integer N (representing the square map of N * n). The next row has three integers, the first two represent positions, and the third number is the number placed on this position. A single row of 0 indicates that the input is complete.
The output corresponds to the input and has multiple output groups. Each group only outputs an integer, indicating the maximum sum obtained from the two paths. Sample input8
2 3 13
2 6 6
3 5 7
4 4 14
5 2 21
5 6 4
6 3 15
7 2 14
0 0 0 sample output
67
Sourcenoip2000 improve group problem solving: Split point fee flow. See http://www.cnblogs.com/crackpotisback/p/3971435.html
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 400;18 struct arc{19 int v,w,f,next;20 arc(int x = 0,int y = 0,int z = 0,int nxt = 0){21 v = x;22 w = y;23 f = z;24 next = nxt;25 }26 };27 arc e[1000];28 int head[maxn],d[maxn],p[maxn],S,T,n,mp[15][15],tot;29 bool in[maxn];30 queue<int>q;31 void add(int u,int v,int w,int f){32 e[tot] = arc(v,w,f,head[u]);33 head[u] = tot++;34 e[tot] = arc(u,-w,0,head[v]);35 head[v] = tot++;36 }37 bool spfa(){38 for(int i = 0; i < maxn; i++){39 d[i] = INF;40 in[i] = false;41 p[i] = -1;42 }43 while(!q.empty()) q.pop();44 d[S] = 0;45 in[S] = true;46 q.push(S);47 while(!q.empty()){48 int u = q.front();49 q.pop();50 in[u] = false;51 for(int i = head[u]; ~i; i = e[i].next){52 if(e[i].f > 0 && d[e[i].v] > d[u] + e[i].w){53 d[e[i].v] = d[u] + e[i].w;54 p[e[i].v]= i;55 if(!in[e[i].v]){56 in[e[i].v] = true;57 q.push(e[i].v);58 }59 }60 }61 }62 return p[T] > -1;63 }64 int solve(){65 int tmp = 0,minV;66 while(spfa()){67 minV = INF;68 for(int i = p[T]; ~i; i = p[e[i^1].v])69 minV = min(minV,e[i].f);70 for(int i = p[T]; ~i; i = p[e[i^1].v]){71 e[i].f -= minV;72 e[i^1].f += minV;73 tmp += minV*e[i].w;74 }75 }76 return tmp;77 }78 int main() {79 int x,y,w;80 while(~scanf("%d",&n)){81 memset(mp,0,sizeof(mp));82 memset(head,-1,sizeof(head));83 S = tot = 0;84 T = n*n*2+1;85 while(scanf("%d %d %d",&x,&y,&w),x||y||w) mp[x][y] = w;86 for(int i = 1; i <= n; i++){87 for(int j = 1; j <= n; j++){88 add(n*(i-1)+j,n*(i-1)+j+n*n,-mp[i][j],1);89 add(n*(i-1)+j,n*(i-1)+j+n*n,0,INF);90 if(i < n) add(n*(i-1)+j+n*n,n*i+j,0,INF);91 if(j < n) add(n*(i-1)+j+n*n,n*(i-1)+j+1,0,INF);92 }93 }94 add(S,1,0,2);95 add(n*n*2,T,0,2);96 printf("%d\n",-solve());97 }98 return 0;99 }
View code
Hrbust 1214 squares