Re: 10099 - The Tourist Guide
Posted: Sun Jun 22, 2014 12:45 pm
A little modification to the problem. I want print the path along which the guide has to travel. How to do that?
Code: Select all
#include <iostream>
#include <cmath>
#include <fstream>
#include <vector>
#include <stack>
#include <sstream>
#include <map>
#include <iomanip>
#include <algorithm>
#include <stdint.h>
#include <exception>
#include <set>
#include <queue>
using namespace std;
struct Pred {
Pred(int i): val(i) {}
bool operator() (std::pair<const std::pair<int, int>, long int> &p) {
return p.first.first == val;
}
private:
int val;
};
int main() {
int N, R;
queue<int> qu;
map<int, bool> visited_cities;
map<int, bool> queued_cities;
map<int, long int> carry_count;
map<int, long int> carry_count_min;
int count = 0;
while(count++, true) {
visited_cities.clear(); queued_cities.clear(); carry_count_min.clear(); carry_count.clear();
cin >> N >> R;
if(N==0 && R==0) break;
for(int i=1; i<=N; ++i) {
visited_cities[i] = false;
queued_cities[i] = false;
carry_count[i] = numeric_limits<int>::max();
carry_count_min[i] = numeric_limits<int>::max();
}
map<pair<int, int>, long int> ways;
int c1, c2, p;
while (R--) {
cin >> c1 >> c2 >> p;
ways[{c2, c1}] = p;
ways[{c1, c2}] = p;
}
int start, end, passengers;
cin >> start >> end >> passengers;
qu.push(start);
queued_cities[start] = true;
carry_count[start] = 0;
while(!qu.empty()) {
int working = qu.front(); qu.pop();
if(working == end) continue;
visited_cities[working] = true;
map<pair<int, int>, long int>::iterator it = ways.begin();
while((it = find_if(it, ways.end(), Pred(working)))!= ways.end()) {
if(visited_cities[it->first.second]) {
it++;
continue; // if I visited here previously
}
if(!queued_cities[it->first.second]) { //enqueue city unless it has been previously, not necessarily needed though. already we are mimicking this by visited flag
qu.push(it->first.second);
queued_cities[it->first.second] = true;
}
double rounds = 1+((passengers-1) / (it->second-1)); //find rounds to make on this road minus 1 is from algorithm, plus 1 is the guide taking this seat
if(rounds > 0 && carry_count[working]+(int)rounds < carry_count[it->first.second]) { //if my working cells carry count plus the roads carry count is smaller than previous carry counts
carry_count[it->first.second] = carry_count[working]+(int)rounds;
carry_count_min[it->first.second] = min(it->second, carry_count_min[working]);
}
it++;
}
}
//cout << carry_count[end] << endl;
cout << "Scenario #" << count << endl;
double rounds;
if(passengers==0) rounds = 0;
else rounds = 1+((passengers-1) / (carry_count_min[end]-1));
cout << "Minimum Number of Trips = " << static_cast<long long int>(rounds) << endl << endl;
}
return 0;
}
I was doing this in different way on the next line but nevertheless I did take your advice and changed the code to the following. Unfortunately again my test cases are fine but it is still a WA according to judge.Try solving it without using floating point.
To take the integer ceiling of a divided by b, you could use:
(a + b - 1) / b
Code: Select all
#include <iostream>
#include <cmath>
#include <fstream>
#include <vector>
#include <stack>
#include <sstream>
#include <map>
#include <iomanip>
#include <algorithm>
#include <stdint.h>
#include <exception>
#include <set>
#include <queue>
using namespace std;
struct Pred {
Pred(int i) : val(i) {}
bool operator() (std::pair<const std::pair<int, int>, long int> &p) {
return p.first.first == val;
}
private:
int val;
};
int main() {
int N, R;
queue<int> qu;
map<int, bool> visited_cities;
map<int, bool> queued_cities;
map<int, long int> carry_count;
map<int, long int> carry_count_min;
int count = 0;
while (count++, true) {
visited_cities.clear(); queued_cities.clear(); carry_count_min.clear(); carry_count.clear();
cin >> N >> R;
if (N == 0 && R == 0) break;
for (int i = 1; i <= N; ++i) {
visited_cities[i] = false;
queued_cities[i] = false;
carry_count[i] = numeric_limits<int>::max();
carry_count_min[i] = numeric_limits<int>::max();
}
map<pair<int, int>, long int> ways;
int c1, c2, p;
while (R--) {
cin >> c1 >> c2 >> p;
ways[{c2, c1}] = p;
ways[{c1, c2}] = p;
}
int start, end, passengers;
cin >> start >> end >> passengers;
qu.push(start);
queued_cities[start] = true;
carry_count[start] = 0;
while (!qu.empty()) {
int working = qu.front(); qu.pop();
if (working == end) continue;
visited_cities[working] = true;
map<pair<int, int>, long int>::iterator it = ways.begin();
while ((it = find_if(it, ways.end(), Pred(working))) != ways.end()) {
if (visited_cities[it->first.second]) {
it++;
continue; // if I visited here previously
}
if (!queued_cities[it->first.second]) { //enqueue city unless it has been previously, not necessarily needed though. already we are mimicking this by visited flag
qu.push(it->first.second);
queued_cities[it->first.second] = true;
}
//int rounds = 1 + ((passengers - 1) / (it->second - 1)); //find rounds to make on this road minus 1 is from algorithm, plus 1 is the guide taking this seat
//(a + b - 1) / b
int rounds = (passengers + it->second - 1 - 1) / (it->second-1);
if (rounds > 0 && carry_count[working] + rounds < carry_count[it->first.second]) { //if my working cells carry count plus the roads carry count is smaller than previous carry counts
carry_count[it->first.second] = carry_count[working] + (int)rounds;
carry_count_min[it->first.second] = min(it->second, carry_count_min[working]);
}
it++;
}
}
//cout << carry_count[end] << endl;
cout << "Scenario #" << count << endl;
int rounds;
if (passengers == 0) rounds = 0;
//else rounds = 1 + ((passengers - 1) / (carry_count_min[end] - 1));
else rounds = (passengers + carry_count_min[end] - 1 - 1) / (carry_count_min[end] - 1);
cout << "Minimum Number of Trips = " << rounds << endl << endl;
}
return 0;
}
Code: Select all
Acc
turcse143 wrote:check this input:hope it helps.Code: Select all
input: 7 10 1 2 30 1 3 15 1 4 10 2 4 25 2 5 60 3 4 40 3 6 20 4 7 35 5 7 20 6 7 30 1 1 99 0 0 output: Scenario #1 Minimum Number of Trips = 0
Code: Select all
7 10
1 2 30
1 3 15
1 4 10
2 4 25
2 5 60
3 4 40
3 6 20
4 7 35
5 7 20
6 7 30
1 1 99
7 10
1 2 30
1 3 15
1 4 10
2 4 25
2 5 60
3 4 40
3 6 20
4 7 35
5 7 20
6 7 30
1 1 99
0 0