首页 > 代码库 > UVa 10246 Asterix and Obelix(变形的最短路径)

UVa 10246 Asterix and Obelix(变形的最短路径)

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: 32 MB

 

After winning a gruesome battle against the Romans in a far-away land, Asterix and his dearest friend Obelix are now returning home. However Obelix is not with Asterix now. He has left Asterix in order to deliver menhir to one of his international buyers (as you probably know, recently he has extended his trade to international markets). But he has promised to join Asterix on his way home and Asterix has 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 on his way home including the starting and the destination city.

 

技术分享

 

Now Asterix is sitting with a map 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 is a road connecting them directly. For each city in the map Asterix has also calculated the cost (in sestertii) of hosting a feast for Obelix in that city. There will be only one feast and for safety Asterix has decided to set aside enough sestertii to host a feast in the costliest city on the route.

 

Since Asterix does not have 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 each test case contains three integers C (£ 80)R (£ 1000) and Q (£ 6320) where C indicates the number of cities (cities are 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 is the cost (in sestertii) of hosting a feast in city i.

 

Each of the next R lines contains three integers: c1, c2 (1 c1) and d indicating that the cost of going from city cto c2 (or from cto c1) is d sestertii.

 

Each of the next Q lines contains two integers c1 and c2 (c1 c2) asking for the cost (in sestertii) of the cheapest route from city cto city c2.

 

The input will terminate with three zeros form CS and Q.

 

Output

 

For each test case in the input first output the test case number (starting from 1) as shown in the sample output. Then for each query in 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 two 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



思路:spfa求出每个点到其余顶点的最短路(最短路上的每个点的val都小于等
起点的val),然后又二维数组dis来保存,最后询问的时候就是枚举中间点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 %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(变形的最短路径)