wikioi 1002 搭橋

來源:互聯網
上載者:User

標籤:style   io   os   ar   for   sp   on   c   amp   

題意:這題剛開始看錯題意了,原來橋是建在一條直線上就行,不管距離多遠。

思路:dfs求第一問答案,然後最小產生樹搞,不能建橋的邊就設為INF就行了,然後如果用到INF的邊就加上0就行了。這樣跑一遍最小產生樹就是答案。

#pragma comment(linker, "/STACK:1024000000,1024000000")#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<map>#include<queue>#include<set>#include<cmath>#include<bitset>#define mem(a,b) memset(a,b,sizeof(a))#define lson i<<1,l,mid#define rson i<<1|1,mid+1,r#define llson j<<1,l,mid#define rrson j<<1|1,mid+1,r#define INF 0x7fffffff#define maxn 55typedef long long ll;typedef unsigned long long ull;using namespace std;int n,m,cnt,vis[maxn][maxn],mm[maxn][maxn],sum;int a[1000005],b[1000005],tmp,sum2,sum3;char s[maxn][maxn];int dist[8][2]= {1,0,1,1,1,-1,0,1,0,-1,-1,0,-1,1,-1,-1};int f[1000005];void dfs(int x,int y,int ans){    for(int i=0; i<8; i++)    {        int xx=x+dist[i][0],yy=y+dist[i][1];        if(xx>=0&&xx<n&&yy>=0&&yy<m&&!vis[xx][yy]&&s[xx][yy]=='#')        {            vis[xx][yy]=1;            f[mm[xx][yy]]=f[ans];            dfs(xx,yy,ans);        }    }}int find(int x){    return f[x]==x?x:f[x]=find(f[x]);}struct abc{    int u,v,w;    bool operator<(const abc &a)const    {        return w<a.w;    }} e[1000005];void kruscal(){    sort(e,e+tmp);    int sum=1;    for(int i=0; i<tmp&&sum!=cnt; i++)    {        int x=find(e[i].u),y=find(e[i].v);        if(x!=y)        {            sum++;            sum3+=(e[i].w==INF?0:e[i].w);            sum2++;            f[x]=y;        }    }}int main(){    scanf("%d%d",&n,&m);    for(int i=0; i<n; i++)        scanf("%s",s[i]);    mem(vis,0);    cnt=tmp=0;    mem(f,0);    mem(mm,0);    int sum1=0;    for(int i=0; i<n; i++)        for(int j=0; j<m; j++)            if(s[i][j]=='#')            {                mm[i][j]=++cnt;                a[cnt]=i,b[cnt]=j;                f[cnt]=cnt;            }    for(int i=1; i<=cnt; i++)        for(int j=i+1; j<=cnt; j++)        {            e[tmp].u=i,e[tmp].v=j;            if(abs(a[i]-a[j])<=1&&abs(b[i]-b[j])<=1)                e[tmp++].w=0;            else if(abs(a[i]-a[j])<=1)                e[tmp++].w=abs(b[i]-b[j])-1;            else if(abs(b[i]-b[j])<=1)                e[tmp++].w=abs(a[i]-a[j])-1;            else e[tmp++].w=INF;        }    for(int i=0; i<n; i++)        for(int j=0; j<m; j++)            if(s[i][j]=='#'&&!vis[i][j])            {                vis[i][j]=1;                dfs(i,j,mm[i][j]);                sum1++;            }    kruscal();    printf("%d\n%d %d\n",sum1,sum2,sum3);    return 0;}


wikioi 1002 搭橋

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.