zoukankan      html  css  js  c++  java
  • AHU-412 Bessie Come Home 【Dijkstra】

    Description
    It's dinner time, and the cows are out in their separate pastures. Farmer John rings the bell so they will start walking to the barn. Your job is to figure out which one cow gets to the barn first (the supplied test data will always have exactly one fastest cow).

    Between milkings, each cow is located in her own pasture, though some pastures have no cows in them. Each pasture is connected by a path to one or more other pastures (potentially including itself). Sometimes, two (potentially self-same) pastures are connected by more than one path. One or more of the pastures has a path to the barn. Thus, all cows have a path to the barn and they always know the shortest path. Of course, cows can go either direction on a path and they all walk at the same speed.

    The pastures are labeled `a'..`z' and `A'..`Y'. One cow is in each pasture labeled with a capital letter. No cow is in a pasture labeled with a lower case letter. The barn's label is `Z'; no cows are in the barn, though.
    Input
    Line 1: Integer P (1 <= P <= 10000) the number of paths that interconnect the pastures (and the barn)
    Line 2..P+1: Space separated, two letters and an integer: the names of the interconnected pastures/barn and the distance between them (1 <= distance <= 1000)
    Output
    A single line containing two items: the capital letter name of the pasture of the cow that arrives first back at the barn, the length of the path followed by that cow.
     
    Sample Input
    5
    A d 6
    B d 3
    C e 9
    d Z 8
    e Z 3
    Sample Output
    B 11
    
    

    单源最短路+Dijkstra,细节处理要注意。


    Code:
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define M(a, b) memset(a, b, sizeof(a))
     4 #define INF 0x3f3f3f3f
     5 const int maxn = 100;
     6 int d[maxn];
     7 bool done[maxn];
     8 bool cow[maxn];
     9 struct Edge {
    10     int from, to, dist;
    11 };
    12 struct Node {
    13     int d, u;
    14     bool operator < (const Node& rhs) const {
    15         return d > rhs.d;
    16     }
    17 };
    18 vector<int> G[maxn];
    19 vector<Edge> edges;
    20 
    21 void init() {
    22     memset(cow, 0, sizeof(cow));
    23     for (int i = 0; i <= 52; ++i) G[i].clear();
    24     edges.clear();
    25 }
    26 
    27 void AddEdge(int from, int to, int dist) {
    28     edges.push_back(Edge{from, to, dist});
    29     int sz = edges.size();
    30     G[from].push_back(sz-1);
    31 }
    32 
    33 void Dijkstra(int s) {
    34     memset(done, 0, sizeof(done));
    35     memset(d, INF, sizeof(d));
    36     priority_queue<Node> Q;
    37     d[s] = 0;
    38     Q.push(Node{0, s});
    39     while(!Q.empty()) {
    40         Node x = Q.top(); Q.pop();
    41         int u = x.u;
    42         if (done[u]) continue;
    43         done[u] = true;
    44         int len = G[u].size();
    45         for (int i = 0; i < len; ++i) {
    46             Edge& e = edges[G[u][i]];
    47             if (d[e.to] > d[u] + e.dist) {
    48                 d[e.to] = d[u] + e.dist;
    49                 Q.push(Node{d[e.to], e.to});
    50             }
    51         }
    52     }
    53 } 
    54 
    55 int main() {
    56     int n, aa, bb, dd;
    57     char a[3], b[3];
    58     while(~scanf("%d", &n)) {
    59         init();
    60         for (int i = 0; i < n; ++i) {
    61             scanf("%s%s%d", a, b, &dd);
    62             if (a[0] <= 'Z') aa = a[0] - 'A' + 26, cow[aa] = 1;
    63             else aa = a[0] - 'a';
    64             if (b[0] <= 'Z') bb = b[0] - 'A' + 26, cow[bb] = 1;
    65             else bb = b[0] - 'a';
    66             AddEdge(aa, bb, dd);
    67             AddEdge(bb, aa, dd);
    68         }
    69         Dijkstra(51);
    70         int ans, ansd = INF;
    71         for (int i = 26; i < 51; ++i)
    72             if (cow[i] && d[i] < ansd) {
    73                 ansd = d[i];
    74                 ans = i;
    75             }
    76         printf("%c %d
    ", ans-26+'A', ansd);
    77     }
    78 
    79     return 0;
    80 }
  • 相关阅读:
    vue数据传递--我有特殊的实现技巧
    解决Vue引入百度地图JSSDK:BMap is undefined 问题
    vue-quill-editor-upload : 实现vue-quill-editor上传图片到服务器
    vue.js的<slot>
    实例化vue发生了什么?(详解vue生命周期)
    vue2实现自定义样式radio单选框
    vue-lazyload插件
    Axios 使用时遇到的问题
    Vue组件开发 -- Markdown
    Javascript系列——对象元素的数组去重实现
  • 原文地址:https://www.cnblogs.com/robin1998/p/6481508.html
Copyright © 2011-2022 走看看