Introduction to algorithms 22.6 professional runners

Source: Internet
Author: User
I. Question 2: Use BFs to solve problems 1. link_graph.h
# Include <iostream> # include <queue> using namespace STD; # define n 100 # define white 0 # define gray 1 # define black 2 Queue <int> q; class mat_graph {public: int N; int color [n + 1]; int d [n + 1]; int pie [n + 1]; int map [n + 1] [n + 1]; mat_graph (INT num): n (Num) {memset (MAP, 0, sizeof (MAP ));} void adddoubleedge (int A, int B, int value = 1) {addsingleedge (a, B, value); addsingleedge (B, A, value);} void addsingleedge (INT start, in T end, int value = 1) {map [start] [end] = value;} void deletedoubleedge (int A, int B) {deletesingleedge (A, B ); deletesingleedge (B, A);} void deletesingleedge (INT start, int end) {map [start] [end] = 0 ;} // 22.2-3 void BFS (int s); void print_path (int s, int v) ;}; void mat_graph: BFS (INT s) {int U, V; for (u = 1; U <= N; U ++) {color [u] = white; d [u] = 0x7fffffff; pie [u] = 0 ;} color [s] = gray; d [s] = 0; pie [s] = 0; while (! Q. Empty () Q. Pop (); q. Push (s); While (! Q. empty () {u = Q. front (); q. pop (); For (V = 1; v <= N; V ++) {If (Map [u] [v] = 0) continue; if (color [v] = white) {color [v] = gray; d [v] = d [u] + 1; pie [v] = u; q. push (v) ;}} color [u] = black ;}} void mat_graph: print_path (int s, int v) {BFS (s ); if (V = s) cout <S <''; else {If (pie [v] = 0) cout <"no path from" <S <"to" <v <"exists. "<Endl; else {print_path (S, pie [v]); cout <v <'';}}}

2. Main. cpp

 
# Include <iostream> # include "link_graph.h" using namespace STD; int main () {int I = 0, A, B, n, R; CIN> N> r; link_graph * g = new link_graph (8); for (I = 1; I <= r; I ++) {CIN> A> B; // undirected graph G-> adddoubleedge (a, B);} G-> bfs_init (); bool ret = true; for (I = 1; I <= N; I ++) {If (G-> V [I]. color = White & G-> V [I]. type = undefined) ret = G-> BFS (I); If (ret = false) {cout <"error" <Endl; return 0 ;}} for (I = 0; I <= N; I ++) {Switch (G-> V [I]. type) {case good: cout <I <"is a good player. "<Endl; break; case bad: cout <I <" is a bad player. "<Endl; break;} Delete g; return 0 ;}

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.