PAT1003Emergency

PAT1003Emergency,第1张

PAT1003Emergency
#include
#include
#include
#include
#include
#include
#include
#include
#define INFINITY 9999999
using namespace std;
int N, M, C1, C2;
int rescue[500];
struct Node {
	int Location;
	int distance;
};
int MaxRescueNum[500];
vector>G(500);
int Distance[500];
int shortestPath[500];
bool visited[500];
int FindMinDistance();
void Dijkstra();
int main()
{
	scanf("%d %d %d %d",&N,&M,&C1,&C2);
	for (int i = 0; i < N; i++)
	{
		scanf("%d", rescue + i);
	}
	for (int i = 0; i < M; i++)
	{
		int node1, node2, distance;
		scanf("%d %d %d", &node1, &node2, &distance);
		G[node1].push_back({ node2,distance });
		G[node2].push_back({ node1,distance });
	}
	Dijkstra();



}
void Dijkstra()
{
	for (int i = 0; i < N; i++)
	{
		Distance[i] = INFINITY;
	}
	shortestPath[C1] = 1;
	Distance[C1] = 0;
	visited[C1] = 1;
	MaxRescueNum[C1] = rescue[C1];
	for (int i = 0; i < G[C1].size(); i++)
	{
		Distance[G[C1][i].Location] = G[C1][i].distance;
		shortestPath[G[C1][i].Location] = 1;
		MaxRescueNum[G[C1][i].Location] = MaxRescueNum[C1] + rescue[G[C1][i].Location];
	}
	while (1)
	{
		int Location = FindMinDistance();
		visited[Location] = true;
		if (visited[C2]==true)
		{
			break;
		}
		else
		{
			for (int i = 0; i < G[Location].size(); i++)
			{
				if (Distance[G[Location][i].Location] > Distance[Location] + G[Location][i].distance)
				{
					Distance[G[Location][i].Location] = Distance[Location] + G[Location][i].distance;
					MaxRescueNum[G[Location][i].Location] = MaxRescueNum[Location] + rescue[G[Location][i].Location];
					shortestPath[G[Location][i].Location] = shortestPath[Location];
				}
				else if (Distance[G[Location][i].Location] == Distance[Location] + G[Location][i].distance)//距离相等
				{
					shortestPath[G[Location][i].Location] += shortestPath[Location];
					MaxRescueNum[G[Location][i].Location] = max(MaxRescueNum[G[Location][i].Location], MaxRescueNum[Location]+rescue[G[Location][i].Location]);
				}
			}
		}
	}
	printf("%d %d", shortestPath[C2], MaxRescueNum[C2]);

}
int FindMinDistance()
{
	int MinDistance = INFINITY;
	int Location=-1;
	for (int i = 0; i < N; i++)
	{
		if (visited[i] == false)
		{
			if (MinDistance > Distance[i])
			{
				Location = i;
				MinDistance = Distance[i];
			}
		}
	}
	return Location;
}```

测试点2错误原因:找到等长的路应该是

```cpp
shortestPath[G[Location][i].Location] += shortestPath[Location];

而不是

shortestPath[G[Location][i].Location] += shortestPath[Location];

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5691014.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存