[SSL] 1760 store location problem

[SSL] 1760 store location problem

Time Limit:1000MS
Memory Limit:65536K

Description

Given a map of a city (represented by an adjacency matrix), the store is located at a point so that the sum of the distances from each place to the store is the shortest.

Input

The first
row is n (there are several cities); N is less than 201, and the second row to the n+ 1th row is a city map (represented by an adjacency matrix);

Output

The sum of the shortest paths;

Sample Input

3
0 3 1
3 0 2
1 2 0

Sample Output

3

Ideas

Use floyd.
0 means unreachable

Code

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<cmath>
#include<queue>
#include<cstring>
using namespace std;
int n;
int a[2010][2010];
bool d[2010];
void input()
{
    
    
	int i,j,x,y,z;
	scanf("%d",&n);
	for(i=1;i<=n;i++)
		for(j=1;j<=n;j++)
		{
    
    
			scanf("%d",&a[i][j]);
			if(i!=j&&a[i][j]==0)
				a[i][j]=100000000;
		}
	return;
}
void floyd()//求多源最短路 
{
    
    
	int i,j,k;
	for(k=1;k<=n;k++)
		for(i=1;i<=n;i++)
			for(j=1;j<=n;j++)
				if(a[i][k]+a[k][j]<a[i][j])
					a[i][j]=a[i][k]+a[k][j];
	return;
}
int main()
{
    
    
	int i,j,sum,ans=100000000;
	input();
	floyd();
	for(i=1;i<=n;i++)
	{
    
    
		for(sum=0,j=1;j<=n;j++)//计算距离和 
			sum+=a[i][j];
		ans=min(ans,sum);
	}
	printf("%d",ans);
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_46975572/article/details/112318361