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的应用,邻接矩阵开不出来,使用邻接表。

 #include<cstdio>
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
#define MAX 10005
#define INF 0x3f3f3f3f
struct Edge{
int from,to,weight;
Edge(int u,int v,int w):from(u),to(v),weight(w){};
};
vector<Edge> E;
vector<int> G[MAX];
void add_edge(int u,int v,int w)
{
E.push_back(Edge(u,v,w));
G[u].push_back(E.size()-);
}
map<string,int> No;
void init()
{
E.clear();
No.clear();
for (int i = ; i<MAX; i++) G[i].clear();
}
bool vis[MAX];
int d[MAX];
void spfa(int st)
{
memset(d,INF,sizeof(d));
memset(vis,false,sizeof(vis));
queue<int> q;
q.push(st);
d[st]=;
vis[st]=;
while (!q.empty())
{
int u=q.front();
q.pop();
vis[u]=;
for(int i=;i<G[u].size();i++)
{
Edge &e=E[G[u][i]];
int tmp=d[e.to];
if(d[e.to]>d[e.from]+e.weight) d[e.to]=d[e.from]+e.weight;
if(d[e.to]<tmp && !vis[e.to])
{
q.push(e.to);
vis[e.to]=;
}
}
}
} int main()
{
int n,t;
scanf("%d",&t);
for(int kase=;kase<=t;kase++)
{
init();
scanf("%d",&n);
int cnt=;
for(int i=;i<=n;i++)
{
char type;
int stop_num;
string stop[];
cin>>type>>stop_num;
for(int j=;j<=stop_num;j++)
{
cin>>stop[j];
if(No.count(stop[j])==) No[stop[j]] = cnt++;
}
for(int u=;u<=stop_num;u++)
{
for(int v=u+;v<=stop_num;v++)
{
int weight=(type == 'A')?:;
add_edge(No[stop[u]], No[stop[v]], weight);
add_edge(No[stop[v]], No[stop[u]], weight);
}
}
}
string st,ed;
cin>>st>>ed;
spfa(No[st]);
printf("Case #%d: %d\n",kase,d[No[ed]]);
}
}

2016江苏省CPC省赛 I - Itinerary Planning的更多相关文章

  1. 2016 CCPC长春重现赛

    1.2016中国大学生程序设计竞赛(长春)-重现赛 2.总结:会做的太少,应变能力也不行,或者说猜题目的能力不行 02  水 04  HDU 5914  Triangle 1.题意:1~n,n个数,问 ...

  2. 2016 华南师大ACM校赛 SCNUCPC 非官方题解

    我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...

  3. 2016 ICPC China-Final 现场赛总结

    距离比赛结束快有一个礼拜了才抽出时间来写这篇总结.今年比赛打了也有5场了(4场区域赛+1场省赛),也取得了不错的成绩(区域赛两银),总的来说第一年就取得这成绩还是挺高兴的.我们队,我自己都渐渐的趋于成 ...

  4. 2016 年青岛网络赛---Family View(AC自动机)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5880 Problem Description Steam is a digital distribut ...

  5. 2016 年沈阳网络赛---QSC and Master(区间DP)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5900 Problem Description Every school has some legend ...

  6. 2016 年青岛网络赛---Tea

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5881 Problem Description Tea is good. Tea is life. Te ...

  7. 2016 年青岛网络赛---Sort(k叉哈夫曼)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5884 Problem Description Recently, Bob has just learn ...

  8. 2016 ICPC北京站现场赛总结(再度流水账)

    其他的都先不说,北大的未名湖真的美! 虽然感觉北大其他地方都有些破旧之感,但是未名湖附近真的值得称赞. 在去北京之前就听说北京温度很低,不过是干冷,果不其然.北京不冷,就是嘴唇太干了,不喝水真的受不了 ...

  9. [刷题]ACM/ICPC 2016北京赛站网络赛 第1题 第3题

    第一次玩ACM...有点小紧张小兴奋.这题目好难啊,只是网赛就这么难...只把最简单的两题做出来了. 题目1: 代码: //#define _ACM_ #include<iostream> ...

随机推荐

  1. Smallest Difference(暴力全排列)

    Smallest Difference Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10387   Accepted: 2 ...

  2. SpringMVC由浅入深day02_9RESTful支持

    9 RESTful支持 9.1 什么是RESTful RESTful架构,就是目前最流行的一种互联网软件架构.它结构清晰.符合标准.易于理解.扩展方便,所以正得到越来越多网站的采用. RESTful( ...

  3. ios开发之--比较两个数组里面的值是否相同

    比较两个数组里面的内容是否相同,代码如下: NSArray *array1 = [NSArray arrayWithObjects:@"a", @"b", @& ...

  4. 利用Visio绘制数据流图与组织结构图

    绘制数据流图: 利用Visio 2007来绘制网上书店系统的数据流图.利用Visio 2007创建Gane- Sarson数据流图,可以选择“软件和数据库”模板,然后再选择“数据流模型图”,创建之后可 ...

  5. c 网络字节序和本机字节序转换

    将多字节整数类型的数据,从主机的字节顺序转化为网络字节顺序 #include <netinet/in.h> uint32_t htonl(uint32_t hostlong);uint16 ...

  6. An internal error occurred during: "Launching xxx on WebLogic10.x".

    An internal error occurred during: "Launching xxx on WebLogic10.x". java.lang.NullPointerE ...

  7. SpringBoot(十)-- 整合MyBatis

    1.pom.xml 配置maven依赖 <dependency> <groupId>org.mybatis.spring.boot</groupId> <ar ...

  8. SaltStack 批量执行脚本

    这里演示如何使用 salt-master 对多台 salt-minion 批量执行脚本,步骤如下: [root@localhost ~]$ cat /srv/salt/top.sls # 先定义入口配 ...

  9. Selenium 获取节点信息

    Selenium 可以通过 find_element() 找到指定的节点,Selenium 也提供了相关的方法和属性来直接提取节点信息,如属性.文本等 from selenium import web ...

  10. 初步总结javascript中学习DOM之前的知识

    嘿嘿,又到了周末时间,周六其实就是总结这周的学习的,记得周二周三刚开始接触javascript时间,还是不知道怎么学习的,就感觉找不到方向,那时间学习的只是总结了一些简单的定义或者是学习结构,今天就把 ...