UVa 10246 Asterix and Obelix (shortest path of deformation)

Source: Internet
Author: User

10246-asterix and Obelix

Time limit:3.000 seconds

Problem A

Asterix and Obelix

input: standard input

output: standard output

Time Limit: 5 Seconds

Memory Limit: MB

After winning a gruesome battle against the Romans on a Far-away land, Asterix and his dearest friend Obelix is now retur Ning home. However Obelix is isn't with Asterix now. He had left Asterix in order to deliver Menhir to one of his international buyers (as you probably know, recently he had E Xtended he trade to international. But he had promised to join Asterix the his and Asterix have promised to host a feast for Obelix (you know how fat He is!) In the city they meet. Obelix may meet Asterix in any city to the home including the starting and the destination city.

Now Asterix are sitting with a maps and trying to figure out the cheapest route home. The map shows the cities and the cost (in sestertii) of going from one city to another if there are a road connecting them Directly. For each city in the map Asterix have also calculated the cost (in sestertii) of hosting a feast for Obelix in. There'll be is only one feast and for safety Asterix have decided to set aside enough sestertii to host a feast in the Costl Iest City on the route.

Since Asterix does not has a computer, he seeks your help to find out the cheapest route home.

Input

The input may contain multiple test cases.

The first line of all test case contains three integers C (£80), R (£1000) and Q (£6320) WH Ere C indicates the number of cities (cities is numbered using distinct integers ranging from 1 to C), R represents the number of roads and Q is the number of queries.

The next line contains C integers where the i-th integer fi are the cost (in sestertii) of Hosti Ng a feast in city I.

Each of the next R lines contains three integers: C1, C2 (1 C1) and D indicating so the cost Of going from the city C1 to C2 (or from C2 to C1) is D sestertii.

Each of the next Q lines contains, integers C1 and C2 (C1 1 c2) asking for the cost (in SE stertii) of the cheapest route from city C1 to City C2.

The input would terminate with three zeros form C, S and Q.

Output

For each test, the input first output the test Case number (starting from 1) as shown in the sample output. Then for each query with the input print a line giving the minimum cost (in sestertii) of going from the first to the second City in the query. If there exists no path between them just print "–1".

Print a blank line between consecutive test cases.

Sample Input

7 8 5

2 3 5 15 4 4 6

1 2 20

1 4 20

1 5 50

2 3 10

3 4 10

3 5 10

4 5 15

6 7 10

1 5

1 6

5 1

3 1

6 7

4 4 2

2 1 8 3

1 2 7

1 3 5

2 4 8

3 4 6

1 4

2 3

0 0 0

Sample Output

Case #1

45

-1

45

35

16

Case #2

18

20



idea: SPFA find the shortest path to the remaining vertices at each point (the Val is less thaninAnd then a two-dimensional array dis to save,the last question is to enumerate the intermediate points I.ans = min{dis[i][u]+dis[i][v]+cost[i]};


#include <iostream> #include <cstdio> #include <queue> #include <cstring> #include <vector    >using namespace Std;const int inf=999999999;const int maxn=110;struct node{int v,t; Node () {} node (int _v,int _t): V (_v), T (_t) {}};vector <node> g[maxn];int c,r,q,cnt,dis[maxn][maxn],w[maxn];bool    Visited[maxn];void SPFA (int x) {memset (visited,0,sizeof (visited));    Queue <int> q;    Q.push (x);    dis[x][x]=0;    Visited[x]=1;        while (!q.empty ()) {int C=q.front ();        Q.pop ();        visited[c]=0;            for (int i=0; i<g[c].size (); i++) {int v=g[c][i].v;                if (dis[x][v]>dis[x][c]+g[c][i].t && w[x]>=w[v]) {dis[x][v]=dis[x][c]+g[c][i].t;                    if (!visited[v]) {Q.push (v);                Visited[v]=1;    }}}}}void initial () {for (int i=0;i<maxn;i++) g[i].clear (); for (int i=0;i<maxn;i++) for (int j=0;j<maxn;j++) Dis[i][j]=inf;}    void input () {int x, y, Z;    for (int i=1;i<=c;i++) scanf ("%d", &w[i]);        for (int i=0;i<r;i++) {scanf ("%d%d%d", &x,&y,&z);        G[x].push_back (Node (y,z));    G[y].push_back (Node (x,z));     }}void solve () {int x, y;     for (int i=1;i<=c;i++) SPFA (i);     printf ("Case #%d\n", ++cnt);         for (int co=0;co<q;co++) {scanf ("%d%d", &x,&y);         int ans=inf;         for (int i=1;i<=c;i++) ans=min (Ans,dis[i][x]+dis[i][y]+w[i]);         if (ans<inf) printf ("%d\n", ans);     else printf (" -1\n"); }}int Main () {while (scanf ("%d%d", &c,&r,&q)!=eof) {if (c==0 && r==0 && q==0         ) break;         if (CNT) printf ("\ n");         Initial ();         Input ();     Solve (); } return 0;}



UVa 10246 Asterix and Obelix (shortest path of deformation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.