0

i was trying to pass this code in uri online judge but i don't know where is my error, all the tests i made work.

the link problem is this: https://www.urionlinejudge.com.br/repository/UOJ_1640_en.html

the description of the problem is:

A transport company often needs to deliver goods from one city to another city. The transport company has made a special deal with a hotel chain which allows its drivers to stay in the hotels of this chain for free. Drivers are only allowed to drive up to 10 hours a day. The transport company wants to find a route from the starting city to the destination city such that a driver can always spend the night in one of the hotels of the hotel chain, and that he needs to drive at most 10 hours from one hotel to the next hotel (or the destination). Of course, the number of days needed to deliver the goods should also be minimized. Input

The input file contains several test cases. Each test case starts with a line containing an integer n, (2 ≤ n ≤ 10000), the number of cities to be considered when planning the route. For simplicity, cities are numbered from 1 to n, where 1 is the starting city, and n is the destination city. The next line contains an integer h followed by the numbers c1, c2, ..., ch indicating the numbers of the cities where hotels of the hotel chain are located. You may assume that 0 ≤ h ≤ min(n, 100). The third line of each test case contains an integer m (1 ≤ m ≤ 105), the number of roads to be considered for planning the route. The following m lines describe the roads. Each road is described by a line containing 3 integers a, b, t (1 ≤ a, b ≤ n and 1 ≤ t ≤ 600), where a, b are the two cities connected by the road, and t is the time in minutes needed by the driver to drive from one end of the road to the other. Input is terminated by n = 0. Output

For each test case, print one line containing the minimum number of hotels the transport company has to book for a delivery from city 1 to city n. If it is impossible to find a route such that the driver has to drive at most 10 hours per day, print -1 instead.

and my try solution is on my github: https://github.com/h31nr1ch/uri/blob/master/1640.cpp

#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <limits> // for numeric_limits
#include <set>
#include <utility> // for pair
#include <algorithm>
#include <iterator>
using namespace std;

typedef long long int vertex_t;
typedef double weight_t;
const weight_t max_weight = numeric_limits<double>::infinity();

struct neighbor {
  vertex_t target;
  weight_t weight;
  weight_t current;
  bool hotel;
  neighbor(vertex_t arg_target, weight_t arg_weight,weight_t arg_current,bool arg_hotel):target(arg_target), weight(arg_weight),current(arg_current),hotel(arg_hotel){ }
};

typedef vector<vector<neighbor> > adjacency_list_t;

void DijkstraComputePaths(vertex_t source,adjacency_list_t &adjacency_list, vector<weight_t> &min_distance, vector<vertex_t> &previous,vector<vertex_t> &hoteis,vector<vertex_t> &weights,vector<bool> &tHotel){
  int n = adjacency_list.size();
  min_distance.clear();
  min_distance.resize(n, max_weight);
  min_distance[source] = 0;
  previous.clear();
  previous.resize(n, -1);
  weights.clear();
  weights.resize(n,0);
  tHotel.clear();
  tHotel.resize(n,false);
  set<pair<weight_t, vertex_t> > vertex_queue;
  vertex_queue.insert(make_pair(min_distance[source], source));
  while (!vertex_queue.empty()){
    weight_t dist = vertex_queue.begin()->first;
    vertex_t u = vertex_queue.begin()->second;
    vertex_queue.erase(vertex_queue.begin());
    vector<neighbor> &neighbors = adjacency_list[u];
    for (vector<neighbor>::iterator neighbor_iter = neighbors.begin(); neighbor_iter != neighbors.end(); neighbor_iter++){
      vertex_t v = neighbor_iter->target;
      weight_t weight = neighbor_iter->weight;
      weight_t current= neighbor_iter->current;
      weight_t distance_through_u = dist + weight;
      if (distance_through_u < min_distance[v]) {
        bool l=true;
        bool ho=false;//Variavel criada para falar se dormiu no hotel ou nao
        //if(distance_through_u>600){
        if(weight+weights[u]>600){
          //Procurando por um hotel
          if(hoteis.size()==0)
            l=false;
          for(vector<vertex_t>::iterator it=hoteis.begin();it!=hoteis.end();it++){
            if(*it==u){
              l=true;
              ho=true;
              break;
            }
            else{
              l=false;
            }
          }
        }
        if(l){
          if(ho){
            tHotel[v]=true;
            weights[v]=weight;
            //cout<<"O nó u= "<<u<<" e nó v= "<<v<<" precisaram de hotel! Entao o peso é: "<<weights[v]<<endl;
          }
          else{
            tHotel[v]=false;
            weights[v]=distance_through_u;
            //cout<<"O nó u= "<<u<<" e nó v= "<<v<<" NÃO precisaram de hotel! Entao o peso é: "<<weights[v]<<endl;
          }
          vertex_queue.erase(make_pair(min_distance[v], v));
          min_distance[v] = distance_through_u;
          previous[v] = u;
          vertex_queue.insert(make_pair(min_distance[v], v));
        }
      }
    }
  }
}

list<vertex_t> DijkstraGetShortestPathTo( vertex_t vertex, const vector<vertex_t> &previous){
   list<vertex_t> path;
   for (;vertex != -1; vertex = previous[vertex])
     path.push_front(vertex);
   return path;
}

int main(){
  int n=1,m,x,y,w,v;
  while(n!=0){
    vector<vertex_t> hoteis;
    cin>>n;
    adjacency_list_t adjacency_list(n);
    if(n==0)
      break;
    cin>>v;
    for(int i=0;i<v;i++){
      cin>>x;
      hoteis.push_back(x-1);
    }
    cin>>m;
    for(int i=0;i<m;i++){
      cin>>x>>y>>w;
      adjacency_list[x-1].push_back(neighbor(y-1,w,0,false));
    }
    vector<weight_t> min_distance;
    vector<vertex_t> previous;
    vector<vertex_t> weights;
    vector<bool> tHotel;
    DijkstraComputePaths(0, adjacency_list, min_distance, previous,hoteis,weights,tHotel);
    //cout << "Distance from 0 to "<<n-1<<" : " << min_distance[n-1] << endl;
    list<vertex_t> path = DijkstraGetShortestPathTo(n-1, previous);
    //cout<<"Weights: ";
    //copy(min_distance.begin(),min_distance.end(),ostream_iterator<vertex_t>(cout," "));
    //cout<<endl;
    //cout << "Path : ";
    //copy(path.begin(), path.end(), ostream_iterator<vertex_t>(cout, " "));
    //cout<<endl;
    int total=0;
    //for(vector<bool>::iterator it=tHotel.begin();it!=tHotel.end();it++){
    //  cout<<*it<<" ";
    //}
    for(list<vertex_t>::iterator it=path.begin();it!=path.end();it++){
      if(tHotel[*it]==1)
        total++;
    }
    //cout<<endl;
    if(min_distance[n-1]==max_weight){
      cout<<"-1\n";
    }
    else{
      cout<<total<<endl;
    }
  }
  return 0;
}

thanks for any help.

1 Answers1

2

The problem can be simplified by first computing the minimum travel times between the hotels. That means you compute from each hotel, the minimum time it takes via the roads to reach every other hotel.

Then, compute the shortest distance from city 1 to every hotel, similarly for city n.

Using the new distances, form a new graph using the shortest path between hotels and the 2 other cities. If the path length is >600, then ignore it (that means do not put it in the new graph). Then use Dijkstra with each edge having a weight of 1. This will give you the minimum number of hotels needed to reach the destination + 1. If there is no path in the new graph, then it is impossible.

The algorithm would take O((k+2)m log n + m log (k+2)) = O(km log n)

Benson Lin
  • 1,394
  • 13
  • 17