PTA 紧急救援 (最短路径,迪杰斯特拉算法)

该博客介绍了如何使用迪杰斯特拉算法来解决PTA平台上的紧急救援问题,强调了不能使用弗洛伊德算法的原因,并提供了通过DFS查找路径的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

L2-001 紧急救援 (25 分)

分析:

题目要求算错路径,显然弗洛伊德算法就不可以用了。
用迪杰斯特拉算法记录路径来源,用DFS来查找路径并存入vector容器。

代码:

#include <bits/stdc++.h>
using namespace std;

int n, m, st, en, city[510], road[510][510], fast[510], ans1, ans2;
bool visit[510];
vector <int> sour[510];//每一个城市的最近路线来源
vector <int> ans3;
vector <int> temp;//在DFS中的暂时最佳路径

void init()//对数据进行初始化
{
   
   
	for (int i = 0; i<510; i++){
   
   
		for (int j = 0; j<510; j++
以下是PTA最短路径迪杰斯特拉算法程序的填空代码: ```python #include <stdio.h> #include <stdlib.h> #define MaxVertexNum 501 #define INFINITY 65535 typedef int Vertex; typedef int WeightType; typedef char DataType; typedef struct GNode *PtrToGNode; struct GNode{ int Nv; int Ne; WeightType G[MaxVertexNum][MaxVertexNum]; DataType Data[MaxVertexNum]; }; typedef PtrToGNode MGraph; typedef struct ENode *PtrToENode; struct ENode{ Vertex V1, V2; WeightType Weight; }; typedef PtrToENode Edge; MGraph CreateGraph( int VertexNum ) { Vertex V, W; MGraph Graph; Graph = (MGraph)malloc(sizeof(struct GNode)); Graph->Nv = VertexNum; Graph->Ne = 0; for (V=0; V<Graph->Nv; V++) for (W=0; W<Graph->Nv; W++) Graph->G[V][W] = INFINITY; return Graph; } void InsertEdge( MGraph Graph, Edge E ) { Graph->G[E->V1][E->V2] = E->Weight; Graph->G[E->V2][E->V1] = E->Weight; } MGraph BuildGraph() { MGraph Graph; Edge E; int Nv, i; scanf("%d", &Nv); Graph = CreateGraph(Nv); scanf("%d", &(Graph->Ne)); if ( Graph->Ne != 0 ) { E = (Edge)malloc(sizeof(struct ENode)); for (i=0; i<Graph->Ne; i++) { scanf("%d %d %d", &E->V1, &E->V2, &E->Weight); E->V1--; E->V2--; InsertEdge( Graph, E ); } } return Graph; } void PrintDist( WeightType dist[], int N ) { int i; for ( i=0; i<N; i++ ) printf("%d ", dist[i]); } void PrintPath( int path[], int V ) { if ( path[V] != -1 ) { PrintPath( path, path[V] ); printf(" %d", V); } else printf("%d", V); } void Dijkstra( MGraph Graph, WeightType dist[], int path[], Vertex S ) { int collected[MaxVertexNum]; Vertex V, W, MinV; WeightType MinDist; for ( V=0; V<Graph->Nv; V++ ) { dist[V] = Graph->G[S][V]; if ( dist[V]<INFINITY ) path[V] = S; else path[V] = -1; collected[V] = 0; } dist[S] = 0; collected[S] = 1; while (1) { MinDist = INFINITY; MinV = -1; for ( V=0; V<Graph->Nv; V++ ) { if ( collected[V]==0 && dist[V]<MinDist ) { MinDist = dist[V]; MinV = V; } } if (MinV==-1) break; collected[MinV] = 1; for( W=0; W<Graph->Nv; W++ ) { if ( collected[W]==0 && Graph->G[MinV][W]<INFINITY ) { if ( Graph->G[MinV][W]<0 ) { printf("Error: Negative Dist!\n"); return; } if ( dist[MinV]+Graph->G[MinV][W] < dist[W] ) { dist[W] = dist[MinV]+Graph->G[MinV][W]; path[W] = MinV; } } } } } int main() { int i; WeightType dist[MaxVertexNum]; int path[MaxVertexNum]; MGraph G = BuildGraph(); Vertex S = 0; Dijkstra( G, dist, path, S ); for ( i=1; i<G->Nv; i++ ) { printf("%d\n", dist[i]); } return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值