POJ3422 Kaka's Matrix Travels
4014 단어 Matrix
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 4905
Accepted: 1910
Description
On an N × N chessboard with a non-negative number in each grid, Kaka starts his matrix travels with SUM = 0. For each travel, Kaka moves one rook from the left-upper grid to the right-bottom one, taking care that the rook moves only to the right or down. Kaka adds the number to SUM in each grid the rook visited, and replaces it with zero. It is not difficult to know the maximum SUM Kaka can obtain for his first travel. Now Kaka is wondering what is the maximum SUM he can obtain after his Kth travel. Note the SUM is accumulative during the K travels.
Input
The first line contains two integers N and K (1 ≤ N ≤ 50, 0 ≤ K ≤ 10) described above. The following N lines represents the matrix. You can assume the numbers in the matrix are no more than 1000.
Output
The maximum SUM Kaka can obtain after his Kth travel.
Sample Input
3 2
1 2 3
0 2 1
1 4 2
Sample Output
15
************************************************************
: n*n , , , SUM 。 K SUM。
: 。 , spfa 。。。 ; 。
。 , x x' 。 , , :x x' , 1, , INF, 0. ,AC 。
#include <stdio.h>
#include <string.h>
#include <queue>
#define N 50050
#define M 10000005
#define INF 0x3f3f3f3f
#define tk(a,b,c) ((a-1)*n+b+c*n*n)
using namespace std;
int tu[55][55];
int n,k,eid,sink,source;
int head[N],ed[M],fee[M],nxt[M],up[M];
int pre[N],vis[N],dist[N],road[N];
void add_edge(int s,int e,int f,int u)
{
up[eid]=u;
ed[eid]=e; nxt[eid]=head[s];
fee[eid]=f; head[s]=eid++;
up[eid]=0;
ed[eid]=s; fee[eid]=-f;
nxt[eid]=head[e]; head[e]=eid++;
}
int spfa(void)
{
queue<int>que;
memset(vis,0,sizeof(vis));
for(int i=1;i<=sink;i++)
dist[i]=-1;
dist[0]=0;
vis[0]=1;que.push(0);
while(!que.empty())
{
int t=que.front();
que.pop();
vis[t]=0;
for(int i=head[t];~i;i=nxt[i])
{
if(up[i]==0)continue;
int e=ed[i],f=fee[i];
if(dist[e]<dist[t]+f)
{
dist[e]=dist[t]+f;
road[e]=i;
pre[e]=t;
if(!vis[e])
{
vis[e]=1;
que.push(e);
}
}
}
}
if(pre[sink]!=-1&&dist[sink]>-1)
return 1;
return 0;
}
void re(void)
{
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&tu[i][j]);
}
void run(void)
{
memset(head,-1,sizeof(head));
eid=0;source=0;sink=2*n*n+1;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
add_edge(tk(i,j,0),tk(i,j,1),tu[i][j],1);
add_edge(tk(i,j,0),tk(i,j,1),0,k-1);
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
if(n-i)
add_edge(tk(i,j,1),tk(i+1,j,0),0,k);
if(n-j)
add_edge(tk(i,j,1),tk(i,j+1,0),0,k);
}
add_edge(source,tk(1,1,0),0,k);
add_edge(tk(n,n,1),sink,0,k);
int ans=0;
int vis[N];
while(spfa())
for(int j=sink;j!=source;j=pre[j])
{
up[road[j]]-=1;
up[road[j]^1]+=1;
ans+=fee[road[j]];
}
printf("%d
",ans);
}
int main()
{
while(scanf("%d%d",&n,&k)==2)
{
re();
run();
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Matrix RGB LED Funduino를 Colorduino에서 사용Matrix LED가 장착된 ATmega328P 보드 Funduino를 사용해 봅시다. Funduino는 Colorduino, RGB LED 8x8 Matrix Board with Driver Shield for A...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.