dijkstra求助
查看原帖
dijkstra求助
727008
lwx20211103楼主2022/11/17 16:58

样例没过

#include <bits/stdc++.h>
using namespace std;

int times[114514], photo[1001][1001]; //邻接矩阵存图 
bool mark[114514];
int dis[114514];//最短路
int n, m;

int dijkstra()
{
	memset(dis, 0x7fffffff, sizeof(dis));
	dis[1] = 0;
	int i, j;
	for (i = 0;i < n - 1;i++)
	{
		int temp = -1;
		for (j = 1;j <= n;j++)
		{
			if (!mark[j] && (temp == -1 || dis[temp] > dis[j]))
			{
				temp = j;
			}
		}
		for (j = 1;j <= n;j++)
			dis[j] = min(dis[j], dis[temp] + photo[temp][j]);
		mark[temp] = true;
	}
	return dis[n];
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	
	cin >> n >> m;
	int i, j;
	for (i = 1;i <= n;i++)
	{
		cin >> times[i];
	}
	times[n] = 0;
	memset(photo, 0x7fffffff, sizeof(photo));
	for (i = 1;i <= m;i++)
	{
		int u, v, cost;
		cin >> u >> v >> cost;
		photo[u][v] = times[v] + cost;
		photo[v][u] = times[u] + cost; //无向边 
	}
	cout << dijkstra();
	return 0;
}


2022/11/17 16:58
加载中...