PAT甲级 - 1003 Emergency (25 分) Dijstra和BF算法

40 篇文章 0 订阅
35 篇文章 0 订阅
本文详细解析了Dijkstra与Bellman-Ford两种经典的最短路径算法。通过实例,介绍了如何在图中寻找两点间的最短路径及其可能的最大救援队伍数量,对比了两种算法的不同之处,包括数据结构的使用、算法流程以及统计最短路径条数的方法。
摘要由CSDN通过智能技术生成

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 N integers, 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

Dijstra图算法的应用,磕磕绊绊的,不过算是自己写完的。

要素

  1. 定义INF,vis[] ,G[][],dis[]
  2. 进入算法,首先处理头结点,然后循环n次{先找到dis最小的节点u,然后以u为中继更新其他未访问节点}
  3. 最短路径的条数:进行累加计数;能找到的队伍数,也是累加计数
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;

const int maxn = 510;
const int INF = 1E9;
int G[maxn][maxn] = { 0 };
int TeamNum[maxn] = { 0 };
int TeamCnt[maxn] = { 0 };
int dis[maxn];
int pathNum[maxn];
bool vis[maxn] = { false };
int N, M, C1, C2;


void Dijstra(int u) {
	dis[u] = 0;
	TeamCnt[u] = TeamNum[u];
	for (int i = 0; i < N; i++) {
		int MIN = INF, u = -1;
		for (int j = 0; j < N; j++) {
			if (vis[j] == false && dis[j] < MIN) { //找到一个未访问的距离最小点作为起点
				MIN = dis[j];
				u = j;
			}
		}
		if (u == -1) return; //if u not update, indicate that not route
		for (int v = 0; v < N; v++) {// update path
			vis[u] = true;
			if (vis[v] == false && G[u][v] != 0) {
				if (dis[u] + G[u][v] < dis[v])
				{
					dis[v] = dis[u] + G[u][v];
					pathNum[v] = pathNum[u];
					TeamCnt[v] = TeamCnt[u] + TeamNum[v];
				}
				else if (dis[u] + G[u][v] == dis[v])
				{
					pathNum[v] += pathNum[u];
					if (TeamCnt[u] + TeamNum[v] > TeamCnt[v])
						TeamCnt[v] = TeamCnt[u] + TeamNum[v];
				}

			}

		}
	}
}

int main() {
	cin >> N >> M >> C1 >> C2;
	fill(dis, dis + maxn, INF);
	fill(pathNum, pathNum + maxn, 1);
	for (int i = 0; i < N; i++)
		cin >> TeamNum[i];
	for (int i = 0; i < M; i++)
	{
		int c1, c2, L;
		cin >> c1 >> c2 >> L;
		G[c1][c2] = L;
		G[c2][c1] = L;
	}
	Dijstra(C1);
	cout << pathNum[C2] << " " << TeamCnt[C2] << endl;
}

Bellman-Ford算法

与Dijstra算法有很大不同,N-1轮,遍历所有边(会重复访问节点)

统计最短路径条数时,由于重复访问节点,要设置前驱set<int> pre[maxn],当相同时,要重新统计前驱和。

num[]数组初始化与Dij相同

Adj邻接表存储,Adj[u][j],j不代表节点地址,只代表Adj中的地址,通过Adj[u][j].v来获取地址

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;

struct node {
	int v, weight; //代表所连接的点和权值
	node(int _v, int _weight) : v(_v), weight(_weight) {}
};
const int maxn = 510;
const int INF = 1e9;
int dis[maxn];
int w[maxn] = { 0 };
int wCnt[maxn] = { 0 };
int num[maxn] = { 0 };	
int N, M, C1, C2;
vector<node> Adj[maxn];
set<int> pre[maxn];

bool BellmanFord(int source) {
	fill(dis, dis + maxn, INF);
	wCnt[source] = w[source]; //same
	dis[source] = 0; //same
	num[source] = 1;
	for (int i = 0; i < N - 1; i++) {
		for (int u = 0; u < M; u++) {
			for (int j = 0; j < Adj[u].size(); j++)// v is index of type node, not the real node
			{
				int v = Adj[u][j].v;
				int weight = Adj[u][j].weight;
				if (dis[u] + weight < dis[v]) {
					dis[v] = dis[u] + weight;
					wCnt[v] = wCnt[u] + w[v];
					num[v] = num[u];
					pre[v].clear();
					pre[v].insert(u);
				}
				else if (dis[u] + weight == dis[v]) {
					//num[v] += num[u]; 由于重复计数,不能这样使用
					if (wCnt[v] < wCnt[u] + w[v]) {
						wCnt[v] = wCnt[u] + w[v];
					}
					num[v] = 0;
					pre[v].insert(u);
					set<int>::iterator it;
					for (it = pre[v].begin(); it != pre[v].end(); it++) {
						num[v] += num[*it];
					}
				}
			}
		}
	}
	for (int i = 0; i < N - 1; i++) {
		for (int u = 0; u < M; u++) {
			for (int j = 0; j < Adj[u].size(); j++) {
				if (dis[u] + Adj[u][j].weight < dis[Adj[u][j].v])
					return false;
			}
		}
	}
	return true;
}

int main() {
	cin >> N >> M >> C1 >> C2;
	for (int i = 0; i < N; i++)
		cin >> w[i];
	for (int i = 0; i < M; i++)
	{
		int c1, c2, L;
		cin >> c1 >> c2 >> L;
		Adj[c1].push_back(node(c2, L));
		Adj[c2].push_back(node(c1, L));
	}
	if (BellmanFord(C1)) {
		cout << num[C2] << " " << wCnt[C2];
	} // output the number of shortest path
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值