1003 Emergency (25分)邻接表

本文介绍了一种基于邻接表实现的最短路径算法,用于解决应急救援中寻找从当前城市到目标城市的不同最短路径数量及最多能集结的救援队伍数量的问题。输入包括城市数量、道路数量、各城市救援队伍数量及城市间的道路长度,输出为最短路径数量和最大救援队伍集结数。

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

题目

                                               1003 Emergency (25分)

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​2and 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
​1to C​2
​​ .
Output Specification:
For each test case, print in one line two numbers: the number of different shortest paths between C
​1and 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
题目地址
邻接表实现

#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>

using namespace std;

typedef struct _AdjListNode {
	int End;
	int Weight;
	_AdjListNode *next;
}AdjListNode;

typedef struct _AdjList {
	int Val;
	_AdjListNode *Head;
}AdjList;

typedef struct _Graph
{
	int Vertices;
	int Edge;
	_AdjList *Array;
}Graph;

Graph* CreateGraph(int v)
{
	Graph* g = new Graph;
	g->Vertices = v;
	g->Edge = 0;
	g->Array = new AdjList[v];
	for (int i = 0; i<v; i++)
	{
		g->Array[i].Head = NULL;
	}
	return g;
}

void AddEdge(Graph *g, int bNode, int eNode, int weight)
{
	AdjListNode *nNode = new AdjListNode;
	nNode->End = eNode;
	nNode->Weight = weight;
	nNode->next = g->Array[bNode].Head;
	g->Array[bNode].Head = nNode;

	nNode = new AdjListNode;
	nNode->End = bNode;
	nNode->Weight = weight;
	nNode->next = g->Array[eNode].Head;
	g->Array[eNode].Head = nNode;
}

bool visited[501];
int dist[501];
int num[501];
int val[501];
const int INF = 99999999;

void Dijkstra(Graph *g, int curPos)
{
	dist[curPos] = 0;
	val[curPos] = g->Array[curPos].Val;
	num[curPos] = 1;
	for(int j=0;j<g->Vertices;j++)
	{
		int visit = -1;
		int minV = INF;
		for (int i = 0; i < g->Vertices; i++)
		{
			if (visited[i] == false && dist[i] < minV)
			{
				visit = i;
				minV = dist[i];
			}
		}
		if (visit == -1)
			break;
		else
			visited[visit] = true;
		AdjListNode *p = new AdjListNode;
		p = g->Array[visit].Head;
		while (p)
		{
			if (visited[p->End] == false)
			{
				if (dist[visit] + p->Weight<dist[p->End])
				{
					dist[p->End] = dist[visit] + p->Weight;
					num[p->End] = num[visit];
					val[p->End] = val[visit] + g->Array[p->End].Val;
				}
				else if (dist[visit] + p->Weight == dist[p->End])
				{
					num[p->End] += num[visit];
					if (val[visit] + g->Array[p->End].Val>val[p->End])
					{
						val[p->End] = val[visit] + g->Array[p->End].Val;
					}
				}
			}
			p = p->next;
		}
	}
}


int main()
{
	int n, e, curPos, tarPos;
	cin >> n >> e >> curPos >> tarPos;
	Graph *g = CreateGraph(n);
	g->Edge = e;
	for (int i = 0; i<n; i++)
	{
		int num;
		cin >> num;
		g->Array[i].Val = num;
	}

	for (int i = 0; i<e; i++)
	{
		int c1, c2, c;
		cin >> c1 >> c2 >> c;
		AddEdge(g, c1, c2, c);
	}
	fill(dist, dist + 501, INF);
	fill(visited, visited + 501, false);
	Dijkstra(g, curPos);
	cout << num[tarPos] << " " << val[tarPos];	
	return 0;
}

/*
5 7 0 2
1 3 1 5 3
0 1 1
0 2 2
0 3 1
1 2 1
2 3 1
2 4 1
3 4 1

2 1 0 1
1 1
0 1 3

1 0 0 0
1

6 7 0 5
1 2 1 1 2 1
0 1 1
0 2 2
1 2 1
2 3 1
2 4 1
3 5 1
4 5 1
*/

https://blog.csdn.net/cv_jason/article/details/80891055
https://blog.csdn.net/u010731824/article/details/48293669

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值