[URI ONLINE JUDGE] – 1931 – Even Obsession

Patricia is an excellent software developer, but, as every brilliant person, she has some strange quirks. One of those is that everything she does has to be in even quantities. Most often that quirk does not affect her, even though it may seem strange to others. Some examples: every day she has to eat an even number of meals; during breakfast, she drinks two cups of coffee, eats two toasts and two slices of cheese; when she goes to the cinema she buys two tickets (fortunately she always has a friend that goes with her); she takes two baths per day (or four, our six…).

Some other times, however, that quirk makes the life of Patricia more difficult. For example, no one wants to travel by car with her because if she has to pay toll, the number of tolls she pays has to be an even number.

Patricia lives in a country where all roads are two-way and have exactly one toll each. She needs to visit a client in a different city, and wants to calculate the minimum total value of tolls she has to pay to go from her city to the client’s city, obeying her strange quirk that she has to pay an even number of tolls.

Input

The input consists of several test cases. The first line of a test case contains two integers C and V, the total number of cities and the number of roads (2 ≤ C ≤ 104 and 0 ≤ V ≤ 50000). The cities are identified by integer numbers from 1 to C. Each road links two different cities, and there is at most one road between each pair of cities. Each of the next V lines contains three integers C1, C2 and G, indicating that the toll value of the road linking cities C1 and C2 is G (1 ≤ C1, C2C and 1 ≤ G ≤ 104). Patricia is currently in city 1 and the client’s city isC.

Output

For each test case in the input your program must output exactly one line, containing exactly one integer, the minimum toll value for Patricia to go from city 1 to city C, paying an even number of tolls, or, if that is not possible, the value ‘-1’.

Input Example Output Example
4 4

1 2 2

2 3 1

2 4 10

3 4 6

12
5 6

1 2 3

2 3 5

3 5 2

5 1 8

2 4 1

4 5 4

-1

 

solution:

 

#include <iostream>
#include <list>
#include <queue>
#define INFINITO 11111111

using namespace std;

class Grafo
{
public:
int V;
list<pair<int, int> > * adj;

Grafo(int V)
{
this->V = V;
adj = new list<pair<int, int> >[V];
}

void addAresta(int v1, int v2, int custo)
{
adj[v1].push_back(make_pair(v2, custo));
}

int dijkstra(int orig, int dest)
{
int dist[V], visitados[V];

priority_queue < pair<int, int>,
vector<pair<int, int> >, greater<pair<int, int> > > pq;

for(int i = 0; i < V; i++)
{
dist[i] = INFINITO;
visitados[i] = false;
}

dist[orig] = 0;
pq.push(make_pair(dist[orig], orig));

while(!pq.empty())
{
pair<int, int> p = pq.top();
int u = p.second;
pq.pop();

if(visitados[u] == false)
{
visitados[u] = true;
list<pair<int, int> >::iterator it;

for(it = adj[u].begin(); it != adj[u].end(); it++)
{
int v = it->first;
int custo_aresta = it->second;

if(dist[v] > (dist[u] + custo_aresta))
{
dist[v] = dist[u] + custo_aresta;
pq.push(make_pair(dist[v], v));
}
}
}
}

return dist[dest];
}
};

int main(int argc, char *argv[])
{
int C, V;

cin >> C;
cin >> V;

Grafo g(C), g_aux(C);

for(int i = 0; i < V; i++)
{
int v1, v2, custo;
cin >> v1;
cin >> v2;
cin >> custo;

g.addAresta(v1 - 1, v2 - 1, custo);
g.addAresta(v2 - 1, v1 - 1, custo);
}

for(int i = 0; i < C; i++)
{
list<pair<int, int> >::iterator it;
for(it = g.adj[i].begin(); it != g.adj[i].end(); it++)
{
int adj = it->first;
int custo = it->second;
list<pair<int, int> >::iterator it2;
bool tam_adj = g.adj[adj].size();

if(tam_adj > 0)
{
for(it2 = g.adj[adj].begin(); it2 != g.adj[adj].end(); it2++)
{
int adj2 = it2->first;
int custo2 = it2->second;
g_aux.addAresta(i, adj2, custo + custo2);
}
}
}
}

int custo_min = g_aux.dijkstra(0, C - 1);

if(custo_min == INFINITO)
cout << "-1\n";
else
cout << custo_min << endl;
return 0;
}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s