zoukankan      html  css  js  c++  java
  • 2016江苏省CPC省赛 I

    Description

    Mike moved to a new city.

    There are bus stations in the city, each has a unique name. Each bus has its designated schedule, and sequentially docks at a series of bus stations. Bus lines are bi-directional, and thus you can get on the bus at a station, and get off at any other station in that bus' line. The city provides two kinds of bus services:

    1. Type A: each ride costs $2.

    2. Type B: rides are completely free of charge.

    Given all bus lines in the city, a source station and a destination station, you should help Mike to find the cheapest ride plan to reach the destination from the source.

    Input

    First line: a positive integer T (T <= 10) indicating the number of test cases.

    There are T cases following. In each case, the rst line contains n (1 <= n <= 1,000) indicating the number of bus lines. Then followed by n lines, each of which describes a bus line in the format of t k s1 s2 ... sk (1 <= k <= 10). Speci cally, t is the type of the bus (either A or B), k denotes the number of bus stations in that line, while strings s1,s2,... sk list names of these stations (a bus line may contain duplicated stations) The last line of the case contains two strings: Mike's source s and destination t. All bus station names are case-sensitive alphabets and is no longer than 20. Input guarantees the destination to be reachable.

    Output

    For each test case: output "Case #x: ans" (without quotes), where x is the number of the case, and ans is the minimum amount of money to reach the destination. 

    Sample Input

    1
    3
    A 5 NJU ZSL XJK YT ATZX
    B 3 XJK HSDWY MGQ
    A 3 HSDWY NJZ MGQ
    NJU NJZ

    Sample Output

    Case #1: 4

    spfa的应用,邻接矩阵开不出来,使用邻接表。

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<vector>
     4 #include<queue>
     5 #include<cstring>
     6 #include<map>
     7 using namespace std;
     8 #define MAX 10005
     9 #define INF 0x3f3f3f3f
    10 struct Edge{
    11     int from,to,weight;
    12     Edge(int u,int v,int w):from(u),to(v),weight(w){};
    13 };
    14 vector<Edge> E;
    15 vector<int> G[MAX];
    16 void add_edge(int u,int v,int w)
    17 {
    18     E.push_back(Edge(u,v,w));
    19     G[u].push_back(E.size()-1);
    20 }
    21 map<string,int> No;
    22 void init()
    23 {
    24     E.clear();
    25     No.clear();
    26     for (int i = 0; i<MAX; i++) G[i].clear();
    27 }
    28 bool vis[MAX];
    29 int d[MAX];
    30 void spfa(int st)
    31 {
    32     memset(d,INF,sizeof(d));
    33     memset(vis,false,sizeof(vis));
    34     queue<int> q;
    35     q.push(st);
    36     d[st]=0;
    37     vis[st]=1;
    38     while (!q.empty())
    39     {
    40         int u=q.front(); 
    41         q.pop(); 
    42         vis[u]=0;
    43         for(int i=0;i<G[u].size();i++)
    44         {
    45             Edge &e=E[G[u][i]];
    46             int tmp=d[e.to];
    47             if(d[e.to]>d[e.from]+e.weight) d[e.to]=d[e.from]+e.weight;
    48             if(d[e.to]<tmp && !vis[e.to])
    49             {
    50                 q.push(e.to);
    51                 vis[e.to]=1;
    52             }
    53         }
    54     }
    55 }
    56 
    57 int main()
    58 {
    59     int n,t;
    60     scanf("%d",&t);
    61     for(int kase=1;kase<=t;kase++)
    62     {
    63         init();
    64         scanf("%d",&n);
    65         int cnt=1;
    66         for(int i=1;i<=n;i++)
    67         {
    68             char type;
    69             int stop_num;
    70             string stop[12];
    71             cin>>type>>stop_num;
    72             for(int j=1;j<=stop_num;j++)
    73             {
    74                 cin>>stop[j];
    75                 if(No.count(stop[j])==0) No[stop[j]] = cnt++;
    76             }
    77             for(int u=1;u<=stop_num;u++)
    78             {
    79                 for(int v=u+1;v<=stop_num;v++)
    80                 {
    81                     int weight=(type == 'A')?2:0;
    82                     add_edge(No[stop[u]], No[stop[v]], weight);
    83                     add_edge(No[stop[v]], No[stop[u]], weight);
    84                 }
    85             }
    86         }
    87         string st,ed;
    88         cin>>st>>ed;
    89         spfa(No[st]);
    90         printf("Case #%d: %d
    ",kase,d[No[ed]]);
    91     }
    92 }



  • 相关阅读:
    查看unity打来的包在手机上面查看日志
    Unity 打包出来动态加载图片丢失问题
    嵌套列表拖拽事件冲突问题
    游戏中实现鼠标拖尾效果
    2048
    面试知识点积累
    ARM处理器架构理论知识
    計算機網絡知識點總結:
    collection
    demo002.链表操作
  • 原文地址:https://www.cnblogs.com/dilthey/p/6804132.html
Copyright © 2011-2022 走看看