zoukankan      html  css  js  c++  java
  • USACO 2015 January Contest, Silver Problem 2. Cow Routing Dijkstra

    PROBLEM

    Tired of the cold winter weather on her farm, Bessie the cow plans to fly to a warmer destination for vacation. Unfortunately, she discovers that only one airline, Air Bovinia, is willing to sell tickets to cows, and that these tickets are somewhat complicated in structure.

    Air Bovinia owns N planes (1 <= N <= 1000), each of which flies on a specific "route" consisting of two or more cities. For example, one plane might fly on a route that starts at city 1, then flies to city 5, then flies to city 2, and then finally flies to city 8. No city appears multiple times in a route. If Bessie chooses to utilize a route, she can board at any city along the route and then disembark at any city later along the route. She does not need to board at the first city or disembark at the last city. Each route has a certain cost, which Bessie must pay if she uses any part of the route, irrespective of the number of cities she visits along the route. If Bessie uses a route multiple times during her travel (that is, if she leaves the route and later comes back to use it from antoher city), she must pay for it each time it is used.

    Bessie would like to find the cheapest way to travel from her farm (in city A) to her tropical destination (city B). Please help her decide what is the minimum cost she must pay, and also the smallest number of individual flights she must use take to achieve this minimum cost.

    INPUT: (file cowroute.in)

    The first line of input contains A, B, and N, separated by spaces.

    The next 2N lines describe the available routes, in two lines per route. The first line contains the cost of using the route (an integer in the range 1..1,000,000,000), and the number of cities along the route (an integer in the range 1..100). The second line contains a list of the cities in order along the route. Each city is identified by an integer in the range 1..1000. Note that the cost of an itinerary can easily add up to more than can fit into a 32-bit integer, so you should probably use 64-bit integers (e.g., "long long" integers in C/C++).

    SAMPLE INPUT:

    3 4 3
    3 5
    1 2 3 4 5
    2 3
    3 5 4
    1 2
    1 5

    OUTPUT: (file cowroute.out)

    Output the minimum cost of an itinerary that Bessie can use to travel from city A to city B, as well as the minimum number of individual flights required to achieve this minimum cost. If there is no solution, output "-1 -1" (quotes for clarity) on a single line.

    SAMPLE OUTPUT:

    2 2

    分析

    利用Dijkstra算法,这道题就不难思考了。

    To solve this problem we can compute a matrix costu,v that gives the minimum cost (and for that min cost the min number of hops) to travel from u to v. Then we can use Dijkstra's algorithm to find the cheapest path of intermediate cities that takes us from A to B.

    上面这段USACO分析很容易理解,用一个cost[u][v]数组来存储u→v的最短路径(花费)和最短路径(花费)所对应的最小换乘次数。然后对于这个cost数组进行Dijkstra算法操作。

    程序

     1 #include <iostream>
     2 #include <vector>
     3 #include <cstring>
     4 #include <cstdio>
     5 
     6 using namespace std;
     7 
     8 #define MAXV 1010
     9 
    10 bool vis[MAXV];
    11 pair<long long, int> cost_a2u[MAXV];
    12 pair<long long, int> cost[MAXV][MAXV];
    13 
    14 int main() {
    15   //freopen("cowroute.in", "r", stdin);
    16   //freopen("cowroute.out", "w", stdout);
    17 
    18   int A, B, N;
    19   cin >> A >> B >> N;
    20 
    21   /* Initialize cost matrix to infinity. */
    22   memset(cost, 0x3F, sizeof(cost));
    23   for (int i = 0; i < MAXV; i++) {
    24     cost[i][i] = make_pair(0, 0);
    25   }
    26 
    27   for (int i = 0; i < N; i++) {
    28     long long route_cost;
    29     int route_len;
    30     cin >> route_cost >> route_len;
    31 
    32     vector<int> route(route_len);
    33     for (int j = 0; j < route_len; j++) {
    34       cin >> route[j];
    35 
    36       /* Update the cost from everything before this city to this city. */
    37       for (int k = 0; k < j; k++) {
    38         cost[route[k]][route[j]] = min(cost[route[k]][route[j]], make_pair(route_cost, j - k));
    39       }
    40     }
    41   }
    42 
    43   /* Perform Dijkstra without a heap in O(V^2) time. */
    44   memset(vis, 0, sizeof(vis));
    45   memset(cost_a2u, 0x3F, sizeof(cost_a2u));
    46   cost_a2u[A] = make_pair(0, 0);
    47   for (int i = 0; i < MAXV; i++) {
    48     /* Find the closest unvisited vertex. */
    49     int u = -1;
    50     for (int j = 0; j < MAXV; j++) {
    51       if (vis[j]) {
    52         continue;
    53       } else if (u == -1 || cost_a2u[j] < cost_a2u[u]) {
    54         u = j;
    55       }
    56     }
    57 
    58     /* Relax vertex u. */
    59     vis[u] = true;
    60     for (int j = 0; j < MAXV; j++) {
    61       pair<long long, int> rlx = cost_a2u[u];
    62       rlx.first += cost[u][j].first;
    63       rlx.second += cost[u][j].second;
    64       cost_a2u[j] = min(cost_a2u[j], rlx);
    65     }
    66   }
    67 
    68   /* Output the cheapest cost and length if possible. */
    69   if (cost_a2u[B].second <= MAXV) {
    70     cout << cost_a2u[B].first << ' ' << cost_a2u[B].second << endl;
    71   } else {
    72     cout << "-1 -1
    ";
    73   }
    74 
    75   return 0;
    76 }
  • 相关阅读:
    开发工程师的职场人生路
    10 ASP.NET Performance and Scalability Secrets(转载推荐)
    手机号码归属地查询接口大全
    参数的秘密“纠结”的压缩比
    爱情本来并不复杂,来来去去不过三个字
    假如苹果、微软、谷歌造汽车 他们的区别是什么
    tomcat修改jsessionid在cookie中的名称
    virtualBox 中 linux 系统 相关配置
    htpasswd用法
    linux 防火墙
  • 原文地址:https://www.cnblogs.com/OIerPrime/p/8227868.html
Copyright © 2011-2022 走看看