题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3631

Shortest Path

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3962    Accepted Submission(s): 938
Problem Description
When YY was a boy and LMY was a girl, they trained for NOI (National Olympiad in Informatics) in GD team. One day, GD team’s coach, Prof. GUO asked them to solve the following shortest-path problem.
There is a weighted directed multigraph G. And there are following two operations for the weighted directed multigraph:
(1) Mark a vertex in the graph.
(2) Find the shortest-path between two vertices only through marked vertices.
For it was the first time that LMY faced such a problem, she was very nervous. At this moment, YY decided to help LMY to analyze the shortest-path problem. With the help of YY, LMY solved the problem at once, admiring YY very much. Since then, when LMY meets problems, she always calls YY to analyze the problems for her. Of course, YY is very glad to help LMY. Finally, it is known to us all, YY and LMY become programming lovers.
Could you also solve the shortest-path problem?
Input
The input consists of multiple test cases. For each test case, the first line contains three integers N, M and Q, where N is the number of vertices in the given graph, N≤300; M is the number of arcs, M≤100000; and Q is the number of operations, Q ≤100000. All vertices are number as 0, 1, 2, … , N - 1, respectively. Initially all vertices are unmarked. Each of the next M lines describes an arc by three integers (x, y, c): initial vertex (x), terminal vertex (y), and the weight of the arc (c). (c > 0) Then each of the next Q lines describes an operation, where operation “0 x” represents that vertex x is marked, and operation “1 x y” finds the length of shortest-path between x and y only through marked vertices. There is a blank line between two consecutive test cases.
End of input is indicated by a line containing N = M = Q = 0.
Output
Start each test case with "Case #:" on a single line, where # is the case number starting from 1.
For operation “0 x”, if vertex x has been marked, output “ERROR! At point x”.
For operation “1 x y”, if vertex x or vertex y isn’t marked, output “ERROR! At path x to y”; if y isn’t reachable from x through marked vertices, output “No such path”; otherwise output the length of the shortest-path. The format is showed as sample output.
There is a blank line between two consecutive test cases.
Sample Input
5 10 10 1 2 6335 0 4 5725 3 3 6963 4 0 8146 1 2 9962 1 0 1943 2 1 2392 4 2 154 2 2 7422 1 3 9896 0 1 0 3 0 2 0 4 0 4 0 1 1 3 3 1 1 1 0 3 0 4 0 0 0
Sample Output
Case 1: ERROR! At point 4 ERROR! At point 1 0 0 ERROR! At point 3 ERROR! At point 4
Source
2010 Asia Regional Tianjin Site —— Online Contest 

代码例如以下:

#include <cstdio>
#include <cstring>
#define INF 0x3fffffff
#define MAXN 317
int dis[MAXN][MAXN];
int mark[MAXN];
int n;
int min(int a, int b)
{return a < b ? a:b;
}
void init()
{for(int i = 0; i < n; i++){for(int j = 0; j < n; j++){if(i == j)dis[i][j] = 0;elsedis[i][j] = INF;}}
}void Floyd(int k)
{for(int i = 0; i < n; i++){for(int j = 0; j < n; j++){dis[i][j] = min(dis[i][j],dis[i][k]+dis[k][j]);}}
}int main()
{int i, j;int M, Q;int a, b, w;int cas = 0;while(~scanf("%d%d%d",&n,&M,&Q)){if(n == 0 && M == 0 && Q == 0)break;if(cas != 0)printf("\n");init();memset(mark,0,sizeof(mark));for(i =  0; i < M; i++){scanf("%d%d%d",&a,&b,&w);if(w < dis[a][b]){dis[a][b] = w;}}int op, x, y;printf("Case %d:\n",++cas);for(i = 0; i < Q; i++){scanf("%d",&op);if(op == 0){scanf("%d",&x);if(mark[x]){printf("ERROR! At point %d\n",x);continue;}mark[x] = 1;Floyd(x);}else if(op == 1){scanf("%d%d",&x,&y);if(!mark[x] || !mark[y]){printf("ERROR! At path %d to %d\n",x,y);continue;}if(dis[x][y] >= INF)printf("No such path\n");elseprintf("%d\n",dis[x][y]);}}}return 0;
}

转载于:https://www.cnblogs.com/yxwkf/p/5276774.html

hdu 3631 Shortest Path(Floyd)相关推荐

  1. Shortest Path(翻译)

    http://noi.openjudge.cn/english/07/ There is a graph with N nodes. Given the length of each edge bet ...

  2. 弗洛伊德算法(Floyd)简介

    弗洛伊德算法(Floyd)简介 Floyd算法适用于解决求最短路径问题,相比于Digkstra算法思路更加简单,更容易理解,但是效率会明显低很多,可以作为初步学习的一种方法. 目录 弗洛伊德算法(Fl ...

  3. 七、最短路径——弗洛伊德(Floyd)算法

    为了能讲明白弗洛伊德(Floyd)算法的精妙所在,我们先来看最简单的案例.下图是一个最简单的3个顶点连通网图. 我们先定义两个二维数组D[3][3]和P[3][3],D代表顶点到顶点的最短路径权值和的 ...

  4. 【数据结构】图—弗洛伊德(Floyd)算法

    前言 上文介绍了迪杰斯特拉(Dijkstra)算法,计算网图的某个源点到其余各个顶点的最短路径问题(边权值为非负值),本文介绍另一个求最短路径的算法--弗洛伊德算法,它是计算所有顶点到所有顶点的最短路 ...

  5. 最短路径之弗洛伊德算法(Floyd)——动态规划

    弗洛伊德算法(Floyd)主要针对多源最短路径,且可以解决路径中有负权的情况(不包含负权回路),但是迪杰斯特拉算法只能解决正权值的单源最短路径(可以迭代多次求多源). 1.弗洛伊德算法的基本思想 弗洛 ...

  6. 算法系列——弗洛伊德算法(Floyd)

    本系列旨在用简单的人话讲解算法,尽可能避免晦涩的定义,读者可以短时间内理解算法原理及应用细节.我在努力! 本篇文章编程语言为Python,供参考. 弗洛伊德算法(Floyd) 典型最短路径算法.用于计 ...

  7. 大话数据结构-迪杰斯特拉算法(Dijkstra)和弗洛伊德算法(Floyd)

    6 最短路径   最短路径,对于图来说,是两顶点之间经过的边数最少的路径:对于网来说,是指两顶点之间经过的边上权值之和最小的路径.路径上第一个顶点为源点,最后一个顶点是终点. 6.1 迪杰斯特拉(Di ...

  8. 数据结构——图——弗洛伊德(Floyd)算法

    数据结构--图--弗洛伊德(Floyd)算法 为了能讲明白弗洛伊德(Flbyd)算法的精妙所在,我们先来看最简单的案例.图7-7-12的左图是一个最简单的3个顶点连通网图. 我们先定义两个二维数组D[ ...

  9. 【算法】弗洛伊德(Floyd)算法

    这个算法主要要弄懂三个循环的顺序关系. 弗洛伊德(Floyd)算法过程: 1.用D[v][w]记录每一对顶点的最短距离. 2.依次扫描每一个点,并以其为基点再遍历所有每一对顶点D[][]的值,看看是否 ...

最新文章

  1. CVPR2019论文看点:自学习Anchor原理
  2. 在ECS上使用Windows “跨区卷”、“条带卷”讨论以及扩容操作
  3. jqgrid的函数与操作
  4. Matlab | 数字信号处理:用FFT做谱分析
  5. Boost:post process后期处理的测试程序
  6. 拼图项目的动机和目标
  7. OpenCV中直方图均衡化
  8. 微信公众号开发之获取用户信息
  9. 关于神经网络的需要注意的概念总结
  10. linux下反汇编命令,Linux命令学习手册-objdump命令
  11. TimesTen 应用层数据库缓存学习:4. 仅仅读缓存
  12. PCA算法原理及实现
  13. 单机手机消消乐php游戏源码,JS叠房子消消乐小游戏代码
  14. 纽微特荒唐事:都知道是找人顶罪,竟没人敢指正
  15. 动态代理和静态代理的区别_动态代理与静态代理
  16. iOS AVPlayer的那些坑
  17. petalinux2020.2离线编译配置
  18. 2022聚合工艺复训题库及在线模拟考试
  19. 一篇文章让你搞懂如何通过Nginx来解决跨域问题
  20. 用Photoshop将照片卡通化

热门文章

  1. 蓝牙鼠标windows linux,Ubuntu下使用蓝牙无线鼠标[图]
  2. java服务器返回错误码,java - java.io.IOException:服务器返回URL的HTTP响应代码:409 - 堆栈内存溢出...
  3. 将angular转化为手机app_手机照片快速转化为PBR材质流程
  4. java字符串转json_java 字符串转成 json 数组并且遍历
  5. python为什么用class_python为什么会有@classmethod?
  6. android权限申请方法,安卓开发Android6+权限申请管理用户拒绝权限一键解决方案...
  7. android 开发传输安全,移动应用安全开发指南(Android)--数据传输
  8. curl php 禁用ip6,CentOS 6禁用IPv6解决curl Couldn’t resolve host或dns解析慢
  9. java 弹性碰撞_球体弹性碰撞位置和速度计算算法
  10. 为什么要使用计算机协议,为什么需要网络协议