UVA numbering Paths
Description
Download as PDF
Background
Problems that process input and generate a simpleyes‘‘ or
No "answer is called decision problems. One class of decision problems, the np-complete problems, is not amenable to general efficient solutions. Other problems may is simple as decision problems and enumerating all possibleYes ' answers may very difficult (or at least time-consuming).
This problem involves determining the number of routes available to an emergency vehicle operating in a city of one-way St Reets.
The problem
Given the intersections connected by one-way streets in a city, you is to write a program that determines the number of D Ifferent routes between each intersection. A route is a sequence of one-way streets connecting and intersections.
Intersections is identified by non-negative integers. A one-way street is specified by a pair of intersections. For example, Tex2html_wrap_inline30 indicates, there are a one-way street from intersection J to intersection K. Note T Hat two-way streets can be modeled by specifying, one-way Streets:tex2html_wrap_inline30 and tex2html_wrap_inline38.
Consider a city of four intersections connected by the following one-way streets:
0 1
0 2
1 2
2 3
There is one route from intersection 0 to 1, and both routes from 0 to 2 (the routes is tex2html_wrap_inline40 and TEX2HTML_WR Ap_inline42), routes from 0 to 3, one route from 1 to 2, one route from 1 to 3, one route from 2 to 3, and no other R Outes.
It is possible for a infinite number of different routes to exist. For example if the intersections above was augmented by the street tex2html_wrap_inline44, there was still only one route from 0 to 1, but there is infinitely many different routes from 0 to 2. This was because the street from 2 to 3 and back to 2 can be repeated yielding a different sequence of streets and hence a Different route. Thus The route tex2html_wrap_inline46 is a different route than tex2html_wrap_inline48.
The Input
The input is a sequence of city specifications. Each specification begins with the number of one-way streets in the city followed by that many one-way streets given as PA IRS of intersections. Each pair tex2html_wrap_inline30 represents a one-way street from intersection J to intersection K. In all cities, intersections is numbered sequentially from 0 to the
Largest "intersection. All integers in the input is separated by whitespace. The input is terminated by End-of-file.
There'll never is a one-way street from the intersection to itself. No City would have more than intersections.
The Output
For each city specification, a square matrix of the number of different routes from intersection J to intersection K is PR Inted. If The matrix is denoted M, then m[j][k] are the number of different routes from intersection J to intersection K. The matrix M should is printed in row-major order, one row per line. Each matrix should is preceded by the string "Matrix for Cityk" (with K-appropriately instantiated, beginning with 0).
If There is an infinite number of different paths between, intersections A-1 should be printed. Worry about justifying and aligning the output of each matrix. All entries in a row should is separated by whitespace.
Sample Input
7 0 1 0 2 0 4 2 4 2 3 3 1 4 3
5
1 S
0 1 1 5 2 5 2 1
9
0 1 0 2 0 3
0 4 1 4 2 1
2 0
3 0
3 1
Sample Output
Matrix for City 0
0 4 1) 3 2
0 0 0) 0 0
0 2 0) 2 1
0 1 0) 0 0
0 1 0) 1 0
Matrix for City 1
0 2 1 0 0 3
0 0 0 0 0 1
0 1 0 0 0 2
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
Matrix for City 2
-1-1-1-1-1
0 0 0) 0 1
-1-1-1-1-1
-1-1-1-1-1
0 0 0) 0 0
Topic: Give a picture, ask you to find out how many paths each point to other points, and output as a matrix. Problem solving idea: Warshall algorithm.
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>using namespace STD;Const intN = -;typedef Long LongllintN, G[n][n], Max;voidInput () {memsetG0,sizeof(G));intA, B; Max =0; for(inti =0; I < n; i++) {scanf("%d%d", &a, &b);if(A > Max) Max = A;if(b > Max) Max = b; G[A][B] =1; }}voidWarshall () { for(intK =0; K <= Max; k++) { for(inti =0; I <= Max; i++) { for(intj =0; J <= Max; J + +) {G[i][j] + = g[i][k] * G[k][j]; } } } for(intK =0; K <= Max; k++) {if(G[k][k]) {//With self-loop for(inti =0; I <= Max; i++) { for(intj =0; J <= Max; J + +) {if(G[i][k] & G[k][j]) G[I][J] =-1; } } } }}intMain () {intCase =0; while(scanf("%d", &n)! = EOF) {printf("Matrix for City%d\n", case++); Input (); Warshall (); for(inti =0; I <= Max; i++) { for(intj =0; J <= Max; J + +) {if(J! =0)printf(" ");printf("%d", G[i][j]); }puts(""); } }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission can also be reproduced, but to indicate the source oh.
UVA numbering Paths (Warshall algorithm)