Numbering Paths |
Problems that process input and generate a simple ``yes'' or ``no'' answer are called decision problems. One class of decision problems, the NP-complete problems, are not amenable to general efficient solutions. Other problems may be simple as decision problems, but enumerating all possible ``yes'' answers may be very difficult (or at least time-consuming).html
This problem involves determining the number of routes available to an emergency vehicle operating in a city of one-way streets.ios
Given the intersections connected by one-way streets in a city, you are to write a program that determines the number of different routes between each intersection. A route is a sequence of one-way streets connecting two intersections.app
Intersections are identified by non-negative integers. A one-way street is specified by a pair of intersections. For example, indicates that there is a one-way street from intersection j to intersectionk. Note that two-way streets can be modeled by specifying two one-way streets: and .ide
Consider a city of four intersections connected by the following one-way streets:spa
0 1 0 2 1 2 2 3
There is one route from intersection 0 to 1, two routes from 0 to 2 (the routes are and ), two 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 routes.code
It is possible for an infinite number of different routes to exist. For example if the intersections above are augmented by the street , there is still only one route from 0 to 1, but there are infinitely many different routes from 0 to 2. This is 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 is a different route than .htm
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 pairs of intersections. Each pair represents a one-way street from intersection j to intersection k. In all cities, intersections are numbered sequentially from 0 to the ``largest'' intersection. All integers in the input are separated by whitespace. The input is terminated by end-of-file.blog
There will never be a one-way street from an intersection to itself. No city will have more than 30 intersections.ci
For each city specification, a square matrix of the number of different routes from intersection j to intersection k is printed. If the matrix is denoted M, then M[j][k] is the number of different routes from intersection j to intersection k. The matrix M should be printed in row-major order, one row per line. Each matrix should be preceded by the string ``matrix for city
k'' (with k appropriately instantiated, beginning with 0).input
If there are an infinite number of different paths between two intersections a -1 should be printed. DO NOTworry about justifying and aligning the output of each matrix. All entries in a row should be separated by whitespace.
7 0 1 0 2 0 4 2 4 2 3 3 1 4 3 5 0 2 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
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
給一個有向圖,求圖中任意兩點之間存在多少種不一樣的路徑。
這裏採用了Floyd思想,設dp[i][j]表示從結點i到結點j的路徑數,有:
dp[i][j]= ∑dp[i][k]*dp[k][j]
題目要求對於兩點間有無數條路徑的輸出-1,即若是圖中出現了環,那麼環中的任意兩點之間的路徑數要輸出-1,所以在Floyd執行完以後要進行一輪判斷,判斷的方法就是,對於任意的dp[x][x]!=0(即存在一個環能夠從點x出發並再次回到x),咱們找出知足dp[i][x]!=0&&dp[x][j]!=0的有序對(i,j),那麼這對點(i,j)之間的路徑是無窮多的,將dp[i][j]改成-1便可。
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cstring> 5 6 using namespace std; 7 8 int E; 9 long long G[50][50]; 10 11 int main() 12 { 13 int kase = -1; 14 15 while (scanf("%d", &E) == 1) 16 { 17 kase++; 18 19 memset(G, 0, sizeof(G)); 20 21 int a, b, n = -1; 22 for (int i = 1; i <= E; i++) 23 { 24 scanf("%d %d", &a, &b); 25 G[a][b] += 1; 26 if (max(a, b) > n) 27 n = max(a, b); 28 } 29 30 for (int k = 0; k <= n; k++) 31 for (int i = 0; i <= n; i++) 32 for (int j = 0; j <= n; j++) 33 G[i][j] += G[i][k] * G[k][j]; 34 35 for(int k=0;k<=n;k++) 36 if (G[k][k]) 37 { 38 for (int i = 0; i <= n; i++) 39 for (int j = 0; j <= n; j++) 40 if (G[i][k] && G[k][j]) 41 G[i][j] = -1; 42 } 43 44 printf("matrix for city %d\n", kase); 45 for (int i = 0; i <= n; i++) 46 { 47 printf("%lld", G[i][0]); 48 for (int j = 1; j <= n; j++) 49 printf(" %lld", G[i][j]); 50 puts(""); 51 } 52 } 53 54 return 0; 55 }