zoukankan      html  css  js  c++  java
  • uva 10246 最短路

    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 (¹ 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


    (World Finals Warm-up Contest, Problem Setter: Rezaul Alam Chowdhury)

    照敲自:http://www.fookwood.com/archives/333

    题意是求任意两点间路径边权和与该路径上最大点权和值的最小值。

    首先Dijkstra枚举所有点(比如u)作为路径上的最大点权的点,同时得到从u到其它所有点的最短距离, 记下这些值。

    则对x,y的询问,答案为min{(x, u, y)}, 枚举u。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<vector>
    #include<cstdlib>
    #include<algorithm>
    #include<queue>
    #include<map>
    #include<stack>
    
    using namespace std;
    
    #define LL long long
    #define UINT unsigned int
    #define MAX_INT 0x7fffffff
    #define cint const int
    
    #define MAXN 100
    #define INF 100000000
    #define MAXM 2200
    
    struct edge{
        int u, v, w, nxt;
    }e[MAXM];
    int h[MAXN], cc, n, m, q;
    int f[MAXN], mdis[MAXN][MAXN];
    
    void add(int u, int v, int w){
        e[cc]=(edge){u, v, w, h[u]};
        h[u]=cc++;
        e[cc]=(edge){v, u, w, h[v]};
        h[v]=cc++;
    }
    
    struct node{
        int u, d;
        bool operator < (const node &rhs)const{
            return d > rhs.d;
        }
    };
    
    int d[MAXN];
    bool done[MAXN];
    void Dijkstra(cint s){
        priority_queue<node> q;
        fill_n(d+1, n, INF);    fill_n(done+1, n, false);
        q.push((node){s, d[s]=0});
        while(!q.empty()){
            node ut = q.top();      q.pop();
            int u = ut.u;
            if(done[u]) continue;       done[u]=true;
            for(int i=h[u]; i!=-1; i=e[i].nxt){
                int v = e[i].v, w = e[i].w;
                if(d[v]>d[u]+w && f[v]<=f[s]){
                    d[v] = d[u] + w;
                    q.push((node){v, d[v]});
                }
            }
        }
    }
    
    int main(){
    //    freopen("C:\Users\Administrator\Desktop\in.txt","r",stdin);
        int cases = 1;
        while(scanf(" %d %d %d", &n, &m, &q)==3 && (n || m ||q)){
            int i, j, u, v, w;
            for(i=1; i<=n; i++) scanf(" %d", f+i);
            fill_n(h+1, n, -1);     cc=0;
            while(m--){
                scanf(" %d %d %d", &u, &v, &w);
                add(u, v, w);
            }
    
            for(i=1; i<=n; i++){
                Dijkstra(i);
                for(j=1; j<=n; j++)
                    mdis[i][j]=d[j];
            }
    
            if(cases>1) printf("
    ");
            printf("Case #%d
    ", cases++);
            while(q--){
                scanf(" %d %d", &u, &v);
                int ans = INF;
                for(i=1; i<=n; i++){
                    if(mdis[i][u]==INF) continue;
                    if(mdis[i][v]==INF) continue;
                    ans = min(ans, mdis[i][u]+f[i]+mdis[i][v]);
                }
                if(ans == INF) printf("-1
    ");
                else printf("%d
    ", ans);
            }
        }
        return 0;
    }
    
  • 相关阅读:
    OpenOffice 在 Linux 下安装使用
    LINUX环境使用rpm命令安装gcc
    用shell脚本执行mysql脚本
    CentOS 7下用firewall-cmd控制端口与端口转发详解(转载)
    Hive字段中文注释乱码解决办法
    真正从零开始,TensorFlow详细安装入门图文教程!
    九、Oracle SQL(优化专题&分表分区)
    八、Oracle SQL(聚合函数&标量函数)
    七、Oracle SQL(Oracle 定时任务基础)
    六、Oracle SQL(游标&RECORD&存过、游标、RECORD整合)
  • 原文地址:https://www.cnblogs.com/ramanujan/p/3377196.html
Copyright © 2011-2022 走看看