Minimum Spanning Tree of POJ 3206 and poj3206 Spanning Tree

Source: Internet
Author: User

Minimum Spanning Tree of POJ 3206 and poj3206 Spanning Tree

DESCRIPTION:
T_T does not understand the meaning of the question. But pinch. Now we know that it is the minimum weight for connecting all vertices (the point of a letter. That is, the Minimum Spanning Tree. Because the Minimum Spanning Tree does not care about the Source Vertex. So we can regard A and S as the same. First, you need to use the BFS wide search algorithm to find the shortest distance between any two points. Then, you can use the prim or kruskal algorithm template to get the right of ouke. But pinch. It seems that this question has two major pitfalls. NO.1 there will be extra spaces after the numbers of row and col are entered. Therefore, you need to eat a string instead of a character. No. 2 although the subject says there are at most 100 aliens + 1 source point. But it seems that there are 102 points. In this case. The value of the array must be greater than or equal to 102.

23333333... although there are only these two pitfalls, but bfs is very messy, I did WA for more than a day.

Bfs + prim code.

# Include <iostream>
# Include <string>
# Include <string. h>
# Include <stdio. h>
Using namespace std;

Constint 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] [next_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;
}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.