最短路径(2066)

应用场景:给出存在的路径,求最短

核心代码:


```cpp
//near存起点
//hope存终点
		
		//初始化
		const int INF = 0x3f3f3f3f
		int m[max][max];
		memset(m, INF, sizeof(m));
		for (int i = 0; i < max; ++i)
			m[i][i] = 0;

		int ans = INF;
		for (k = 0; k <=n; ++k) {//每次增加一个中介点,看是否会更短
			for (i = 0; i <=n; ++i) {
				if (m[i][k] == INF)//如果起始地i与中介k无路径 可跳过
					continue;
				for (j = 0; j <=n; ++j) {
					m[i][j] = m[i][j] < m[i][k] + m[k][j] ? m[i][j] : m[i][k] + m[k][j];
					if (near[i] && hope[j] && m[i][j] < ans)//更新可能的最短路径
						ans = m[i][j];
				}
			}
		}
上一篇:蜜蜂路线


下一篇:【CF1473E】Minimum Path