Uvalive-7303-aquarium "minimum Spanning tree" "Connected block"

Source: Internet
Author: User

Uvalive-7303-aquarium


Topic Links: 7303

The main idea: to give you a R * C lattice, each lattice has a ' \ ' or '/' wall, as well as the cost of knocked out the wall, asked to make all the block Unicom minimum cost. (see picture to understand)

The idea of the topic: is to turn him into a diagram, Unicom's block equivalent to a point, two points between a side, the weight of the side of the wall for the cost.

The idea of converting to connected blocks is that each lattice is viewed as two parts, left and right. In a row, suppose that two lattices a, B. Then the number on the right side of the B lattice must be the same as the number on the left of the a lattice.

Mark the left and right of each lattice, then add the edge, and the two endpoints of the edge are the two numbers of a lattice. Weight is the cost of the wall

Then process the edges between rows and rows, and a little discussion, assuming that the top lattice is a and the bottom lattice is b. So if A is '/' and B is '/', then the right lattice of a and B's left lattice are interlinked, at this time add an edge, the weight value is set to 0 can be.

Note the array size!!!

Here's the code:

#include <iostream>#include <iomanip>#include <fstream>#include <sstream>#include <cmath>#include <cstdio>#include <cstring>#include <cctype>#include <algorithm>#include <functional>#include <numeric>#include <string>#include <set>#include <map>#include <stack>#include <vector>#include <queue>#include <deque>#include <list>using namespace STD;typedef Long Longllstrings[ the];intval[ the][ the];structnode{intb;} g[ the][ the];structnode2{intA,b,len;} edge[40005];intfa[20005];intKk//======= minimum spanning tree = = =voidAddintUintVintW) {edge[kk].a = u;    edge[kk].b = v; Edge[kk++].len = w;}BOOLCMP (Node2 A,node2 b) {returnA.len < B.len;}intGetfather (intV) {return(Fa[v] = = v)? V:FA[V] = Getfather (Fa[v]);}voidMergesintXintY) {x = Getfather (x); y = Getfather (y);if(x! = y) fa[x] = y;}//==============intMain () {intTCin>> T;intLllll =1; while(t--) {intC,r;Cin>> c >> R; KK =0; for(inti =0; I < C; i++) {Cin>> S[i]; } for(inti =0; I < C; i++) { for(intj =0; J < R; J + +) {Cin>> Val[i][j]; }        }intp =1; for(inti =0; I < C; i++) { for(intj =0; J < R; J + +) {if(J = =0) {g[i][j].a = p++;                    g[i][j].b = p++;                Add (G[i][j].a,g[i][j].b,val[i][j]); }Else{g[i][j].a = g[i][j-1].b;                    g[i][j].b = p++;                Add (G[i][j].a,g[i][j].b,val[i][j]); }if(I! =0)                {if(S[i-1][J] = ='/')                    {if(S[i][j] = ='/') {Add (G[i][j].a,g[i-1][J].B,0); }Else{Add (G[i][j].b,g[i-1][J].B,0); }                    }Else{if(S[i][j] = ='/') {Add (G[i][j].a,g[i-1][J].A,0); }Else{Add (G[i][j].b,g[i-1][J].A,0); }                    }                }            }        } for(inti =1; I <=20001;        i++) {Fa[i] = i; } sort (Edge,edge + kk,cmp);intAns =0; for(inti =0; i < KK; i++) {intu = edge[i].a;intv = edge[i].b;if(Getfather (u)! = Getfather (v))                {Merges (U,V);            Ans + = Edge[i].len; }        }printf("Case%d:", lllll++);cout<< ans << Endl; }}

Uvalive-7303-aquarium "minimum Spanning tree" "Connected block"

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.