Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

居然不止100个点。一次RE。 然后 输入了X Y之后莫名其妙的空格。一次WA。。。就改了这两个地方就AC了。。。

Posted by dongshimou at 2014-07-06 02:04:58 on Problem 3026 and last updated at 2014-07-06 02:06:10
Kruskal+BFS


#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<iostream>
#include<list>
#include<set>
#include<cmath>
#define INF 0x7fffffff
#define eps 1e-6
using namespace std;
int n,m,xl,yl;
int cot;
int xx[]= {-1,1,0,0};
int yy[]= {0,0,-1,1};
struct lx
{
    int u,v,len;
} l[50*1001];
int fa[1001];

bool vis[151][151];
char g[151][151];
int site[151][151];
struct node
{
    int x,y,lv;
} point[1001];
bool cmp(lx a,lx b)
{
    return a.len<b.len;
}
int father(int x)
{
    if(x!=fa[x])
        return fa[x]=father(fa[x]);
}
void BFS(node S)
{
    memset(vis,0,sizeof(vis));
    queue<node>q;
    q.push(S);
    vis[S.x][S.y]=1;
    int u=site[S.x][S.y];
    int num=1;
    while(!q.empty())
    {
        node now,tmp;
        tmp=q.front();q.pop();
        int v=site[tmp.x][tmp.y];
        if(v>=1&&u!=v)
        {
            l[cot].u=u;
            l[cot].v=v;
            l[cot++].len=tmp.lv;
            num++;
        }
        if(num==n)break;
        for(int k=0;k<4;k++)
        {
            int x=tmp.x+xx[k];
            int y=tmp.y+yy[k];
            if(y>=yl||y<0||x>=xl||x<0||g[x][y]=='#'||vis[x][y])
                continue;
            vis[x][y]=1;
            now.lv=tmp.lv+1;
            now.x=x,now.y=y;
            q.push(now);
        }
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&xl,&yl);
        char str[51];
        memset(site,0,sizeof(site));
        n=1;
        gets(str);
        for(int i=0; i<yl; i++)
        {
            gets(str);
            for(int j=0; j<xl; j++)
            {
                g[i][j]=str[j];
                if(str[j]=='A'||str[j]=='S')
                {
                    site[i][j]=n;
                    point[n].x=i,point[n].y=j;
                    point[n++].lv=0;

                }
            }
        }
        cot=0;
        n--;
        for(int i=1; i<=n; i++)
            BFS(point[i]);
        for(int i=1; i<=n; i++)
            fa[i]=i;
        sort(l,l+cot,cmp);
        int ans=0;
        for(int i=0;i<cot;i++)
        {
            int fu=father(l[i].u);
            int fv=father(l[i].v);
            if(fu==fv)continue;
            fa[fv]=fu;
            ans+=l[i].len;
        }
        printf("%d\n",ans);
    }

}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator