码迷,mamicode.com
首页 > 其他好文 > 详细

uva_10246_Asterix and Obelix(最短路)

时间:2015-04-20 09:36:18      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:acm   algorithm   uva   最短路   

10246 Asterix and Obelix

After winning a gruesome battle against the Romans in a faraway 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 delivermenhir to one of his internationalbuyers (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 (?= c1)and d indicating that the cost of going from city c1 to c2 (or from c2 to 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 c1 to city c2.

The input will terminate with three zeros form C, S 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处理出每个点到其他点的最短路(注意:每个点的value都得小于起点的value),用dp数组保存,然后枚举每个点,ans=min( ans,dp[i][u]+dp[i][v]+f[i] )。

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28972

代码清单:

#include<map>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;

const int maxv = 80 + 5;
const int maxe = 1000 + 5;
const int max_dis = 1e9;

struct Edge{
    int to;
    int dis;
    Edge(int to,int dis){
        this -> to = to;
        this ->dis = dis;
    }
};
int C,R,Q;
int f[maxv];
int a,b,c,p,q,ans;
int dp[maxv][maxv];
int vis[maxv],dist[maxv],Case=0;
vector<Edge>G[maxv];

void spfa(int s){
    fill(vis+1,vis+1+C,0);
    fill(dist+1,dist+1+C,max_dis);
    queue<int>q;
    while(q.size()) q.pop();
    dist[s]=0;
    vis[s]=1;
    q.push(s);
    while(q.size()){
        int v=q.front(); q.pop();
        vis[v]=0;
        for(int i=0;i<G[v].size();i++){
            Edge& e=G[v][i];
            if(f[e.to]<=f[s]&&dist[e.to]>dist[v]+e.dis){ //最短路上每个点的value小于起点的value
                dist[e.to]=dist[v]+e.dis;
                if(!vis[e.to]){
                    vis[e.to]=1;
                    q.push(e.to);
                }
            }
        }
    }
    for(int i=1;i<=C;i++) dp[s][i]=dist[i];
}

int main(){
    while(scanf("%d%d%d",&C,&R,&Q)!=EOF){
        if(C==0&&R==0&&Q==0) break;
        for(int i=0;i<maxv;i++) G[i].clear();
        for(int i=1;i<=C;i++) scanf("%d",&f[i]);
        for(int i=0;i<R;i++){
            scanf("%d%d%d",&a,&b,&c);
            G[a].push_back(Edge(b,c));
            G[b].push_back(Edge(a,c));
        }
        if(Case) printf("\n");
        printf("Case #%d\n",++Case);
        for(int i=1;i<=C;i++) spfa(i);
        while(Q--){
            ans=max_dis;
            scanf("%d%d",&p,&q);
            for(int i=1;i<=C;i++){
                ans=min(ans,dp[i][p]+dp[i][q]+f[i]);
            }
            if(ans==max_dis) ans=-1;
            printf("%d\n",ans);
        }
    }return 0;
}




uva_10246_Asterix and Obelix(最短路)

标签:acm   algorithm   uva   最短路   

原文地址:http://blog.csdn.net/jhgkjhg_ugtdk77/article/details/45132259

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!