In the year 2163, wormholes were discovered. A wormhole is a subspace tunnel through space and time connecting two star systems. Wormholes have a few peculiar properties:
• Wormholes are one-way only.
• The time it takes to travel through a wormhole is negligible.
• A wormhole has two end points, each situated in a star system.
• A star system may have more than one wormhole end point within its boundaries.
• For some unknown reason, starting from our solar system, it is always possible to end up in any star system by following a sequence of wormholes (maybe Earth is the centre of the universe).
• Between any pair of star systems, there is at most one wormhole in either direction.
• There are no wormholes with both end points in the same star system.
    All wormholes have a constant time difference between their end points. For example, a specific wormhole may cause the person travelling through it to end up 15 years in the future. Another wormhole may cause the person to end up 42 years in the past.
    A brilliant physicist, living on earth, wants to use wormholes to study the Big Bang. Since warp drive has not been invented yet, it is not possible for her to travel from one star system to another one directly. This can be done using wormholes, of course.
    The scientist wants to reach a cycle of wormholes somewhere in the universe that causes her to end up in the past. By travelling along this cycle a lot of times, the scientist is able to go back as far in time as necessary to reach the beginning of the universe and see the Big Bang with her own eyes. Write a program to find out whether such a cycle exists.
Input
The input file starts with a line containing the number of cases c to be analysed. Each case starts with a line with two numbers n and m. These indicate the number of star systems (1 ≤ n ≤ 1000) and the number of wormholes (0 ≤ m ≤ 2000). The star systems are numbered from 0 (our solar system) through n − 1 . For each wormhole a line containing three integer numbers x, y and t is given. These numbers indicate that this wormhole allows someone to travel from the star system numbered x to the star system numbered y, thereby ending up t (−1000 ≤ t ≤ 1000) years in the future.
Output
The output consists of c lines, one line for each case, containing the word ‘possible’ if it is indeed possible to go back in time indefinitely, or ‘not possible’ if this is not possible with the given set of star systems and wormholes.
Sample Input
2
3 3
0 1 1000
1 2 15
2 1 -42
4 4
0 1 10
1 2 20
2 3 30
3 0 -60
Sample Output
possible
not possible

问题链接:UVA558 LA5579 Wormholes
问题简述:(略)
问题分析:用Floyd算法解决,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA558 LA5579 Wormholes */#include <bits/stdc++.h>using namespace std;/* Floyd-Warshall算法:计算图中任意2点之间的最短距离* 复杂度:O(N×N×N)* 输入:n 全局变量,图结点数*      g 全局变量,邻接矩阵,g[i][j]表示结点i到j间的边距离* 输出:g 全局变量*/
const int INF = 0x3f3f3f3f;
const int N = 1000;
int g[N + 1][N + 1], n;void floyd()
{for(int k = 0; k < n; k++)for(int i = 0; i < n; i++)if(g[i][k] < INF)for(int j = 0; j < n; j++)if(g[k][j] < INF)g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
}int main()
{int t, m, x, y, time;scanf("%d", &t);while(t--) {scanf("%d%d", &n, &m);memset(g, INF, sizeof(g));for(int i = 0; i < m; i++) {scanf("%d%d%d", &x, &y, &time);g[x][y] = time;}floyd();int flag = 1;for(int i = 0; i < n && flag; i++)for(int j = 0; j < n; j++)if(g[i][j] < INF && g[j][i] < INF) {if(g[i][j] + g[j][i] < 0) {flag = 0;break;}}puts(flag ? "not possible" : "possible");}return 0;
}

UVA558 LA5579 Wormholes【Floyd算法】相关推荐

  1. 【POJ/算法】 3259 Wormholes(Bellman-Ford算法, SPFA ,FLoyd算法)

    Bellman-Ford算法 Bellman-Ford算法的优点是可以发现负圈,缺点是时间复杂度比Dijkstra算法高.而SPFA算法是使用队列优化的Bellman-Ford版本,其在时间复杂度和编 ...

  2. 数据结构与算法(7-4)最短路径(迪杰斯特拉(Dijkstra)算法、弗洛伊德(Floyd)算法)

    目录 一.最短路径概念 二.迪杰斯特拉(Dijkstra)算法(单源最短路径) 1.原理 2.过程 3.代码 三.弗洛伊德(Floyd)算法(多源最短路径) 1.原理 2.存储 3.遍历 4.代码 参 ...

  3. 最小环算法求解(Dijkstra算法+Floyd算法)

    方法一: #include<iostream> #include<algorithm> #include<cmath> #include<cstdio> ...

  4. HDU2544(Bellman-ford算法和Floyd算法)

    思路: 1.初始化时将起点 s 到各个顶点 v 的距离 dist(s->v) 赋值为 ∞,dist(s->s) 赋值为 0: 2.后续进⾏最多 n-1 次遍历操作 (n 为顶点个数), 对 ...

  5. 【图论专题】Floyd算法及其扩展应用

    Floyd的拓展应用: 任意两点最短路 传递闭包 找最小环 恰好经过k条边的最短路(倍增) 题目列表: 题目 算法 AcWing 1125. 牛的旅行 任意两点最短路Floyd AcWing 343. ...

  6. 【图论】用一道题从本质上讲清楚Floyd算法

    P1119 [灾后重建] 4 5 1 2 3 4 0 2 1 2 3 1 3 1 2 2 1 4 0 3 5 4 2 0 2 0 1 2 0 1 3 0 1 4 -1 -1 5 4 一道非常好的Flo ...

  7. 图的单源最短路径,Floyd算法(数据结构c++)

    这个算法结构很是简单,但是理解还是有一定的困难,一开始做的时候想不明白,跟着算法自己动手画画就知道这个算法具体是怎么回事了. 时间复杂度是O(N*3) 算法有点动态规划的意思,有两个数组,一个(dis ...

  8. floyd算法_最短路径的算法:Floyd算法

    点击箭头处"蓝色字",关注我们哦!! 算法 最短路径的算法-Floyd算法 ● ○ ● Shortest Path Algorithm - Floyd Algorithm ● ○ ...

  9. 最短路径—Dijkstra算法和Floyd算法

    Dijkstra算法 1.定义概览 Dijkstra(迪杰斯特拉)算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止.Di ...

最新文章

  1. #!(sha-bang)--脚本的开始
  2. debug LUW3 - callback workflow - ABAP工作流的调试(第三部分)
  3. 迅雷前CEO陈磊涉嫌职务侵占罪已被立案侦查,股价周四下跌超7%
  4. 【iOS开发】理解 IBOutlet 和 IBAction
  5. 动画效果之 Canvas学习-globalCompositeOperation详解
  6. 软考:软件设计师(历年真题汇总)|希赛网
  7. RS232标准DB9接口定义
  8. spss因子分析结果解读_AMOS进行问卷分析中效度分析之验证因子分析教程 ——【杏花开生物医药统计】...
  9. [示例代码]植物大战僵尸网页版
  10. PHP + Apache + Mysql集成环境部署及简要教程
  11. java-php-python-ssm智能健身房管理计算机毕业设计
  12. java导出pdf 含图片_【Java】itext根据模板生成pdf(包括图片和表格)
  13. 七夕,眼光长远点,聊点孩子的事?
  14. 声音和音频设备属性无法设置
  15. 风险管理中的风险预测、风险评估、风险控制
  16. 木兰天池全新景观2013闪亮登场
  17. SpringBoot配置websocket
  18. jquery遍历后台数据
  19. 【python】——爬虫05 初级反爬笔记
  20. Java高手速成 | 高质量代码编写最佳实践

热门文章

  1. matplotlib 中文_Python 关于matplotlib无法显示中文字体的解决方法
  2. python读写excel表格_Python读写Excel表格(简单实用)
  3. 2020-02-16 Git客户端下载
  4. go tcp连接_在Go中构建并发TCP服务器样例
  5. ANE 在 Android 上的应用
  6. Android 中关于Cursor类的介绍
  7. php路径详解,详解与PHP路径相关的dirname,realpath,__FILE__函数
  8. 数据库身份证号用什么类型_互联网企业数据库安全的现状与对策
  9. php黑名单绕过,利用最新Apache解析漏洞(CVE-2017-15715)绕过上传黑名单
  10. Ros无法自动补全命令的解决