zoukankan      html  css  js  c++  java
  • poj 3026 Borg Maze (最小生成树+bfs)

      有几个错误,调试了几个小时,样例过后 1Y.

    题目:http://poj.org/problem?id=3026

    题意:就是让求A们和S的最小生成树

    先用bfs找每两点的距离,再建树。没剪枝 63MS。

      1 #include <iostream>
      2  #include<cstdio>
      3  #include<cstring>
      4  #include<cstdlib>
      5  #include<stack>
      6  #include<queue>
      7  #include<cmath>
      8  #include<algorithm>
      9  using namespace std;
     10  
     11  char G[60][60];
     12  int dx[5]={0,0,1,-1};
     13  int dy[5]={1,-1,0,0};
     14  int r,c,bin[10010],an,ff[150][150]; //an表示边数
     15  struct node
     16  {
     17      int x,y;
     18  }e[150];   //表示点
     19  
     20  struct tree
     21  {
     22      int u,v,w;
     23  }q[11000];//边及权值
     24  
     25  struct point
     26  {
     27      int x,y,step;
     28  }next,pos;//bfs 里的队列
     29  
     30  void add(int u,int v,int w)//加边
     31  {
     32       q[an].u=u; q[an].v=v; q[an++].w=w;
     33  }
     34  
     35  void bfs(int f,int x,int y)
     36  {
     37      int vis[60][60],i;
     38       queue<point>qu;
     39      memset(vis,0,sizeof(vis));
     40      next.x=x; next.y=y; next.step=0;
     41      qu.push(next);
     42      vis[x][y]=1;
     43      while(!qu.empty())
     44      {
     45          pos=qu.front();
     46          qu.pop();
     47          for(i=0; i<4; i++)
     48          {
     49              next.x=pos.x+dx[i]; next.y=pos.y+dy[i]; next.step=pos.step+1;
     50              if(next.x>=0 && next.x<r && next.y>=0 && next.y<c && vis[next.x][next.y]==0 && G[next.x][next.y]!='#')
     51              {
     52                  vis[next.x][next.y]=1;
     53                  qu.push(next);
     54                  if(G[next.x][next.y]=='A'||G[next.x][next.y]=='S')
     55                    add(f,ff[next.x][next.y],next.step);
     56              }
     57          }
     58      }
     59  }
     60  int cmp(const void *a,const void *b)
     61  {
     62      return (*(tree *)a).w-(*(tree *)b).w;
     63  }
     64  int find(int a)
     65  {
     66     if(a==bin[a])
     67     return a;
     68     else
     69     bin[a]=find(bin[a]);
     70  };
     71  int main()
     72  {
     73      int t,cnt,i,j,x,y,num,sum;//cnt表示点的个数
     74      char str[200];
     75      cin>>t;
     76      while(t--)
     77      {
     78          sum=0; num=0; an=0;
     79          cin>>c>>r;
     80          gets(str);
     81          for(i=0; i<r; i++)
     82          gets(G[i]);
     83  
     84          cnt=0;
     85          for(i=0; i<r; i++)
     86          for(j=0; j<c; j++)
     87          if(G[i][j]=='S'||G[i][j]=='A')
     88          {
     89            ff[i][j]=cnt;
     90            e[cnt].x=i; e[cnt++].y=j;
     91          }
     92  
     93          for(i=0; i<cnt; i++)
     94          {
     95              bfs(i,e[i].x,e[i].y);
     96          }
     97  
     98          for(i=0; i<cnt; i++)
     99          bin[i]=i;
    100          qsort(q,an,sizeof(q[0]),cmp);
    101          for(i=0; i<=an-1; i++)
    102          {
    103              x=find(q[i].u); y=find(q[i].v);
    104              if(x!=y)
    105              {
    106                  sum+=q[i].w;
    107                  num++;
    108                  bin[x]=y;
    109              }
    110              if(num==cnt-1)
    111              break;
    112          }
    113          printf("%d
    ",sum);
    114      }
    115  }
    116  
  • 相关阅读:
    CSS浮动元素的水平居中
    html5移动web开发实战必读书记
    再说CSS3渐变——线性渐变
    链栈学习笔记
    用数组实现从文件搜索帐户和验证密码
    启程!
    在Application中集成Microsoft Translator服务之获取访问令牌
    在Application中集成Microsoft Translator服务之开发前准备
    Kinetic使用注意点--canvas
    Kinetic使用注意点--blob
  • 原文地址:https://www.cnblogs.com/bfshm/p/3253262.html
Copyright © 2011-2022 走看看