• Sicily 1031. Campus


    1031. Campus

    Constraints

    Time Limit: 1 secs, Memory Limit: 32 MB

    Description

    At present, Zhongshan University has 4 campuses with a total area of 6.17 square kilometers sitting respectively on both sides of the Pearl River or facing the South China Sea. The Guangzhou South Campus covers an area of 1.17 square kilometers, the North Campus covers an area of 0.39 square kilometers, the Guangzhou East Campus has an area of 1.13 square kilometers and the Zhuhai Campus covers an area of 3.48 square kilometers. All campuses have exuberance of green trees, abundance of lawns and beautiful sceneries, and are ideal for molding the temperaments, studying and doing research.

     

           Sometime, the professors and students have to go from one place to another place in one campus or between campuses. They want to find the shortest path between their source place S and target place T. Can you help them?

    Input

    The first line of the input is a positive integer C. C is the number of test cases followed. In each test case, the first line is a positive integer N (0<N<=100) that represents the number of roads. After that, N lines follow. The i-th(1<=i<=N) line contains two strings Si, Ti and one integer Di (0<=Di<=100). It means that there is a road whose length is Di between Si and Ti. Finally, there are two strings S and T, you have to find the shortest path between S and T. S, T, Si(1<=i<=N) and Ti(1<=i<=N) are all given in the following format: str_Campus.str_Place. str_Campus represents the name of the campus, and str_Place represents the place in str_Campus. str_Campus is "North", "South", "East" or "Zhuhai". str_Place is a string which has less than one hundred lowercase characters from "a-z". You can assume that there is at most one road directly between any two places.

    Output

    The output of the program should consist of C lines, one line for each test case. For each test case, the output is a single line containing one integer. If there is a path between S and T, output the length of the shortest path between them. Otherwise just output "-1" (without quotation mark). No redundant spaces are needed.

    Sample Input

    1
    2
    South.xiaolitang South.xiongdelong 2
    South.xiongdelong Zhuhai.liyuan 100
    South.xiongdelong South.xiaolitang
    

    Sample Output

    2

    Problem Source

    ZSUACM Team Member

    Dijkstra,没什么好说的

    这题神坑的地方就是:如果两个地点一个在列表中一个不在列表中,输出-1

    两个都不在列表中但是两个地点相同输出0

    注意判定。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <cstring>
     5 #include <queue>
     6 #include <map>
     7 #define rep(i,l,r) for(int i = l; i <= r; i++)
     8 #define clr(x,y) memset(x,y,sizeof(x))
     9 #define travel(x) for(Edge *p = last[x]; p; p = p -> pre)
    10 using namespace std;
    11 const int INF = 0x3f3f3f3f;
    12 const int maxn = 210;
    13 inline int read(){
    14     int ans = 0, f = 1; char c = getchar();
    15     for(; !isdigit(c); c = getchar()) if (c == '-') f = -1;
    16     for(; isdigit(c); c = getchar()) ans = ans * 10 + c - '0';
    17     return ans * f;
    18 }
    19 struct Edge{
    20     Edge *pre; int to, cost;
    21 }edge[510];
    22 Edge *pt, *last[maxn];
    23 struct Node{
    24     int x, d;
    25     Node(int _x,int _d) : x(_x), d(_d){    }
    26     inline bool operator < (const Node &_Tp) const{
    27         return d > _Tp.d;
    28     }
    29 };
    30 priority_queue <Node> q;
    31 string si, ti, s, t;
    32 int n, cnt, S, T, d[maxn];
    33 inline void addedge(int x,int y,int z){
    34     pt->pre = last[x]; pt->to = y; pt->cost = z; last[x] = pt++; 
    35 }
    36 void dijkstra(){
    37     while (!q.empty()) q.pop();
    38     clr(d,INF); d[S] = 0;
    39     q.push(Node(S,0));
    40     while (!q.empty()){
    41         Node now = q.top(); q.pop();
    42         if (d[now.x] != now.d) continue;
    43         travel(now.x){
    44             if (d[p->to] > d[now.x] + p->cost){
    45                 d[p->to] = d[now.x] + p->cost;
    46                 q.push(Node(p->to,d[p->to])); 
    47             }
    48         }
    49     }
    50 }
    51 void work(){
    52     clr(last,0); pt = edge; cnt = 0;
    53     n = read();
    54     map <string, int> list;
    55     rep(i,1,n){
    56         cin >> si >> ti;
    57         int x = read();
    58         if (!list.count(si)) list.insert(pair <string,int> (si,++cnt));
    59         if (!list.count(ti)) list.insert(pair <string,int> (ti,++cnt));
    60         addedge(list[si],list[ti],x);
    61         addedge(list[ti],list[si],x);
    62     }
    63     cin >> s >> t;
    64     if ((!list.count(s)) || (!list.count(t))){
    65         if (s != t){
    66             printf("-1
    "); return;
    67         }
    68         else printf("0
    "); return;
    69     }
    70     S = list[s]; T = list[t];
    71     if (S == T){
    72         printf("0
    "); return;
    73     }
    74     dijkstra();
    75     if (d[T] == INF) printf("-1
    "); else printf("%d
    ",d[T]);
    76 }
    77 int main(){
    78     int C = read();
    79     while (C--) work();
    80     return 0;
    81 }
    View Code
  • 相关阅读:
    day09页面的声明周期函数
    day8小程序的运行环境与基本架构
    day09小程序复习
    day08前后端交互
    day07获取图片
    day07获取用户地址信息
    MySQL 主从同步延迟的原因及解决办法
    升级Oracle 19c经验: TTS 在使用datapump导matadata时EXCLUDE=STATISTICS 不启作用
    12c,19c自动kill长时间未活动会话特性
    SuSE11单实例二进制安装MySQL5.7
  • 原文地址:https://www.cnblogs.com/jimzeng/p/sicily1031.html
Copyright © 2020-2023  润新知