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
#include
#include
#include

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

你可能感兴趣的:(PTA)