uva 125 Numbering Paths(warshall算法)

uva 125 Numbering Paths

Description

Download 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算法。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;

const int N = 50;
typedef long long ll;
int n, G[N][N], 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;
}

版权声明:本文为博主原创文章,未经博主允许也可以转载,不过要注明出处哦。

时间: 2024-08-03 03:50:15

uva 125 Numbering Paths(warshall算法)的相关文章

Numbering Paths (Uva 125 floyd+dp思想)

Numbering Paths Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Background Problems that process input and generate a simple ``yes'' or ``no'' answer are called decision problems. One class of decis

poj 3660 Cow Contest(warshall算法)

poj 3660 Cow Contest Description N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming contest. As we all know, some cows code better than others. Each cow has a certain constant skill rating that is unique among the co

uva oj 567 - Risk(Floyd算法)

1 /* 2 一张有20个顶点的图上. 3 依次输入每个点与哪些点直接相连. 4 并且多次询问两点间,最短需要经过几条路才能从一点到达另一点. 5 6 bfs 水过 7 */ 8 #include<iostream> 9 #include<cstring> 10 #include<vector> 11 #include<cstdio> 12 #include<queue> 13 using namespace std; 14 15 struct

hdu 1625 Numbering Paths 最短路的变形,使用Floyd 外加判环

Numbering Paths Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 158    Accepted Submission(s): 47 Problem Description Problems that process input and generate a simple ``yes'' or ``no'' answer

UVa 988 - Many Paths, One Destination

题目:人生有很多选择,现在给你一些选择(0~n-1),和每个选择分支后面的其他选择序号,求选择总数. 分析:dp,图论.如果某状态的后续选择个数是0个则,代表死亡,统计所有到达死亡的路径条数即可. 用一个状态数组记录到达每个选择的路径数,它等于能到达它的前驱节点的路径加和. 稀疏图,使用邻接表储存.初始是节点0的路径条数为1,代表出生. 说明:没有给数据范围略坑啊,RE一次,WA一次,╮(╯▽╰)╭. #include <iostream> #include <cstdlib> #

Warshall算法

---用Warshall算法解决传递闭包问题 ---在一个关系R中,如果任意的(a,b)和(b,c)在关系R中,则(a,c)也一定在关系R中,则称R是可传递的.一个关系R的传递闭包是包 含R的最小的传递关系. ---Warshall算法不用来计算最短路径,而是用来判断i和j之间是否单向连接,无论是直接连接还是间接连接 ---初始条件不再是邻接矩阵,而是关系矩阵,如果两个点之间直接单向连接,那么在矩阵中对应的值就为1,反之为0 ---根据已有的直接连接关系得出其它所有的间接连接关系,即判断两点之间

Warshall算法求传递闭包

传递闭包 在数学中,在集合 X 上的二元关系 R 的传递闭包是包含 R 的 X 上的最小的传递关系. 例如,如果 X 是(生或死)人的集合而 R 是关系“为父子”,则 R 的传递闭包是关系“x 是 y 的祖先”.再比如,如果 X 是空港的集合而关系 xRy 为“从空港 x 到空港 y 有直航”,则 R 的传递闭包是“可能经一次或多次航行从 x 飞到 y”. Warshall算法 Warshall在1962年提出了一个求关系的传递闭包的有效算法.其具体过程如下,设在n个元素的有限集上关系R的关系矩

求传递闭包的warshall算法

———————————————————————————— Question:R是定义于集合S上的二元关系,求R的传递闭包. Input:relation R,set A Output:t(R),which is the transitive closure of R  Solution:Warshall algorithm ———————————————————————————— 传递闭包(transitive closure) R* = R1 ∪ R2 ∪ R3 ∪ ...... ∪ Rn  

UVA 10564 十 Paths through the Hourglass

Paths through the Hourglass Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 10564 1 #include <stdio.h> 2 #include <string.h> 3 4 int n,s; 5 long long dp[55][56][550]; 6 int a[56][55]; 7 8 i