hdu 3376 Matrix again
来源:互联网 发布:男生发型软件下载 编辑:程序博客网 时间:2024/06/05 02:01
Matrix Again
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 368 Accepted Submission(s): 119
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
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
Author
Starvae
Source
HDOJ Monthly Contest – 2010.04.04
Recommend
lcy
花了一天多的时间做的网络流。。。。感动啊! 现在我来说一下建图过程吧,把矩阵中的每个点拆成两个点,v和v',之间连一条边 容量为1,花费为-matrix[i][j];然后v'在与v+1 和v+n的点连一条边!(v+1,v+n保证在矩阵中是存在的!否则不连)然后在源点1和源点1‘连一条边,容量为2,花费为0;在汇点n*n和n*n‘连一条边,容量为2,花费为0;
因为一开始用全局VECTOR,每次做之前要全部清空!所以导致了我10来次的超时。后来索性不用了。改用覆盖的形式,用一个cnt[N]记录建图时放进来的边数;
然后就纯粹的最小费用最大流!
#include<iostream>
#include<queue>
#include<vector>
#include<memory>
using namespace std;
#define inf 0x7ffffff
const int N=721000;
#include<queue>
#include<vector>
#include<memory>
using namespace std;
#define inf 0x7ffffff
const int N=721000;
struct FUCK
{
int c,w,f;//c 容量 w花费 f流量
int next;//记录下一节点
}node,map[N][7];//map[N][4]这个就够大了,map[N][3]就错了
{
int c,w,f;//c 容量 w花费 f流量
int next;//记录下一节点
}node,map[N][7];//map[N][4]这个就够大了,map[N][3]就错了
//vector<FUCK> map[N];
int dia[605*605];
int n,tot;
int pre[N];
int dis[N];
bool flag[N];
int cnt[N];
int dia[605*605];
int n,tot;
int pre[N];
int dis[N];
bool flag[N];
int cnt[N];
int SPFA()
{
queue<int>Q;
//int temp;
int i,now;
pre[1] = 1;
for(i=2;i<=tot+tot;i++)
dis[i] = inf;
dis[1] = 0;
memset(flag,0,sizeof(flag));
flag[1] = true;
Q.push(1);
while(!Q.empty())
{
now = Q.front();
Q.pop();
flag[now] = false;
for(i=0;i<cnt[now];i++)
{
node = map[now][i];
if(node.c - node.f > 0 && dis[node.next] > dis[now] + node.w)
{
dis[node.next] = dis[now] + node.w;
pre[node.next] = now;
if(!flag[node.next])
{
Q.push(node.next);
flag[node.next] = true;
}
}
}
}
if(dis[tot+tot] != inf)
return 1;
else
return 0;
}
int mincostflow()
{
int i,j,min,ans=0;
while(SPFA())
{
//min = 0x7fffffff;
//min = 0x7fffffff;
/*for(i=tot+tot;i!=1;i=pre[i])
for(j=0;j<map[pre[i]].size();j++)
if(map[pre[i]][j].next == i)
{
min = min < (map[pre[i]][j].c - map[pre[i][j]
*/
for(i=tot+tot;i!=1;i=pre[i])
for(j=0;j<cnt[pre[i]];j++)
if(map[pre[i]][j].next == i)
{
map[pre[i]][j].f+=1;
node.c=0;
node.f=-1;
node.next=pre[i];
node.w = -map[pre[i]][j].w;
// if(i!=tot+tot&&i!=tot)
map[i][cnt[i]++] = node;
}
ans += 1*dis[tot+tot];
}
return ans;
}
{
int i,j,min,ans=0;
while(SPFA())
{
//min = 0x7fffffff;
//min = 0x7fffffff;
/*for(i=tot+tot;i!=1;i=pre[i])
for(j=0;j<map[pre[i]].size();j++)
if(map[pre[i]][j].next == i)
{
min = min < (map[pre[i]][j].c - map[pre[i][j]
*/
for(i=tot+tot;i!=1;i=pre[i])
for(j=0;j<cnt[pre[i]];j++)
if(map[pre[i]][j].next == i)
{
map[pre[i]][j].f+=1;
node.c=0;
node.f=-1;
node.next=pre[i];
node.w = -map[pre[i]][j].w;
// if(i!=tot+tot&&i!=tot)
map[i][cnt[i]++] = node;
}
ans += 1*dis[tot+tot];
}
return ans;
}
void makegraph()
{
int i,j;
int temp;
tot = n*n;
node.c=2;node.f=0;node.w=0;node.next=tot+1;
map[1][cnt[1]++] = node;
node.c=2;node.f=0;node.w=0;node.next=tot+tot;
map[tot][cnt[tot]++] = node;
for(i=1;i<=n*n;i++)
{
node.c = 1;
node.w=-dia[i];
node.f=0;
node.next=tot+i;
map[i][cnt[i]++] = node;
}
for(i=tot+1;i<=tot+tot;i++)
{
node.w = 0;
node.f = 0;
node.c = 1;
temp = i-tot;
if(temp%n!=0)
{
node.next=temp+1;
map[i][cnt[i]++] = node;
}
if(temp+n<=tot)
{
node.next=temp+n;
map[i][cnt[i]++] = node;
}
}
}
{
int i,j;
int temp;
tot = n*n;
node.c=2;node.f=0;node.w=0;node.next=tot+1;
map[1][cnt[1]++] = node;
node.c=2;node.f=0;node.w=0;node.next=tot+tot;
map[tot][cnt[tot]++] = node;
for(i=1;i<=n*n;i++)
{
node.c = 1;
node.w=-dia[i];
node.f=0;
node.next=tot+i;
map[i][cnt[i]++] = node;
}
for(i=tot+1;i<=tot+tot;i++)
{
node.w = 0;
node.f = 0;
node.c = 1;
temp = i-tot;
if(temp%n!=0)
{
node.next=temp+1;
map[i][cnt[i]++] = node;
}
if(temp+n<=tot)
{
node.next=temp+n;
map[i][cnt[i]++] = node;
}
}
}
int main()
{
int i;
while(scanf("%d",&n)!=EOF)
{
// for(i=1;i<=n*n*2;i++)
// map[i].clear();
memset(cnt,0,sizeof(cnt));
for(i=1;i<=n*n;i++)
scanf("%d",&dia[i]);
makegraph();
cout<<-mincostflow()<<endl;
}
return 0;
}
{
int i;
while(scanf("%d",&n)!=EOF)
{
// for(i=1;i<=n*n*2;i++)
// map[i].clear();
memset(cnt,0,sizeof(cnt));
for(i=1;i<=n*n;i++)
scanf("%d",&dia[i]);
makegraph();
cout<<-mincostflow()<<endl;
}
return 0;
}
- hdu 3376 Matrix again
- hdu 3376 Matrix Again
- hdu 2686 Matrix hdu 3376 Matrix Again 费用流
- HDU 2686 Matrix HDU 3376 Matrix Again 费用流
- HDU 2686 Matrix && HDU 3376 Matrix Again(最大费用)
- HDU 3376 Matrix Again 费用流
- hdu 3376 Matrix Again【最大费用流】
- HDU 3376 Matrix Again(费用流)
- HDU 3376 Matrix Again(费用流)
- hdu 3376 Matrix Again 最大费用流
- hdu 2386 Matrix// 3376 Matrix Again (费用流)
- HDU 2686 Matrix 3376 Matrix Again(费用流)
- hdu 2686 Matrix / 3376 Matrix Again最大费用流
- hdu 2686/hdu 3376 Matrix Again 费用流模板
- HDU 3376 Matrix Again (最小费用最大流)
- hdu 3376 Matrix Again(最小费用流)
- Matrix Again HDU 3376 最大费用最大流
- HDOJ 3376 Matrix Again
- cbo优化中的基数(cardinality)介绍
- 谈两种子网掩码的计算方法
- 多媒体资料整理----1
- 我的职业规划
- 10条可用性方面的启发
- hdu 3376 Matrix again
- PyOpenGL
- Linux和我的“流言蜚语”: 算个开始吧
- 计算几何常用算法(一共23个)
- I420 转 RGB24
- Java集合类详解
- Word 文件乱码修复方法大全
- ls v0.2
- 催出来的测试报告与测试的价值