(PAT 1003) Emergency (dijkstra算法)

As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.

Input Specification:

Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (≤500) – the number of cities (and the cities are numbered from 0 to N−1), M – the number of roads, C​1​​ and C​2​​ – the cities that you are currently in and that you must save, respectively. The next line contains Nintegers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c​1​​, c​2​​ and L, which are the pair of cities connected by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C​1​​ to C​2​​.

Output Specification:

For each test case, print in one line two numbers: the number of different shortest paths between C​1​​ and C​2​​, and the maximum amount of rescue teams you can possibly gather. All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.

Sample Input:

5 6 0 2
1 2 1 5 3
0 1 1
0 2 2
0 3 1
1 2 1
2 4 1
3 4 1

Sample Output:

2 4

解题思路:

学习迪杰斯特拉算法后试做的第一题,不是特别熟悉

我在编写算法时没有对起点位置的权值和距离进行初始化,所以一直出错

一定要记住起点的权值要初始化

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int MAXV = 510;
const int INF = 0x3fffffff;
int n, m, c1, c2;
int GMAP[MAXV][MAXV];
int d[MAXV];
int AWeight[MAXV];
bool Visited[MAXV] = { false };
int numRoutes[MAXV] = { 0 };
int w[MAXV] = { 0 };

void MDijstra(int s) {
	fill(d, d + MAXV, INF);
	d[s] = 0;
	numRoutes[s] = 1;
	w[s] = AWeight[s];
	// 切记,起点一定要初始化!!!,一旦开始循环,起点的权值是不考虑的
	for (int i = 0; i < n; ++i) {
		//寻找当前的最小点
		int u = -1, MMIN = INF;
		for (int j = 0; j < n; ++j) {
			if (!Visited[j] && d[j] < MMIN) {  //为被访问且被开放的结点
				MMIN = d[j];
				u = j;
			}
		}
		if (u == -1) return; //结束
		Visited[u] = true;
		//更新开放结点的最短距离
		for (int v = 0; v < n; ++v) {
			if (!Visited[v] && GMAP[u][v] != INF) {  //边是通的情况下
				if (GMAP[u][v] + d[u] < d[v]) {
					d[v] = GMAP[u][v] + d[u];
					numRoutes[v] = numRoutes[u];
					w[v] = w[u] + AWeight[v];
				}
				else if (GMAP[u][v] + d[u] == d[v]) {
					numRoutes[v] += numRoutes[u];   //计算最短路
					if (w[u] + AWeight[v] > w[v]) {
						w[v] = w[u] + AWeight[v];
					}
				}
			}
		}
	}
}

int main() {
	fill(GMAP[0], GMAP[0] + MAXV * MAXV, INF);
	scanf("%d %d %d %d", &n, &m, &c1, &c2);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &AWeight[i]);
	}
	for (int i = 0; i < m; ++i) {
		int node1, node2, nlength;
		scanf("%d %d %d", &node1, &node2, &nlength);
		GMAP[node1][node2] = nlength;
		GMAP[node2][node1] = nlength;
	}
	MDijstra(c1);
	printf("%d %d", numRoutes[c2], w[c2]);

	system("PAUSE");
	return 0;
}

 

    原文作者:Dijkstra算法
    原文地址: https://blog.csdn.net/alex1997222/article/details/86938162
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞