hdu 3631 Shortest Path(Floyd)[通俗易懂]

hdu 3631 Shortest Path(Floyd)

大家好,又见面了,我是全栈君。

题目链接: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


代码例如以下:

#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;
			else
				dis[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");
				else
					printf("%d\n",dis[x][y]);
			}
		}
	}
	return 0;
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115664.html原文链接:https://javaforall.net

(0)
上一篇 2022年2月1日 上午11:00
下一篇 2022年2月1日 上午11:00


相关推荐

  • linux替换大文件内容,Linux批量替换文件内容

    linux替换大文件内容,Linux批量替换文件内容今天测试人员一不小心把导航的地址改错了,大约6000多个导航文件,要通过后台配置的话也很麻烦,可以通过linux命令实现对批量文件进行内容替换,但是技术经理不在,我对linux命令不熟,没办法只好硬着头皮来。经在网上一番辛苦搜索,找到以下几个命令,并尝试执行……最终终于实现效果,哎,“书到用时方恨少”,特此针对今天的情况总结了一下Linux批量替换文件内容的命令,第一种:格式:sed-i”s/…

    2022年7月26日
    13
  • Nacos(二):SpringCloud项目中接入Nacos作为注册中心

    Nacos(二):SpringCloud项目中接入Nacos作为注册中心前言通过上一篇文章 Nacos 介绍简单了解了 Nacos 的发展历程和现状 本文我们开始 Nacos 试水的第一步 使用 Nacos 做注册中心上周末 7 6 Nacos 发布了 V1 1 0 版本 这次更新支持灰度配置 地址服务器模式 配置文件导入导出等其他功能 感觉社区的老哥们都很高产呐 本文主要通过两个项目来完成演示 nacos provide 服务提供者 nacos consumer 服务

    2026年3月16日
    2
  • 2—MATLAB将十进制转换成二进制补码

    2—MATLAB将十进制转换成二进制补码MATLAB 中提供了一个将十进制转换为二进制的函数 dec2bin 但是该函数只能接受大于 0 的数 也就是不能直接将负数转换为二进制补码 那如何在 MATLAB 中生成补码呢 我们都知道负数的补码为其反码加 1 然而 MATLAB 中的二进制是字符型 是不能直接运算的 所以如果用这种方式生成补码的话会比较困难 可能取反码还相对容易 反码加 1 可能就不是那么容易了 事实上我们有更好的办法 首先得了解补码的原理

    2026年3月26日
    1
  • Linux开放指定端口命令

    Linux开放指定端口命令1.方式一1、开启防火墙systemctlstartfirewalld2、开放指定端口(比如1935端口)firewall-cmd–zone=public–add-port=1935/tcp–permanent命令含义:–zone#作用域–add-port=1935/tcp#添加端口,格式为:端口/通讯协议–permanent#永久生效,没有此参数重启后失效3、重启防火墙firewall-cmd–reload4、查看..

    2025年9月27日
    6
  • 用Visio画软件(模块)功能图

    用Visio画软件(模块)功能图最开始自己也不会画 画了好久也没画出自己心中所想的 比如下图这样的 在形状框中搜索 方块 其中的图形有 框 和 多树枝直角 这两个组件从左边的 框 拖动到右边的幕布上 自己调整大小 双击编辑文字 选中可以在上方功能栏选择 设计 改变框的颜色和样式下滑 方块 栏 找到 多树枝直角 并拖动到右侧白色的圈圈可以调整它的位置方向和高度 调整后如下 黄色的圈圈可以链接子功能框 两个就代表可以链接两个子功能框 若两个不够 可以从中间的黄色圈圈再拖出来一个 拖动黄色圈圈也可以

    2026年3月19日
    1
  • 理查德•弗曼学习法思维导图-程序猿学习法

    理查德•弗曼学习法思维导图-程序猿学习法理查德•弗曼学习法思维导图-程序猿学习法

    2022年6月12日
    32

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号