SSLOJ1760 商店选址问题

Description

给出一个城市的地图(用邻接矩阵表示),商店设在一点,使各个地方到商店距离之和最短。

Input

第一行为n(共有几个城市); N小于201
第二行至第n+1行为城市地图(用邻接矩阵表示);

Output

最短路径之和;

Sample Input

3
0 3 1
3 0 2
1 2 0

Sample Output

3

思路

注意,0,代表到不了!!!
剩下的floyd计算最短路,然后枚举求最小
code:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
int n,m,q;
int f[201][201];
int main()
{
    
    
 cin>>q;
 for (int i=1;i<=q;i++) for (int j=1;j<=q;j++)
 {
    
    
  cin>>f[i][j];
  if (i!=j&&f[i][j]==0) f[i][j]=0x7f7f7f7f;
 }
 for (int k=1;k<=q;k++)
 {
    
    
  for (int i=1;i<=q;i++)
  {
    
    
   for (int j=1;j<=q;j++)
   {
    
    
     f[i][j]=min(f[i][j],f[i][k]+f[k][j]);
   }
  }
 }
 int mn=0x7f7f7f7f;
 for (int i=1;i<=q;i++)
 {
    
    
  int s=0;
  for (int j=1;j<=q;j++) s+=f[i][j];
  mn=min(s,mn);
 }
 cout<<mn;
 return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_49843717/article/details/112139465