DESCRIPTION:
T_t in the next is not understand test instructions. But pinch. It is now known to be the smallest weight that is asked to connect all the dots (the dots of letters). That is, the minimum spanning tree. Because the minimum spanning tree is not about which source point is. So you can think of a and s as the same. First, we need to find out the shortest distance between any two points by using BFS wide search algorithm. Then directly using the prim or Kruskal algorithm template on the European gram. But pinch. It seems that there are two big holes in the problem. There are extra spaces after the first input row and col two numbers. So you need to eat a string instead of a character. No.2 Although the topic says there are up to 100 Aliens + 1 source points. But there seems to be 102 points. In that case. The array must be open to >= 102.
23333333 .... Although there are only two pits, but the BFS is very chaotic I really WA for a day more.
Bfs+prim code is attached.
#include <iostream>
#include <string>
#include <string.h>
#include <stdio.h>
using namespace Std;
const int inf=2501;
Char map[51][51];
int node[51][51];
int num;
int edge[102][102];
int x, y;
void BFs (int II, int JJ)
{
/***initial***/
int dist[55][55];
int que_x[2500], que_y[2500];
int head, tail;
int move[4][2] = {1, 0,-1, 0, 0, 1, 0,-1};
int vis[55][55];
memset (Vis, 0, sizeof (VIS));
Head = 0;
tail = 0;
memset (Dist, 0, sizeof (Dist));
Que_x[tail] = II;
que_y[tail++] = JJ;
VIS[II][JJ] = 1;
while (Head < tail)
{
int top_x = Que_x[head];
int top_y = que_y[head++];
if (Node[top_x][top_y])
Edge[node[ii][jj]][node[top_x][top_y]] = dist[top_x][top_y];
for (int k=0; k<4; ++k)
{
int next_x = top_x + move[k][0];
int next_y = top_y + move[k][1];
if (next_x >= 0 && next_x < y && next_y >= 0 && next_y < x &&!vis[next_x][nex T_y] && map[next_x][next_y]! = ' # ')
{
Vis[next_x][next_y] = 1;
Dist[next_x][next_y] = dist[top_x][top_y] + 1;
Que_x[tail] = next_x;
que_y[tail++] = next_y;
}
}
}
Return
}
int prim (void)
{
int S=1;
int m=1;
BOOL u[102];
U[s]=true;
int min_w;
int prim_w=0;
int point;
int low_dis[102];
for (int i=1;i<=num;i++)
{
Low_dis[i]=inf;
U[i]=false;
}
while (true)
{
if (m==num)
Break
Min_w=inf;
for (int i=2;i<=num;i++)
{
if (!u[i] && low_dis[i]>edge[s][i])
Low_dis[i] = Edge[s][i];
if (!u[i] && min_w>low_dis[i])
{
Min_w=low_dis[i];
point=i;
}
}
S=point;
U[s]=true;
Prim_w+=min_w;
m++;
}
return prim_w;
}
int main (int i,int j)
{
int test;
cin>>test;
while (test--)
{
memset (node,0,sizeof (node));
num=0;
cin>>x>>y;
Char temp[20];
Gets (temp);
for (i=0;i<y;i++)
{
Gets (Map[i]);
for (j=0;j<x;j++)
{
if (map[i][j]== ' A ' | | map[i][j]== ' S ')
Node[i][j]=++num;
}
}
for (i=0;i<y;i++)
for (j=0;j<x;j++)
if (Node[i][j])
BFS (I,J);
Cout<<prim () <<endl;
}
return 0;
}
POJ 3206 minimum Spanning tree