floyd 算法   如果存在无数条路  则存在a->a的路  a->b的路径数等于 a->i 和 i->b(0=<i<=_max) 路径数的乘积和

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 30+5;
int n, g[maxn][maxn],ca;
int main()
{
ca = 0;
while (scanf("%d", &n) == 1)
{
int _max = 0;
memset(g, 0, sizeof(g));
for (int i = 0; i < n; i++)
{
int x, y;
scanf("%d%d", &x, &y);
g[x][y] = 1;
_max = max(_max, max(x, y));
}
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;
printf("matrix for city %d\n", ca++);
for (int i = 0; i <= _max; i++)
{
for (int j = 0; j <= _max; j++)
{
if (j) putchar(' ');
printf("%d", g[i][j]);
}
puts("");
}
}
return 0;
}

uva 125的更多相关文章

  1. UVA 125 Numbering Paths

    题目大意:给定n条单向边,求图中任意两点的连通路径的数目.其中点是从0-输入中出现的最大的点. 可以用floyd-warshall算法或者dfs. for(int k = 0; k < n; k ...

  2. UVA 125 统计路径条数 FLOYD

    这道题目折腾了我一个下午,本来我的初步打算是用SPFA(),进行搜索,枚举出发点,看看能到达某个点多少次,就是出发点到该点的路径数,如果出现环,则置为-1,关键在于这个判环过程,如果简单只找到某个点是 ...

  3. Root :: AOAPC I: Beginning Algorithm Contests (Rujia Liu) Volume 7. Graph Algorithms and Implementation Techniques

    uva 10803 计算从任何一个点到图中的另一个点经历的途中必须每隔10千米 都必须有一个点然后就这样 floy 及解决了 ************************************* ...

  4. UVa 107 - The Cat in the Hat (找规律,注意精度)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  5. UVa 100 - The 3n + 1 problem(函数循环长度)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  6. Uva 12563,劲歌金曲,01背包

    题目链接:https://uva.onlinejudge.org/external/125/12563.pdf 题意:n首歌,每首歌的长度给出,还剩 t 秒钟,由于KTV不会在一首歌没有唱完的情况下切 ...

  7. 【转】UVa Problem 100 The 3n+1 problem (3n+1 问题)——(离线计算)

    // The 3n+1 problem (3n+1 问题) // PC/UVa IDs: 110101/100, Popularity: A, Success rate: low Level: 1 / ...

  8. 【set&&sstream||floyed判环算法】【UVa 11549】Calculator Conundrum

    CALCULATOR CONUNDRUM Alice got a hold of an old calculator that can display n digits. She was bored ...

  9. UVa 10012 - How Big Is It? 堆球问题 全排列+坐标模拟 数据

    题意:给出几个圆的半径,贴着底下排放在一个长方形里面,求出如何摆放能使长方形底下长度最短. 由于球的个数不会超过8, 所以用全排列一个一个计算底下的长度,然后记录最短就行了. 全排列用next_per ...

随机推荐

  1. 面试之SQL(2)--left join, inner join 和 right join的区别

    表A记录如下: aID        aName 1           a1 2           a2 3           a3 4           a4 5           a5 ...

  2. 分析Android程序之破解第一个程序

    破解Android程序通常的方法是将apk文件利用ApkTool反编译,生成Smali格式的反汇编代码,然后阅读Smali文件的代码来理解程序的运行机制,找到程序的突破口进行修改,最后使用ApkToo ...

  3. centos6.5下磁盘分区及挂载

    1..查看磁盘空间 2.磁盘分区 3.格式化分区 4.挂载/卸载

  4. [Bootstrap]全局样式(一)

    页面必须设置为html5文档类型 <!DOCTYPE html> <html lang="zh-CN"> ... </html> 适应移动设备 ...

  5. js 书写规范

    1.字符串用单引号 2.运算符号和变量之间用空格建立间距 3.书写插件时使用 'use strict'; 开头 4.方法如果是获取或者设置数据集合则使用动词如  getData,setData,eac ...

  6. Spring事务配置的五种方式(转发)

    Spring事务配置的五种方式(原博客地址是http://www.blogjava.net/robbie/archive/2009/04/05/264003.html)挺好的,收藏转发 前段时间对Sp ...

  7. java访问webservice服务(一)

    欢迎转载  http://www.cnblogs.com/shizhongtao/p/3433653.html 使用wsdl2java工具命令   一. 调出命令提示符cd到cxf的解压路径“D:\学 ...

  8. Mysql 数据库安装配置

    MySQL的多种安装方法 在当今的互联网企业,Mysql数据服务几乎都是运行在LINUX系统操作系统上,当然你也可以在WINDOWS.UNIX等商业操作系统上运行. 但是一般企业都会采用LNMP.LA ...

  9. Java 学习计划

    第一部分 在搭建SSM的过程中,可能会经常接触到一个叫maven的工具.这个工具也是你以后工作当中几乎是必须要使用的工具,所以你在搭建SSM的过程中,也可以顺便了解一下maven的知识.在你目前这个阶 ...

  10. JSON Helper

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Se ...