当前位置: 代码迷 >> 综合 >> HDU - 1874 (2020.6.11训练G题)
  详细解决方案

HDU - 1874 (2020.6.11训练G题)

热度:68   发布时间:2023-12-07 00:08:57.0

题意:有N个点,M个双向边且有输入距离,求起点到终点的最短距离

单源最短路径算法题
AC代码

#include<iostream>
using namespace std;
typedef long long ll;
const ll maxn = 0xfffffff;
int N, M;
int map[205][205];
int a, b, x;
int f, l;
int main()
{
    while (scanf("%d%d", &N, &M)!=EOF){
    //初始化for (int i = 0; i < N; i++){
    for (int j = 0; j < N; j++){
    map[i][j] = i == j ? 0 : maxn;}}//读入数据for (int i = 0; i < M; i++){
    cin >> a >> b >> x;if(x<map[a][b])map[a][b] = map[b][a] = x;}for (int i = 0; i < N; i++)//中转点{
    for (int j = 0; j < N; j++){
    for (int k = 0; k < N; k++){
    if (map[j][k] > map[j][i] + map[i][k]){
    map[j][k] = map[j][i] + map[i][k];}}}}cin >> f >> l;if (map[f][l] == maxn)cout << "-1" << endl;elsecout << map[f][l] << endl;}
}