8649 breadth Traversal of graphs

Source: Internet
Author: User

8649 breadth Traversal of graphs

Time limit: 1000MS memory limit: 1000K
Number of submissions: 1573 Hits: 975

Question types: programming language: g++; Gcc

Description
By using the graph's depth traversal to implement the adjacency table storage structure and basic operation functions, the breadth traversal algorithm of graphs is implemented and tested. Note Use the queue storage structure correctly.




Input format
First line: Enter an integer between 0 and 3 (a forward graph: 0, with mesh: 1, no direction: 2, no mesh: 3); The second line: Enter the number of vertices and the number of edges; the third line: Enter the values of each vertex (character, length 〈3); (Traverse starts at the first vertex of the input) line fourth: Enter each arc (EDGE) arc tail and arc head ( With a space as the interval), if the net also to enter the weight value;



Output format
Results of output-to-graph breadth traversal



Input sample
3a B CA BB cc b



Output sample
A b C



Tips



Author

Yqm

SCAU data structure Class A OJ problem,,, because lazy. So do not want to follow the school sent the data structure of the method to do, and do not test efficiency, so in the topic directly with the STL queue<> and Stack<> Instead of the list of queues and adjacency tables to be used in the wide search.

  

1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <cstring>5#include <cmath>6#include <cstdlib>7#include <cctype>8#include <queue>9#include <stack>Ten#include <map> One#include <vector> A#include <Set> -#include <utility> - #definell Long Long the #defineINF 0x3f3f3f3f - using namespacestd; -  -typedefstructNode//the node that indicates the vertex, of course, can also be used directly with string + { -     Charname[4]; + } node; A node temp; atStack<node> q[100005];//Here we use stacks instead of the linked list that each vertex extends . - Chart1[4],t2[4]; - intbook[100005]; - intVCharA[])//This function is used to record the weights corresponding to the string of the node . - { -     if(Strlen (a) = =1) in         returna[0]; -     Else to         returna[0]*122+a[1]; + } - intMain () the { *     //freopen ("Input.txt", "R", stdin); $memset (book,0,sizeof(book));Panax Notoginseng     inttype; -scanf"%d",&type); the     intn,m,w; + node first; Ascanf"%d%d",&n,&m); the      for(intI=0; i<n; i++) +     { -scanf"%s", T1); $         if(i==0) $         { - strcpy (FIRST.NAME,T1); -         } the     } -      while(m--)Wuyi     { the         if(type==1|| type==3) -scanf"%d%s%s",&w,t1,t2); Wu         Else -scanf"%s%s", t1,t2); About         if(type==2|| type==3)//if it is a graph without direction $         { - strcpy (TEMP.NAME,T1); - q[v (T2)].push (temp); - strcpy (TEMP.NAME,T2); A q[v (t1)].push (temp); +         } the         Else //If there is a graph -         { $ strcpy (TEMP.NAME,T2); the q[v (t1)].push (temp); the         } the     } the     // -     inttt; inQueue<node> TQ;//queue to be used for wide search the Tq.push (first); the      while(!tq.empty ()) About     { thett=V (Tq.front (). name); the         if(!book[tt])//Mark whether the vertex has been accessed the         { +printf"%s", Tq.front (). name); -book[tt]=1; the         }Bayi          while(!q[tt].empty ())//joins all out edges of the point to the queue the         { the Tq.push (Q[tt].top ()); - Q[tt].pop (); -         } the Tq.pop (); the     } the     return 0; the}

8649 breadth Traversal of graphs

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.