I have written code for the problem “A Node too far” problem id uva 336. But it is giving wrong answer can any one guess what i am doing wrong. Below is my code.
The link to the problem is A Node too far
Also if someone can tell me what is the common reasons for getting a wrong answer and is there any way that we know that against which input our program is creating problems.
#include <iostream>
#include <vector>
#include <list>
#include <map>
unsigned int caseID = 0;
struct Edge;
struct Node{
Node():id(0),cost(0),color(false),added(false){}
int id;
int cost;
bool color;
bool added;
std::list<Edge *>edges;
};
struct Edge{
Edge():destination(0){}
Node *destination;
};
void make_graph(unsigned int sourceid,unsigned int destinationid,
std::map<int, Node*> &mymap, int &totalNodes){
Node *source = 0;
Node *destination = 0;
std::map<int, Node*>::iterator it = mymap.find(sourceid);
if(it == mymap.end()){
source = new Node;
++ totalNodes;
source->id = sourceid;
mymap.insert(std::pair<int,Node*> (sourceid,source));
}
else{
source = it->second;
}
if(sourceid == destinationid){
return;
}
it = mymap.find(destinationid);
if(it == mymap.end()){
++totalNodes;
destination = new Node;
destination->id= destinationid;
mymap.insert(std::pair<int,Node*> (destinationid,destination));
}
else{
destination = it->second;
}
Edge *e = new Edge;
e->destination = destination;
source->edges.push_back(e);
e = new Edge;
e->destination = source;
destination->edges.push_back(e);
}
void delete_graph(std::map<int, Node*> &mymap){
std::map<int,Node*>::iterator it = mymap.begin();
for(;it != mymap.end(); ){
Node *n = it->second;
if(!n){
continue;
}
std::list<Edge *>::iterator myEdge = it->second->edges.begin();
while(myEdge != n->edges.end()){
Edge *e = *myEdge;
if(e){
e->destination = 0;
delete e;
e = 0;
}
++myEdge;
}
delete n;
n = 0;
++it;
std::cout << std::endl;
}
}
void calc_nodes(int value, Node *n, unsigned int &nodesCount, int prevCost){
if(!n->added){
n->cost = ++prevCost;
if(n->cost == value){
++nodesCount;
n->added = true;
return;
}
++nodesCount;
n->added = true;
}else{
n->cost = ++prevCost;
}
std::list<Edge *>::iterator it = n->edges.begin();
while(it != n->edges.end()){
Edge *e = *(it);
if(e->destination->color){
++it;
continue;
}
n->color = true;
e->destination->color = true;
calc_nodes(value,e->destination,nodesCount,n->cost);
++it;
}
}
void clearGraph(std::map<int, Node *>& mymap ){
std::map<int, Node *>::iterator it = mymap.begin();
while(it != mymap.end()){
it->second->added = false;
it->second->color = false;
++it;
}
}
void calc_nodes_aux(int totalNodes,std::map<int,Node *> &mymap){
unsigned int TTL = 0;
Node *source = 0;
unsigned int sourceId = 0;
unsigned int nodesCount = 0;
while(true){
std::cin >> sourceId >>TTL;
if(sourceId == 0 && TTL == 0){
break;
}
std::map<int,Node *>::iterator it = mymap.find(sourceId);
source = it->second;
if(source && TTL > 0){
nodesCount = 0;
clearGraph(mymap);
calc_nodes(TTL,source,nodesCount, -1);
if(caseID > 0){
std::cout <<std::endl;
}
std::cout << "Case "<< ++caseID<<": " <<totalNodes - nodesCount <<
" nodes not reachable from node " <<sourceId << " with TTL = " << TTL<<".";
}
}
}
int main(){
unsigned int edges = 0;
unsigned int sourceId = 0;
unsigned int destinationId = 0;
while(true){
std::cin >>edges;
if(edges == 0){
break;
}
std::map<int,Node*>mymap;
int totalNodes = 0;
for(unsigned int i = 0; i < edges; ++i ){
std::cin >> sourceId >> destinationId;
make_graph(sourceId,destinationId,mymap,totalNodes);
}
calc_nodes_aux(totalNodes,mymap);
delete_graph(mymap);
}
return 0;
}
Your code is extremely complicated for the task given and you do not even pass the example test! Please note that in (almost all) online judges you are required to produce an output that is the same byte-wise as the expected one. You output about 10 additional newline characters after the first test case and even if your code is otherwise correct this will result in wrong answer.
Here are two approaches on how to solve this particular problem with less effort:
All you need to do is to build the graph of nodes and then run a breadth first search from the node in the query. After that count all nodes that have their shortest distance to the initial node greater then the given threshold(the TTL).
As the number of nodes is really small(up to 30), an alternative solution would be to run a Floyd Warshall algorithm. This solution will be even shorter but will not work for much greater constraints.
Both approaches could easily fit in less then 50 lines.
One approach on how to find which test do you have WA is to try generating random graphs and simulate the movement of the packages between any two nodes and compare the result with the one found by your program. Always generate small examples! In this case I believe up to 5 nodes is more then enough.
Second approach, which I generally prefer is to generate graphs by hand and compute the expected answer again by hand. Try to cover as many edge cases as possible(single node in the network, all nodes are reachable, TTL is 0 and so on).
In the online judges only a few offer the option to see which case is your code failing on and UVA is not one of them. This is done for a purpose – it forces you to try and debug your program on your own. Also on ACM finals no one is going to tell you the case that you are failing.