• hiho_1089_floyd最短路


    题目

        floyd算法求所有顶点之间的最短路,典型的模板题。唯一需要注意的是两个顶点之间可能有多条边直接相连,在初始化的时候,直接选择最小的长度作为两点间的距离即可。

    实现

    #include<iostream>
    #include<stdio.h>
    #include<algorithm>
    #include<unordered_map>
    #include<list>
    #include<string>
    #include<string.h>
    #include<set>
    using namespace std;
    int min_dist[105][105];
    
    int main(){
    	int n, m, u, v, d;
    	scanf("%d %d", &n, &m);
    	memset(min_dist, 0x0F, sizeof(min_dist));
    	for (int i = 1; i <= n; i++)
    		min_dist[i][i] = 0;
    	for (int i = 0; i < m; i++){
    		scanf("%d %d %d", &u, &v, &d);
    		if (min_dist[u][v] > d)  //可能存在两个点之间有多条直接相连的边,取最小的那一条即可
    			min_dist[u][v] = min_dist[v][u] = d;
    	}
    	//floyd算法求最短路
    	for (int k = 1; k <= n; k++){
    		for (int i = 1; i <= n; i++){
    			for (int j = 1; j <= n; j++){
    				if (min_dist[i][j] > min_dist[i][k] + min_dist[k][j])
    					min_dist[i][j] = min_dist[i][k] + min_dist[k][j];
    			}
    		}
    	}
    	for (int i = 1; i <= n; i++){
    		for (int j = 1; j <= n; j++)
    			printf("%d ", min_dist[i][j]);
    		printf("
    ");
    	}
    	return 0;
    }
    
  • 相关阅读:
    pat乙级1034
    getline()读入一整行
    c++ 输入split
    13.缓存、三级缓存、内存溢出、AsyncTask
    12、json、GridView、缓存
    11.webview、shareSDK
    10.下拉刷新、加载更多、标记已读、轮播条、缓存
    9.indicate、xutils、json
    8.滑动事件处理
    7.ViewPagerIndicator
  • 原文地址:https://www.cnblogs.com/gtarcoder/p/5558820.html
Copyright © 2020-2023  润新知