2016江苏省CPC省赛 I - Itinerary Planning
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
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的更多相关文章
- 2016 CCPC长春重现赛
1.2016中国大学生程序设计竞赛(长春)-重现赛 2.总结:会做的太少,应变能力也不行,或者说猜题目的能力不行 02 水 04 HDU 5914 Triangle 1.题意:1~n,n个数,问 ...
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- 2016 ICPC China-Final 现场赛总结
距离比赛结束快有一个礼拜了才抽出时间来写这篇总结.今年比赛打了也有5场了(4场区域赛+1场省赛),也取得了不错的成绩(区域赛两银),总的来说第一年就取得这成绩还是挺高兴的.我们队,我自己都渐渐的趋于成 ...
- 2016 年青岛网络赛---Family View(AC自动机)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5880 Problem Description Steam is a digital distribut ...
- 2016 年沈阳网络赛---QSC and Master(区间DP)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5900 Problem Description Every school has some legend ...
- 2016 年青岛网络赛---Tea
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5881 Problem Description Tea is good. Tea is life. Te ...
- 2016 年青岛网络赛---Sort(k叉哈夫曼)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5884 Problem Description Recently, Bob has just learn ...
- 2016 ICPC北京站现场赛总结(再度流水账)
其他的都先不说,北大的未名湖真的美! 虽然感觉北大其他地方都有些破旧之感,但是未名湖附近真的值得称赞. 在去北京之前就听说北京温度很低,不过是干冷,果不其然.北京不冷,就是嘴唇太干了,不喝水真的受不了 ...
- [刷题]ACM/ICPC 2016北京赛站网络赛 第1题 第3题
第一次玩ACM...有点小紧张小兴奋.这题目好难啊,只是网赛就这么难...只把最简单的两题做出来了. 题目1: 代码: //#define _ACM_ #include<iostream> ...
随机推荐
- Android安装器学习笔记(一)
Android安装器学习笔记(一) 一.Android应用的四种安装方式: 1.通过系统应用PackageInstaller.apk进行安装,安装过程中会让用户确认 2.系统程序安装:在开机的时候自动 ...
- 《倾国倾城》全套源代码:client+服务端+资源,歧视复制帖子
郝萌主倾心贡献,尊重作者的劳动成果,请勿转载. 假设文章对您有所帮助,欢迎给作者捐赠,支持郝萌主,捐赠数额任意.重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源代码下载:点我传送 游戏官方下 ...
- ios开发之--VC的生命周期
当一个视图控制器被创建,并在屏幕上显示的时候. 代码的执行顺序 1. alloc 创建对象,分配空间 2.init (initWit ...
- ASP代码审计学习笔记 -5.文件下载漏洞
文件下载漏洞 漏洞代码: <% function download(f,n) on error resume next Set S=CreateObject("Adodb.Stream ...
- MongoDB 备份恢复
备份: mongodump --host -u admin -p -o /tmp/alldb/ // 备份所有的库 mongodump --host -u admin -p -d mydb -o /t ...
- mac 常用的终端命令
OSX 的文件系统 OSX 采用的Unix文件系统,所有文件都挂在跟目录 / 下面,所以不在要有Windows 下的盘符概念. 你在桌面上看到的硬盘都挂在 /Volumes 下. 比如接上个叫做 US ...
- Studio更新
其实最主要的是下面三个步骤: 1.更新As工程为3.0 2.必须升级gradle到4.0以上 3.buildToolsVersion升级到26.0.0 4.在gradle.properties中配置版 ...
- N76E003之IO控制
N76E003最多支持26个可位寻址的通用I/O引脚,分成4组 P0 到 P3 .每一个端口有它的端口控制寄存器(Px).端口控制寄存器的写和读有不同的意思.写端口控制寄存器设置输出锁存逻辑值,读端口 ...
- 浅析JavaBean
一.概述 JavaBean组件本质上是一个Java类,只是这个类的编码要遵循一些约定.用户可以使用JavaBean将功能.处理.值.数据库访问和其他任何可以用java代码创造的对象进行打包,并且其他的 ...
- 页面调用Iframe中数据
<iframe src="html的路径(至于MVC中cshtml直接路径好像是不行的,得使用action进行请求出来的路径)" id="iframechild&q ...