Query with algorithm for a [closed] problem

3

Good treatment to do the next problem .

Here's my solution :

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define MAX 10005
#define Node pair< int , int >
#define INF 1<<30

struct cmp {
    bool operator() ( const Node &a , const Node &b ) {
        return a.second > b.second;
    }
};

vector< Node > ady[ MAX ]; vector<int> distancia[ MAX ];
bool visitado[ MAX ];
priority_queue< Node , vector<Node> , cmp > Q;
int V;

void init(){
    for( int i = 0 ; i <= V ; ++i ){
        for (int j = 0; j <= V; ++j){
            distancia[i].push_back(INF);
        }
        visitado[ i ] = false;
    }
}

void relajacion( int actual , int adyacente , int peso ,int inicial){
    if( distancia[ inicial ][actual] + peso < distancia[ inicial][adyacente ] ){
        distancia[ inicial][adyacente ] = distancia[ inicial ][actual] + peso;
        Q.push( Node( adyacente , distancia[inicial][ adyacente ] ) );
    }
}

void dijkstra( int inicial ){
    init();
    Q.push( Node( inicial , 0 ) );
    distancia[ inicial ][inicial]=0;
    int actual , adyacente , peso;

    while( !Q.empty() ){
        actual = Q.top().first;
        Q.pop();
        if( visitado[ actual ] ) continue;
        visitado[ actual ] = true;

        for( int i = 0 ; i < ady[ actual ].size() ; ++i ){
            adyacente = ady[ actual ][ i ].first;
            peso = ady[ actual ][ i ].second;
            if( !visitado[ adyacente ] ){
                relajacion( actual , adyacente , peso ,inicial);
            }
        }
    }
}

int main(){
    int origen, destino , peso , inicial;
    int f;

    while(cin>>f>>V){
        vector <int> fire(f);
        for(int i=0;i<f;i++){
            scanf("%d",&fire[i]);

        }

        for (int i = 0; i < V; ++i){
            scanf("%d %d %d" , &origen , &destino , &peso );
            ady[ origen ].push_back( Node( destino , peso ) );
            ady[ destino ].push_back( Node( origen, peso ) );
        }

        for (int i = 1; i <= V; ++i){
            dijkstra(i);
        }

        int consulta=0;
        int cont2=INF;

        for (int i = 1; i <= V; ++i){
            int cont=0;
            int nuevo=INF;
            fire.push_back(i);

            for (int k = 1; k <= V; ++k){
                nuevo=INF;
                vector<int>::iterator it=find(fire.begin(),fire.end(),k);
                if(it!=fire.end())continue;

                for (int j = 0; j < fire.size(); ++j){
                    if(k!=fire[j]){
                        if(nuevo>distancia[k][fire[j]]){
                            nuevo=distancia[k][fire[j]];
                        }
                    }
                }

                if(nuevo==INF){
                    nuevo=0;
                }

                cont+=nuevo;
            }

            if(cont2>cont){
                cont2=cont;
                consulta=i;
            }

            fire.pop_back();
        }

        printf("%d",consulta);
        return 0;
    }
}

Which is inefficient because of the complexity O (f2 * i), and the memory it occupies, however, I think it's not all bad, but in the last part, if you could give me a help, beware I know that the solution is in a web, I just want a clue, that I've had a good time in this problem, or another approach, because I think that using some voracious algorithm could come out.

    
asked by Kevin AB 15.06.2016 в 21:20
source

1 answer

0

The problem they want to solve is NOT TSP (which in Spanish is usually called the problem of the traveling salesman, not the traveling agent, I do not know what they will find if they google the traveling agent). I do not know what they call total solution, I guess they mean polynomial solution, and yes, indeed TSP does not have known polynomial solutions but it is NOT KNOWN whether or not it has polynomial solutions because it is not known if P (the set of problems with polynomial solutions ) is or not equal to NP (another set of problems, where is for example TSP).

That said, Dijkstra is an algorithm that solves the minimum path problem, a problem that IS known to come out in polynomial time.

The thing of 10 ^ 6 is a lie, in a second there are approximately 10 ^ 8 operations, but it is not measured in operations, it is measured in time, it is what it takes your program to execute, it runs in your computer with large cases and You will be able to see how long it takes.

The problem that formally asks is given an unguided weighted graph (the city) and some distinguished vertices (those of the stations) finding the lowest vertex (in index) such that adding a station to that vertex minimizes the maximum distance between u and v for all u vertex of the graph and v vertex with station.

This IS a minimum path problem and comes out, for example, with the Dijkstra algorithm. It also comes with the Floyd-Warshall algorithm which is much easier to implement.

If any of these words (graph, algorithm, computer, vertex, index, distance, Dijkstra, Floyd-Warshall, etc.) were not understood, I think they are all in Wikipedia.

    
answered by 09.09.2016 в 17:14