北邮OJ-272. 网络的核-14计院上机(第二套)C

Floyd模板题。

题目

//输入顶点从1始,内部运行顶点从1始 
//其实顶点从0编号也可以,改一改就好了 
#include 
#include 
#define MAXSIZE 100 
#define INF 0x7fffffff
using namespace std;

int main(){
    int t,n,m;//vexes' num,edge's num,sourse vex,destination vex
    int thisNode,nextNode;
    int newDist;
    int dist[MAXSIZE+1][MAXSIZE+1];
    int totalDist[MAXSIZE+1];
    cin>>t;
    while (t--){
        cin>>n>>m;
        //initiate
        for (int i=1;i<=n;i++){
            for (int j=1;j<=n;j++){
                if (i==j)
                    dist[i][j]=0;
                else
                    dist[i][j]=INF;
            }
            totalDist[i]=0;
        }
        //input
        for (int i=0;icin>>thisNode>>nextNode;
            dist[thisNode][nextNode]=dist[nextNode][thisNode]=1;//适用于无向图 
        }
        //floyd
        for (int k=1;k<=n;k++){
            for (int i=1;i<=n;i++){
                for (int j=1;j<=n;j++){
                    if (i==k||j==k)
                        continue;//第k趟邻接矩阵十字区不改变 
                    if (dist[i][k]==INF||dist[k][j]==INF)//经过k点不可达 
                        newDist=INF;
                    else
                        newDist=dist[i][k]+dist[k][j];//经过k点可达 
                    if (newDist//比较新旧距离 
                        dist[i][j]=newDist;
                }
            }
        }
        //cal
        for (int i=1;i<=n;i++){
            for (int j=1;j<=n;j++){
                if (dist[i][j]==INF)
                    totalDist[i]+=n;
                else
                    totalDist[i]+=dist[i][j];
            }
        }
        int minVex=1;
        for (int i=1;i<=n;i++){
            if (totalDist[i]//output
        cout<return true;
}

你可能感兴趣的:(北邮OJ-14计院2,Floyd,图,最短路径)