uva 125 Numbering Paths(warshall算法)
2015-08-01 10:06
344 查看
uva 125 Numbering Paths
DescriptionDownload as PDF
Background
Problems that process input and generate a simple
yes'' orno” 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). This problem involves determining the number of routes available to an emergency vehicle operating in a city of one-way streets. The Problem 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. Intersections are identified by non-negative integers. A one-way street is specified by a pair of intersections. For example, tex2html_wrap_inline30 indicates that there is a one-way street from intersection j to intersection k. Note that two-way streets can be modeled by specifying two 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, two routes from 0 to 2 (the routes are tex2html_wrap_inline40 and tex2html_wrap_inline42 ), 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. It is possible for an infinite number of different routes to exist. For example if the intersections above are augmented by the street tex2html_wrap_inline44 , 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 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 pairs of intersections. Each pair tex2html_wrap_inline30 represents a one-way street from intersection j to intersection k. In all cities, intersections are numbered sequentially from 0 to thelargest” intersection. All integers in the input are separated by whitespace. The input is terminated by end-of-file.
There will never be a one-way street from an intersection to itself. No city will have more than 30 intersections.
The Output
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 cityk” (with k appropriately instantiated, beginning with 0).
If there are an infinite number of different paths between two intersections a -1 should be printed. DO NOT worry about justifying and aligning the output of each matrix. All entries in a row should be separated by whitespace.
Sample Input
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
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
题目大意:给出一张图,要求你求出每个点到其他点有多少条路径,并以矩阵形式输出。
解题思路:warshall算法。
[code]#include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <cstdlib> using namespace std; const int N = 50; typedef long long ll; int n, G , Max; void input() { memset(G, 0, sizeof(G)); int a, b; Max = 0; for (int i = 0; i < n; i++) { scanf("%d %d", &a, &b); if (a > Max) Max = a; if (b > Max) Max = b; G[a][b] = 1; } } void warshall() { for (int k = 0; k <= Max; k++) { for (int i = 0; i <= Max; i++) { for (int j = 0; j <= Max; j++) { G[i][j] += G[i][k] * G[k][j]; } } } for (int k = 0; k <= Max; k++) { if (G[k][k]) {//有自环 for (int i = 0; i <= Max; i++) { for (int j = 0; j <= Max; j++) { if (G[i][k] & G[k][j]) G[i][j] = -1; } } } } } int main() { int Case = 0; while (scanf("%d", &n) != EOF) { printf("matrix for city %d\n", Case++); input(); warshall(); for (int i = 0; i <= Max; i++) { for (int j = 0; j <= Max; j++) { if (j != 0) printf(" "); printf("%d", G[i][j]); }puts(""); } } return 0; }
相关文章推荐
- Unity Colliders
- Redis持久化实践及灾难恢复模拟
- SQL基础学习_05_函数、谓词、CASE表达式
- 暑假集训-个人赛第六场
- 最全的libcurl库资源整理
- 使用NSURLConnection实现大文件断点下载
- 补充:关于不定长的链表的实现
- HDOJ Intersection 5120【环相交面积】
- 消息队列 redis vs nsq
- Servlet线程
- Git、Gradle初步
- VS2010错误“LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏”解决办法
- C# 自定义特性的使用
- 第十章 幸运抽奖
- sql语法:inner join on, left join on, right join on详细使用方法
- hdu2473 Junk-Mail Filter
- hihoCoder 1048 状态压缩·二
- 最强AngularJS资源合集
- EventBus 《四》 事件的优先级及其EventBus Demo
- 最强AngularJS资源合集