HDU 3376 Matrix Again
2015-11-07 15:42
561 查看
Matrix Again
Time Limit: 2000msMemory 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解题:费用流
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int maxn = 601*602*2; struct arc { int to,flow,cost,next; arc(int x = 0,int y = 0,int z = 0,int nxt = -1) { to = x; flow = y; cost = z; next = nxt; } } e[3000010]; int head[maxn],d[maxn],p[maxn],tot,S,T; bool in[maxn]; void add(int u,int v,int flow,int cost) { e[tot] = arc(v,flow,cost,head[u]); head[u] = tot++; e[tot] = arc(u,0,-cost,head[v]); head[v] = tot++; } bool spfa() { memset(in,false,sizeof in); memset(d,-1,sizeof d); memset(p,-1,sizeof p); queue<int>q; d[S] = 0; q.push(S); while(!q.empty()) { int u = q.front(); q.pop(); in[u] = false; for(int i = head[u]; ~i; i = e[i].next) { if(e[i].flow && d[e[i].to] < d[u] + e[i].cost) { d[e[i].to] = d[u] + e[i].cost; p[e[i].to] = i; if(!in[e[i].to]) { in[e[i].to] = true; q.push(e[i].to); } } } } return p[T] > -1; } int solve(int ret = 0) { while(spfa()) { int mh = 1; //for(int i = p[T]; ~i; i = p[e[i^1].to]) //mh = min(mh,e[i].flow); for(int i = p[T]; ~i; i = p[e[i^1].to]) { e[i].flow -= mh; e[i^1].flow += mh; } ret += mh*d[T]; } return ret; } int main() { int n; while(~scanf("%d",&n)) { memset(head,-1,sizeof head); int ret = tot = 0; for(int i = 1; i <= n; ++i) for(int j = 1,val; j <= n; ++j) { scanf("%d",&val); int u = (i-1)*n + j,v = u + n*n; if(u == 1 || u == n*n) { add(u,v,2,val); ret -= val; } else add(u,v,1,val); if(i < n) add(v,u + n,1,0); if(j < n) add(v,u + 1,1,0); } S = 1; T = 2*n*n; printf("%d\n",solve() + ret); } return 0; }
View Code
相关文章推荐
- 欢迎使用CSDN-markdown编辑器
- ibm ds5020 for aix storage manager .
- failed to load the jni shared jvm.dll
- Daily Scrum (2015/11/6)
- [leetcode] Contains Duplicate 判断一数组是否有重复元素
- foreign key constraint fails错误的原因
- Should i Backup all my domain controllers
- Software Failure Is Organizational Failure
- 谷歌人工智能算法RankBrain运行原理解析
- 动态更改AIX操作系统SWAP空间
- 【Alpha】Daily Scrum Meeting第四次
- U盘无法安装win10提示Your PC/Device needs to be repaired
- U盘无法安装win10提示Your PC/Device needs to be repaired
- LightOJ - 1076 Get the Containers(二分)
- Daily Scrumming* 2015.11.6(Day 18)
- Daily Scrumming* 2015.11.5(Day 17)
- Libev源码分析06:异步信号同步化--sigwait、sigwaitinfo、sigtimedwait和signalfd
- Learning Multi-Domain Convolutional Neural Networks for Visual Tracking 笔记
- 【Codeforces Round 329 (Div 2) B】【映射 排序 贪心】Anton and Lines 查看所有直线pair在[x1,x2]范围内是否有交点
- poj 3691 DNA repair(AC自动机+DP)