1003 Emergency (25分)邻接表

题目

                                               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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
邻接表和逆邻接表是一种常用的图的存储结构,用于表示有向图或无向图的边关系。下是求邻接表和逆邻接表的方法: 1. 邻接表邻接表是一种链式存储结构,用于表示图的边关系。对于有向图,邻接表中的每个顶点都有一个链表,链表中存储了该顶点指向的其他顶点。具体求邻接表的步骤如下: - 创建一个数组,数组的大小为图中顶点的个数。 - 遍历图中的每个顶点,对于每个顶点,创建一个链表,链表中存储该顶点指向的其他顶点。 - 遍历图中的每条边,将边的起始顶点添加到对应顶点的链表中。 2. 逆邻接表: 逆邻接表邻接表的一种变形,用于表示有向图中每个顶点的入边关系。具体求逆邻接表的步骤如下: - 创建一个数组,数组的大小为图中顶点的个数。 - 遍历图中的每个顶点,对于每个顶点,创建一个链表,链表中存储指向该顶点的其他顶点。 - 遍历图中的每条边,将边的终点添加到对应顶点的链表中。 下面是一个示例,展示如何求解邻接表和逆邻接表: ```python # 邻接表 adj_list = [[] for _ in range(num_vertices)] # 创建一个空的邻接表 for edge in edges: start, end = edge # 边的起始顶点和终点 adj_list[start].append(end) # 将终点添加到起始顶点的链表中 # 逆邻接表 reverse_adj_list = [[] for _ in range(num_vertices)] # 创建一个空的逆邻接表 for edge in edges: start, end = edge # 边的起始顶点和终点 reverse_adj_list[end].append(start) # 将起始顶点添加到终点的链表中 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值