HPU 2686--Matrix【最大費用最大流 && 經典建圖】,hpu2686--matrix
Matrix
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2062 Accepted Submission(s): 1074
Problem DescriptionYifenfei very like play a number game in the n*n Matrix. A positive integer number is put in each area of the Matrix.
Every time yifenfei should to do is that choose a detour which frome the top left point to the bottom right point and than back to the top left point with the maximal values of sum integers that area of Matrix yifenfei choose. But from the top to the bottom can only choose right and down, from the bottom to the top can only choose left and up. And yifenfei can not pass the same area of the Matrix except the start and end.
InputThe input contains multiple test cases.
Each case first line given the integer n (2<n<30)
Than n lines,each line include n positive integers.(<100)
OutputFor each test case output the maximal values yifenfei can get.
Sample Input
210 35 10310 3 32 5 36 7 1051 2 3 4 52 3 4 5 63 4 5 6 74 5 6 7 85 6 7 8 9
Sample Output
284680
和HPU3376一模一樣, 但是資料比較弱,果斷水了。
具體的解析請看這裡 :HPU3376
#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#define INF 0x3f3f3f3f#define maxn 800000 + 1000#define maxm 4000000 + 1000using namespace std;int n;int outset;int inset;struct node { int u, v, cap, flow, cost, next;};node edge[maxm];int head[maxn], cnt;int per[maxn];int dist[maxn], vis[maxn];int map[660][660];void init(){ cnt = 0; memset(head, -1, sizeof(head));}void add(int u, int v, int w, int c){ node E1 = {u, v, w, 0, c, head[u]}; edge[cnt] = E1; head[u] = cnt++; node E2 = {v, u, 0, 0, -c, head[v]}; edge[cnt] = E2; head[v] = cnt++;}int change(int x, int y){ return (x - 1) * n + y;}void getmap(){ int t = n * n; outset = 0; inset = n * n * 2 + 1; for(int i = 1; i <= n; ++i){ for(int j = 1; j <= n; ++j){ scanf("%d", &map[i][j]); if(i == 1 && j == 1 || i == n && j == n) add(change(i, j), change(i, j) + t, 2, map[i][j]); else add(change(i, j), change(i, j) + t, 1, map[i][j]); if(i + 1 <= n) add(change(i, j) + t, change(i + 1, j), 1, 0); if(j + 1 <= n) add(change(i, j) + t, change(i, j + 1), 1, 0); } } add(outset, 1, 2, 0); add(change(n, n) + t, inset, 2, 0);}bool SPFA(int st, int ed){ queue<int>q; for(int i = 0; i <= inset; ++i){ dist[i] = -INF; vis[i] = 0; per[i] = -1; } dist[st] = 0; vis[st] = 1; q.push(st); while(!q.empty()){ int u = q.front(); q.pop(); vis[u] = 0; for(int i = head[u]; i != -1; i = edge[i].next){ node E = edge[i]; if(dist[E.v] < dist[u] + E.cost && E.cap > E.flow){ dist[E.v] = dist[u] + E.cost; per[E.v] = i; if(!vis[E.v]){ vis[E.v] = 1; q.push(E.v); } } } } return per[ed] != -1;}void MCMF(int st, int ed, int &cost, int &flow){ flow = 0; cost = 0; while(SPFA(st, ed)){ int mins = INF; for(int i = per[ed]; i != -1; i = per[edge[i ^ 1].v]){ mins = min(mins, edge[i].cap - edge[i].flow); } for(int i = per[ed]; i != -1; i = per[edge[i ^ 1].v]){ edge[i].flow += mins; edge[i ^ 1].flow -= mins; cost += edge[i].cost * mins; } flow += mins; }}int main (){ while(scanf("%d", &n) != EOF){ init(); getmap(); int cost, flow; MCMF(outset, inset, cost, flow); cost = cost - map[1][1] - map[n][n]; printf("%d\n", cost); } return 0;}
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。