POJ 3206 最小生成树

DESCRIPTION:
T_T 在下是读不懂题意的。但是捏。现在知道是求把所有的点(是字母的点)连起来的最小的权值。即最小生成树。因为求最小生成树是不计较源点是哪个的。所以可以把A和S看成一样的。首先需要用BFS广搜算法求出任意两点之间的最短距离。然后直接用prim或kruskal算法模板就欧克了。但是捏。貌似这道题有两大坑。NO.1 输入row 和 col 两个数之后会有多余的空格。所以需要吃掉一个字符串而不是一个字符。NO.2 虽然题目说最多有100个外星人+1个源点。但是好像有102个点。这样的话。数组必须开到>= 102。

23333333....虽然只有这两个坑,但是bfs很混乱的我确实WA了一天多。

附bfs+prim代码。

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

时间: 2024-08-02 01:31:08

POJ 3206 最小生成树的相关文章

poj——2031 最小生成树(MST) Kruskal算法

poj——2031 最小生成树(MST)  Kruskal算法 Building a Space Station Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 4985   Accepted: 2503 Description You are a member of the space station engineering team, and are assigned a task in the constructio

poj 1251(最小生成树)

Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly, so the large road network is too expensi

Arctic Network (poj 2349 最小生成树)

Language: Default Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12397   Accepted: 4053 Description The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different

poj 1789 最小生成树 kruskal实现

题目链接:http://poj.org/problem?id=1789 1Y 题目大意:对于每一个点给你一个字符串,两个点之间的距离就是两个点所对应的字符串对应位置有几个不同的字符..找出最小生成树. 把Kruskal换成了用一个struct来记录边的两个点,以及这条边的距离.这样就不需要多开一个数组... 之后用一个结构体排序,让长度最小的边排到最前面.. 代码: #include <iostream> #include <cstdio> #include <cstring

POJ - 2485(最小生成树.prime)

题目链接: http://poj.org/problem?id=2485 题目: Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36525   Accepted: 16329 Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the

POJ - 1251(最小生成树.krustal)

题目链接:http://poj.org/problem?id=1251 题目: Jungle Roads Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 31876   Accepted: 14909 Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was s

POJ - 1258(最小生成树.prime)

题目链接: http://poj.org/problem?id=1258 题目: Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 68462   Accepted: 28369 Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet conn

poj 1251 最小生成树模板题

Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E 44E 2 F 60 G 38F 0G 1 H 35H 1 I 353A 2 B 10 C 40B 1 C 200Sample Output 21630 prim算法 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <

POJ 2485 最小生成树

2333333333 又是水题.prim模板直接水过.求最小生成树里的最大的边的权值. 附代码:// 如果我木猜错的话.是要求最小生成树的最大边值. #include<stdio.h>#include<string.h>#include<iostream>#define inf 0x1f1f1f1fusing namespace std; int n, t;int cost[520][520]; int prim(){    int low[510], vis[510]