Matrix Again
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)Total Submission(s): 4370 Accepted Submission(s): 1273
Problem Description
Starvae 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 starvae should to do is that choose a detour which from 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 starvae 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 starvae can not pass the same area of the Matrix except the start and end..
Do you know why call this problem as “Matrix Again”? AS it is like the problem 2686 of HDU.
Every time starvae should to do is that choose a detour which from 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 starvae 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 starvae can not pass the same area of the Matrix except the start and end..
Do you know why call this problem as “Matrix Again”? AS it is like the problem 2686 of HDU.
Input
The input contains multiple test cases.
Each case first line given the integer n (2<=n<=600)
Then n lines, each line include n positive integers. (<100)
Each case first line given the integer n (2<=n<=600)
Then n lines, each line include n positive integers. (<100)
Output
For each test case output the maximal values starvae can get.
Sample Input
2 10 3 5 10 3 10 3 3 2 5 3 6 7 10 5 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 5 6 7 8 9
Sample Output
28 46 80
以左上角为源点,右下角为汇点。对于每个点拆成两点,两点间连一条容量为1花费为cost的边,注意起点和终点都能走两次,因此对于起点和终点,拆成的两点间再连一条容量为1且话费为0的边,对于每个cost都取反,则转化为最小费用最大流。
#include<stdio.h> #include<algorithm> #include<string.h> #include<queue> using namespace std; #define INF 0x3f3f3f3f const int maxm = 1000005; struct node { int u, v, cap, cost, next; }edge[maxm*2]; int vis[maxm], pre[maxm], head[maxm], dis[maxm]; int n, m, s, t, cnt; void init() { cnt = 0, s = 0, t = ((n - 1)*n + n - 1) * 2 + 1; memset(head, -1, sizeof(head)); } void add(int u, int v, int cap, int cost) { edge[cnt].u = u, edge[cnt].v = v; edge[cnt].cap = cap, edge[cnt].cost = cost; edge[cnt].next = head[u], head[u] = cnt++; edge[cnt].u = v, edge[cnt].v = u; edge[cnt].cap = 0, edge[cnt].cost = -cost; edge[cnt].next = head[v], head[v] = cnt++; } int bfs() { memset(pre, -1, sizeof(pre)); queue<int>q; for (int i = 0;i <= t;i++) dis[i] = INF; dis[s] = 0;q.push(s); while (!q.empty()) { int u = q.front();q.pop(); for (int i = head[u];i != -1;i = edge[i].next) { int v = edge[i].v; if (dis[v] > dis[u] + edge[i].cost&&edge[i].cap) { dis[v] = dis[u] + edge[i].cost; pre[v] = i; q.push(v); } } } if (dis[t] == INF) return 0; return 1; } int MCMF() { int ans = 0, minflow; while (bfs()) { minflow = INF; for (int i = pre[t];i != -1;i = pre[edge[i].u]) minflow = min(minflow, edge[i].cap); for (int i = pre[t];i != -1;i = pre[edge[i].u]) { edge[i].cap -= minflow; edge[i ^ 1].cap += minflow; } ans += dis[t] * minflow; } return ans; } int main() { int i, j, k, a; while (scanf("%d", &n) != EOF) { init(); for (i = 0;i < n;i++) { for (j = 0;j < n;j++) { scanf("%d", &a); add((i*n + j) * 2, (i*n + j) * 2 + 1, 1, -a); if (i != n - 1) add((i*n + j) * 2 + 1, ((i + 1)*n + j) * 2, 1, 0); if (j != n - 1) add((i*n + j) * 2 + 1, (i*n + j + 1) * 2, 1, 0); } } add(s, s + 1, 1, 0); add(t - 1, t, 1, 0); printf("%d\n", -MCMF()); } return 0; }