2016江苏省CPC省赛 I
来源:互联网 发布:网络运维管理规范 编辑:程序博客网 时间:2024/04/19 12:53
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
13A 5 NJU ZSL XJK YT ATZXB 3 XJK HSDWY MGQA 3 HSDWY NJZ MGQNJU 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 0x3f3f3f3fstruct 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()-1);}map<string,int> No;void init(){E.clear();No.clear();for (int i = 0; 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]=0;vis[st]=1;while (!q.empty()){int u=q.front(); q.pop(); vis[u]=0;for(int i=0;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]=1;}}}}int main(){int n,t;scanf("%d",&t);for(int kase=1;kase<=t;kase++){init();scanf("%d",&n);int cnt=1;for(int i=1;i<=n;i++){char type;int stop_num;string stop[12];cin>>type>>stop_num;for(int j=1;j<=stop_num;j++){cin>>stop[j];if(No.count(stop[j])==0) No[stop[j]] = cnt++;}for(int u=1;u<=stop_num;u++){for(int v=u+1;v<=stop_num;v++){int weight=(type == 'A')?2:0;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
- 2016江苏省大学生程序设计大赛 JSCPC 总结
- 江苏省电子地图[江苏音符]
- 行车助理(江苏省) 免费
- 江苏省昆山市软件公司汇总
- 江苏省计算机三级知识结构
- 江苏省高级职称评定资料
- 2017江苏省赛总结
- 2017江苏省赛
- 【网摘】CPC CPA CPS
- 名词解释CPC、CPM、CPA...
- CPC功率控制器
- CPC、CPM、CPA、CPS
- cpc,a wonderful concert
- CPC广告反作弊
- 什么是cpc cpm cvr
- CPC广告业务架构总结
- 扫盲cpc和cpt
- 面试题39_2:平衡二叉树
- 由于该设备有问题,Windows 已将其停止。 (代码 43)
- DTW(Dynamic Time Warping,动态时间归整)
- Node.js使用MongoDB3.4+Access control is not enabled for the database解决方案
- 如何写好C++类
- 2016江苏省CPC省赛 I
- 如何判断终端打开的设备
- Unity3D 官方文档 NavMesh三个组件的翻译与解释 自动寻路需要知道的细节
- 编程心得
- 面试题58:二叉树中的下一个结点
- Unity XUPorter overwriteBuildSetting相关的坑
- javaScript基础---(一):如何在网页中实现javaScript脚本语言
- py3 下安装bs4的方法
- 面试题59:对称的二叉树