uva 125 Numbering Paths

原题:
Background
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).
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 the largest'' 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 city k” (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 NOTworry 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

中文:
给你一个有向图,然后让你输出一个矩阵,表示出从一个点到另外一个点的路径有多少条,如果有无数条,那么这两个点之间用-1表示。

#include<bits/stdc++.h>
using namespace std;
int n,ind;
int mat[31][31];

int main()
{
    ios::sync_with_stdio(false);
 //   fstream in,out;
    int t=0;
//    in.open("in.txt");
//    out.open("out.txt");
    while(cin>>n)
    {
        memset(mat,0,sizeof(mat));
        ind=0;
        for(int i=0;i<n;i++)
        {
            int a,b;
            cin>>a>>b;
            mat[a][b]=1;
            ind=max(ind,max(a,b));

        }
        for(int k=0;k<=ind;k++)
        {
            for(int i=0;i<=ind;i++)
            {
                for(int j=0;j<=ind;j++)
                {
                    mat[i][j]+=mat[i][k]*mat[k][j];
                }
            }
        }
        for(int k=0;k<=ind;k++)
        {
            if(mat[k][k]!=0)
            for(int i=0;i<=ind;i++)
            {
                for(int j=0;j<=ind;j++)
                {
                    if(mat[i][k]!=0&&mat[k][j]!=0)
                        mat[i][j]=-1;
                }
            }
        }
        cout<<"matrix for city "<<t++<<endl;
        for(int i=0;i<=ind;i++)
        {
            for(int j=0;j<=ind;j++)
            {
                cout<<mat[i][j];
                if(j==ind)
                    cout<<endl;
                else
                    cout<<" ";
            }
        }
    }
 //   in.close();
 //   out.close();
    return 0;
}

思路:

第一眼看到这题就立刻就想起Matrix 67的一篇文章,就是讲矩阵的那部分。
原文链接http://www.matrix67.com/blog/archives/276
所以很快就把代码写出来,结果一直wa,上网上搜了搜其他人的答案,思路也是大同小异,只不过是用floyed算法改进而来。

基本上的思路是这样的,mat[i][j]表示从i到j的路径数,那么mat[i][j]=sigma mat[i][jk]*mat[k][j]
这里k只中间节点

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
GT编码方案是一个全球统一的电话编码方案,为各个国家和地区提供了一种统一的组织和管理电话号码的方法。GT编号方案是由国际电信联盟(ITU)制定的,旨在简化全球通信系统的管理和运营。 GT编号方案基于国家和地区的电话国家号(国际直拨号码)以及各个电话运营商的识别码。它的主要目的是为了实现全球范围内的电话呼叫和短信传送的互通性,确保电话网络的有效和高效运作。 GT编码方案的核心是国家/地区代码,它是一个固定长度的数字序列,用来标识特定国家或地区的电话网络。每个国家/地区都有一个唯一的代码,通过这个代码,电话系统可以准确地识别呼叫的目的地。 在GT编码方案中,还有一个关键元素是国内/本地代码,它是由电话运营商分配给用户的一个编号,用来标识特定用户或用户组织。这个编号通常是一个可变长度的数字序列,根据各个国家和地区的不同规定而定。 GT编码方案的实施有助于简化国内和国际电话号码的管理,提高通信设备与网络之间的互操作性。它为用户提供了更便捷、高效的电话通信服务,也促进了全球通信业务的发展。 总之,GT编码方案是一个全球统一的电话编码方案,通过统一的国家/地区代码和本地代码,为电话网络的组织和管理提供了标准化的方法,实现了全球范围内的电话通信互通。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值