HDU 3376 Matrix Again

Matrix Again

Time Limit: 2000ms

Memory Limit: 102400KB

This problem will be judged on HDU. Original ID: 3376
64-bit integer IO format: %I64d      Java class name: Main

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.

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)

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

Source

HDOJ Monthly Contest – 2010.04.04

解题:费用流

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int INF = 0x3f3f3f3f;
 4 const int maxn = 601*602*2;
 5 struct arc {
 6     int to,flow,cost,next;
 7     arc(int x = 0,int y = 0,int z = 0,int nxt = -1) {
 8         to = x;
 9         flow = y;
10         cost = z;
11         next = nxt;
12     }
13 } e[3000010];
14 int head[maxn],d[maxn],p[maxn],tot,S,T;
15 bool in[maxn];
16 void add(int u,int v,int flow,int cost) {
17     e[tot] = arc(v,flow,cost,head[u]);
18     head[u] = tot++;
19     e[tot] = arc(u,0,-cost,head[v]);
20     head[v] = tot++;
21 }
22 bool spfa() {
23     memset(in,false,sizeof in);
24     memset(d,-1,sizeof d);
25     memset(p,-1,sizeof p);
26     queue<int>q;
27     d[S] = 0;
28     q.push(S);
29     while(!q.empty()) {
30         int u = q.front();
31         q.pop();
32         in[u] = false;
33         for(int i = head[u]; ~i; i = e[i].next) {
34             if(e[i].flow && d[e[i].to] < d[u] + e[i].cost) {
35                 d[e[i].to] = d[u] + e[i].cost;
36                 p[e[i].to] = i;
37                 if(!in[e[i].to]) {
38                     in[e[i].to] = true;
39                     q.push(e[i].to);
40                 }
41             }
42         }
43     }
44     return p[T] > -1;
45 }
46 int solve(int ret = 0) {
47     while(spfa()) {
48         int mh = 1;
49         //for(int i = p[T]; ~i; i = p[e[i^1].to])
50             //mh = min(mh,e[i].flow);
51         for(int i = p[T]; ~i; i = p[e[i^1].to]) {
52             e[i].flow -= mh;
53             e[i^1].flow += mh;
54         }
55         ret += mh*d[T];
56     }
57     return ret;
58 }
59 int main() {
60     int n;
61     while(~scanf("%d",&n)) {
62         memset(head,-1,sizeof head);
63         int ret = tot = 0;
64         for(int i = 1; i <= n; ++i)
65             for(int j = 1,val; j <= n; ++j) {
66                 scanf("%d",&val);
67                 int u = (i-1)*n + j,v = u + n*n;
68                 if(u == 1 || u == n*n) {
69                     add(u,v,2,val);
70                     ret -= val;
71                 } else add(u,v,1,val);
72                 if(i < n) add(v,u + n,1,0);
73                 if(j < n) add(v,u + 1,1,0);
74             }
75         S = 1;
76         T = 2*n*n;
77         printf("%d\n",solve() + ret);
78     }
79     return 0;
80 }

时间: 2024-11-03 22:39:27

HDU 3376 Matrix Again的相关文章

POJ 2135 Farm Tour &amp;&amp; HDU 2686 Matrix &amp;&amp; HDU 3376 Matrix Again 费用流求来回最短路

累了就要写题解,最近总是被虐到没脾气. 来回最短路问题貌似也可以用DP来搞,不过拿费用流还是很方便的. 可以转化成求满流为2 的最小花费.一般做法为拆点,对于 i 拆为2*i 和 2*i+1,然后连一条流量为1(花费根据题意来定) 的边来控制每个点只能通过一次. 额外添加source和sink来控制满流为2. 代码都雷同,以HDU3376为例. #include <algorithm> #include <iostream> #include <cstring> #in

hdu 3376 Matrix Again【最大费用流】

Matrix Again Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others) Total Submission(s): 2947    Accepted Submission(s): 860 Problem Description Starvae very like play a number game in the n*n Matrix. A positive integer

HDU 3376 Matrix Again(最大费用最大流)HDU2686加强题

Matrix Again Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others) Total Submission(s): 3206    Accepted Submission(s): 937 Problem Description Starvae very like play a number game in the n*n Matrix. A positive integer

HDU 2686 Matrix 3376 Matrix Again(费用流)

HDU 2686 Matrix 题目链接 3376 Matrix Again 题目链接 题意:这两题是一样的,只是数据范围不一样,都是一个矩阵,从左上角走到右下角在从右下角走到左上角能得到最大价值 思路:拆点,建图,然后跑费用流即可,不过HDU3376这题,极限情况是300W条边,然后卡时间过了2333 代码: #include <cstdio> #include <cstring> #include <vector> #include <queue> #i

hdu 2686 Matrix &amp;&amp; hdu 3367 Matrix Again (最大费用最大流)

Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1394    Accepted Submission(s): 758 Problem Description Yifenfei very like play a number game in the n*n Matrix. A positive integer number

HDU 4902 Matrix multiplication

点击打开链接 Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 2113    Accepted Submission(s): 956 Problem Description Given two matrices A and B of size n×n, find the product o

HDU 2686 Matrix(最大费用最大流+拆点)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2686 和POJ3422一样 删掉K把汇点与源点的容量改为2(因为有两个方向的选择)即可 #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> const int ma

HDU多校赛第9场 HDU 4965Fast Matrix Calculation【矩阵运算+数学小知识】

难度上,,,确实,,,不算难 问题是有个矩阵运算的优化 题目是说给个N*K的矩阵A给个K*N的矩阵B(1<=N<=1000 && 1=<K<=6),先把他们乘起来乘为C矩阵,然后算C^(N*N) 相当于 ABABABABABABAB...=(AB)^(N*N) 不如 A(BA)^(N*N-1)B 因为BA乘得K*K的矩阵,K是比较小的 #include <cstdio> #include <cstdlib> #include <cstr

HDU 2830 Matrix Swapping II (DP,最大全1矩阵)

题意  给你一个n*m矩阵  每列都可以随便交换位置   求最优交换后最大的全1子矩阵 又是HDU 1505 1506的变种  但这个更容易了  因为每列都可以交换位置了   那么这一行中所有比i高的都可以与i相邻了  只需要统计这一行有多少个比i高就行了   可以在算出每一行后  把高度大的放前面去  用num[i]记录排序后的列原来的数  这样就有j列比h[i][num[j]]高了  最后的答案也就是max(j*h[i][num[j]]) #include<cstdio> #include