PTA Double Eleven

General idea of ​​the topic
During Double Eleven, a well-known e-commerce platform "Dongdong" prepared to respond to the peak sales in nnAdd another self-operated warehouse in n cities, the requirement is that the warehouse is located innnOne of the n cities, and the sum of the shortest distances from all other cities is the smallest. Please write a program to help "Dongdong" find out where to set up a warehouse. Supposennn cities are numbered from 0 tonnn -1, there is at least one city and all other cities between them.
Input format The
input contains multiple sets of data. The first line of each group of data is two positive integersnnn andeee , no more than 100. nnn represents the number of cities. NexteeRow e represents the distance information between two cities, each row is 3 non-negative integersa, b, ca, b, ca , b , c , whereaaa andbbb represents two city codes,ccc represents the distance between cities.
Tip:EOF EOFcan be usedE O F judges the end of input.
Output format The
output is an integer, which represents the city number where the warehouse is built. If multiple cities meet the requirements, the smallest number will be output.
Input sample

6 5
0 1 1
0 2 1
0 3 1
0 4 1
0 5 1
4 5
0 1 1
0 2 5
1 2 2
1 3 4
2 3 1

Sample output

0
1

F l o y e d Floyed F l o y e d find the shortest path from multiple sources, first find the path from each point to all points, and finally find the sum, the first point to the last point is violently enumerated to find the minimum value, and then output the point, Time complexityO (n 3) O(n^3)O ( n3)

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 107;
typedef long long ll;
int mp[107][107];
int main()
{
    
    
    int n,e;
    while(~scanf("%d %d",&n,&e))
    {
    
    
        memset(mp, 0x3f, sizeof mp);
        for(int i = 1; i <= e; i++)
        {
    
    
            int from, to, v;
            scanf("%d %d %d",&from,&to,&v);
            ++from, ++to;
            if(from == to) mp[from][to] = 0;
            else
            {
    
    
                mp[from][to] = v;
                mp[to][from] = v;
            }
        }
        
        for(int k = 1; k <= n; k++)
            for(int i = 1; i <= n; i++)
                for(int j = 1; j <= n; j++)
                    if(mp[i][j] > mp[i][k] + mp[k][j])
                        mp[i][j] = mp[i][k] + mp[k][j];
        
        ll dis[maxn] = {
    
    0};
        for(int i = 1; i <= n; i++)
        {
    
    
            for(int j = 1; j <= n; j++)
            {
    
    
                if(i != j) dis[i] = dis[i] + (ll)mp[i][j];
            }
        }
        ll ans = 0x3f3f3f3f;
        int pos = 1;
        for(int i = 1; i <= n; i++)
            if(dis[i] < ans) ans = dis[i], pos = i;
        --pos;
        printf("%d\n",pos);
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/Edviv/article/details/111715914