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. Ubuntu系统应用程序创建快捷方式的方法

    大家安装了最新版的Ubuntu 14.0系统之后可能觉得很不习惯,因为Ubuntu的桌面干干净净没有任何快捷方式,任务栏的图标拖不下来,右键点击程序图标也没有创建快捷方式的菜单选项: 那如何把自己经常 ...

  2. Android经验之谈1

    豌豆荚等可能会导致手机网络出现问题,电话打不进来,所以需要及时卸载豌豆荚. 系统apk,不能通过在manifest.xml里面添加origin-package或者useid等来改变. 而是需要用and ...

  3. MySQL之经典语句

    数据库的创建:(例如创建名为ConstructionDB的数据库) --创建SelfStudy数据库 CREATE DATABASE ConstructionDB ON PRIMARY --创建主数据 ...

  4. ecshop调用文章显示上一篇下一篇

    首先调用文章中的上一篇和下一篇语法为:  代码如下 复制代码 上一篇:<a href="{$next_article.url}">{$next_article.titl ...

  5. Cocos2d-x优化中多线程并发访问

    多线程并发访问在Cocos2d-x引擎中用的不是很多,这主要是因为中整个结构设计没有采用多线程.源自于Objective-C的Ref对象,需要使用AutoreleasePool进行内存管理,Autor ...

  6. C++转到C#历程零基础知识(持续增加)

    1.命名空间.类和源文件的关系:几个源文件用同一个命名空间时候,那么这几个源文件中的各个类之间的调用时可行的.他不会根据你的源文件分离而分开,因为最终编译后生成的是dll,这里来看你的几个源文件是没有 ...

  7. 20141016--for 兔子

    Console.Write("请输入月数:"); int m =int.Parse(Console.ReadLine()); ;//成兔对数ct ;//小兔对数xt ;//幼兔对数 ...

  8. Android开发虚拟机的各种分辨率

  9. 【leetcode】363. Max Sum of Rectangle No Larger Than K

    题目描述: Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the ma ...

  10. spring中得到servletContext对象方法

    1.spring得到servletContext,这个和session没有什么关系,上下文可以说是一个session容器,一个上下文可以有多个会话session 在web.xml中有以下配置后.加入s ...