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. IIS------如何占用80端口

    如何占用80端口 请看我的一篇随笔: https://www.cnblogs.com/tianhengblogs/p/9292347.html

  2. [转]spring 官方下载地址(Spring Framework 3.2.x&Spring Framework 4.0.x)

    SPRING官方网站改版后,建议都是通过 Maven和Gradle下载,对不使用Maven和Gradle开发项目的,下载就非常麻烦,下给出Spring Framework jar官方直接下载路径: h ...

  3. 处理特殊格式的GET传参

    有群友问 这样的传参格式如何接受获取 xx.php?con="one"=>5,"two"=>0,"three"=>1 那么 ...

  4. mysql报错“Starting MySQL...The server quit without updating PID file”处理

    http://blog.csdn.net/lzq123_1/article/details/51354179 注意:要将/usr/bin/mysql_install_db替换成 /usr/bin/my ...

  5. 数据库iops的理解

    想购买阿里云的RDS mysql,想请教下最大连接数是请求数吗?如下图,600最大可支持连接数,那一个页面查询30次,20个人同时请求,数据库不就超载了么?(内存2400MB,专用数据服务器,只能支持 ...

  6. 【代码审计】JTBC(CMS)_PHP_v3.0 任意文件删除漏洞分析

      0x00 环境准备 JTBC(CMS)官网:http://www.jtbc.cn 网站源码版本:JTBC_CMS_PHP(3.0) 企业版 程序源码下载:http://download.jtbc. ...

  7. 个人成长|荣获CNVD年度最有价值漏洞奖

    本文共750+字,预计阅读2-3分钟. 前几天,很荣幸受主办方邀请,还拿了CNVD的一个“年度最有价值漏洞奖”,说一说,这几天的故事吧. 11月20号,意外收到一个会议邀请,当时还比较诧异,印象中我在 ...

  8. Linux 下 c 语言 聊天软件

    这是我学C语言写的第一个软件,是一个完整的聊天软件,里面包括客户端,和服务器端,可以互现聊天,共享文件,有聊天室等,是一个有TCP和UDP协议的聊天软件,测试过很多次在CENTOS和UBUNTU下都通 ...

  9. 安装ubuntu后,你的屏幕尺寸太小,无法设置,该怎么解决

    安装完虚拟机之后,你的Ubuntu可能会在尺寸很小,(这种情况可能有,可能没有) 想要点击设置,选中Display里的分辨率下拉框,但是却因为这个窗口太大,无法点击apply按钮.悲剧啦!!! Ctr ...

  10. 通过java的i/o机制进行图片流的存储以及对网络图片的存储

    存储内地图片思路:首先把原有的图片以流的方式读取出来,再以流的方式存储到目标文件: package imgStream; import java.io.*; public class ImgStrea ...