[URI Online Judge] – 1529 – Max, the Mad

In the year 2042, after the emergence of malevolent Union of Independent Republics (URI), humanity was faced with a huge lack of resources. Water and gasoline have become assets very valuable, with much of the technology was lost after URI taking the world power.

You are part of a resistance group that aims to take away the power of the URI. Max, the hero of the resistance, must perform various missions involving car trips between cities. There are gas stations in each city, despite the high prices. As financial resources to the resistence are limited, you have been asked to write a program that calculates what the minimum amount of credits required to complete each one of the Max missions.


The input consists of several test cases. Each test case is started by three integers N, M and T, (1 ≤ N ≤ 10, 1 ≤ M ≤ 20, 1 ≤ T ≤ 50) corresponding to the number of cities along the route, the number of roads and capacity of the car Max liters tank. The input ends when N = M = T = 0.

The following M lines describe the bonds between the cities. Each line contains the integers A, B and C, (1 ≤ C ≤ 1000) indicating the existence of a route (roundtrip) between cities A and B, with a consumption of C liter of gasoline. Due to the poor state of the roads, it is possible that certain cities are inaccessible. There is more of a direct route between any pair of cities.

The next N lines describe the cost in credits from the union per liter of petrol in each city. The first line describes the cost of city gas in the first, the second line depicts the cost of the second city, and so on.


For each test case, your program should print a line containing the lowest possible cost to travel from town 1 to town N. If it is not possible to travel between cities, print -1.

Sample Input Sample Output
3 2 50
1 2 30
2 3 30
3 1 10
1 2 20
0 0 0


my solution:

#include <iostream>
#include <cstring>
#include <climits>
#include <vector>
#include <algorithm>
#include <queue>
#include <cstdio>
#define MAX 15
using namespace std;
struct Edge {
int v, t, c;
Edge(int v, int t, int c) : v(v), t(t), c(c) {}
inline bool operator < (const Edge& that) const { return c < that.c; }
vector<Edge> G[MAX];
priority_queue<Edge> Q;
int n, m, s, t;
int V[MAX][60], P[MAX];
int main() {
int n, m, k;
while(scanf("%d %d %d", &n, &m, &k), n|m|k) {
memset(V, 0x3f, sizeof(V));
memset(G, 0, sizeof(G));
Q = priority_queue<Edge>();

for(int i=0; i<m; i++) {
int a, b, c;
cin >> a >> b >> c;
G[a].push_back(Edge(b, 0, c));
G[b].push_back(Edge(a, 0, c));

for(int i=1; i<=n; i++)
cin >> P[i];

Q.push(Edge(1, k, 0));

while(!Q.empty()) {
Edge item = Q.top(); Q.pop();

for(int j=0; j<G[item.v].size(); j++) {
Edge e = G[item.v][j];
int tank = item.t - e.c;
if (tank < 0) continue;

for(int jj=0; jj<=k-tank; jj++) {
int newc = item.c + P[e.v]*jj;
if (newc < V[e.v][tank+jj]) {
V[e.v][tank+jj] = newc;
Q.push(Edge(e.v, tank+jj, newc));

int minn = 0x3f3f3f3f;
for(int i=0; i<=k; i++) {
minn = min(minn, V[n][i]);

cout << (minn<0x3f3f3f3f?minn:-1) << endl;


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