Description

At present, Zhongshan University has 4 campuses with a total area of 6.17 square kilometers sitting respectively on both sides of the Pearl River or facing the South China Sea. The Guangzhou South Campus covers an area of 1.17 square kilometers, the North Campus covers an area of 0.39 square kilometers, the Guangzhou East Campus has an area of 1.13 square kilometers and the Zhuhai Campus covers an area of 3.48 square kilometers. All campuses have exuberance of green trees, abundance of lawns and beautiful sceneries, and are ideal for molding the temperaments, studying and doing research.

       Sometime, the professors and students have to go from one place to another place in one campus or between campuses. They want to find the shortest path between their source place S and target place T. Can you help them?

Input

The first line of the input is a positive integer C. C is the number of test cases followed. In each test case, the first line is a positive integer N (0<N<=100) that represents the number of roads. After that, N lines follow. The i-th(1<=i<=N) line contains two strings Si, Ti and one integer Di (0<=Di<=100). It means that there is a road whose length is Di between Si and Ti. Finally, there are two strings S and T, you have to find the shortest path between S and T. S, T, Si(1<=i<=N) and Ti(1<=i<=N) are all given in the following format: str_Campus.str_Place. str_Campus represents the name of the campus, and str_Place represents the place in str_Campus. str_Campus is "North", "South", "East" or "Zhuhai". str_Place is a string which has less than one hundred lowercase characters from "a-z". You can assume that there is at most one road directly between any two places.

Output

The output of the program should consist of C lines, one line for each test case. For each test case, the output is a single line containing one integer. If there is a path between S and T, output the length of the shortest path between them. Otherwise just output "-1" (without quotation mark). No redundant spaces are needed.

Sample Input

1
2
South.xiaolitang South.xiongdelong 2
South.xiongdelong Zhuhai.liyuan 100
South.xiongdelong South.xiaolitang

Sample Output

2
使用dijkstra算法,算法思路可以看https://www.youtube.com/watch?v=gdmfOwyQlcI

因为dijkstra第三个参数传错debug了好久,以后要注意细节。

看到别人用了map来计算新城市,我是直接暴力查找添加的。

以下是代码:

#include <iostream>
#include <string>
using namespace std; #define INF 1000000
#define MAX 210
int roadLength[MAX][MAX];
string cities[MAX];
bool visited[MAX];
int len[MAX]; void initial(int n) { // initial all arrays
for (int i = ; i <= n; i++) {
cities[i] = "";
for (int j = ; j <= n; j++) roadLength[i][j] = INF;
roadLength[i][i] = ;
visited[i] = false;
len[i] = INF;
}
} int cityPos(string x, int cityCount) { // return city pos in array cities, if not exist, return -1
for (int i = ; i <= cityCount; i++) {
if (cities[i] == x) return i;
}
return -;
} void addCity(string x, int &xpos, int &cityCount) { // if the city not exist in the array, add it; xpos store the pos of the city
xpos = cityPos(x, cityCount);
if (xpos == -) {
cities[++cityCount] = x;
xpos = cityCount;
}
} int dijkstra(int startCityPos, int endCityPos, int n) { // n is cityCount
len[startCityPos] = ;
for (int i = ; i <= n; i++) {
// currentVisitPos is the pos of the city which is not visited and has the shortest len
int currentVisitPos = startCityPos;
int minLen = INF;
for (int j = ; j <= n; j++) {
if (!visited[j] && len[j] < minLen) {
minLen = len[j];
currentVisitPos = j;
}
}
visited[currentVisitPos] = true;
// update the lens of unvisited cities
for (int j = ; j <= n; j++) {
if (!visited[j] && len[currentVisitPos] + roadLength[currentVisitPos][j] < len[j]) {
len[j] = len[currentVisitPos] + roadLength[currentVisitPos][j];
}
}
}
if (visited[endCityPos]) return len[endCityPos];
return -;
} int main() {
int t;
cin>>t;
while(t--) {
int n;
cin>>n;
initial(n*);
int cityCount = ;
for (int i = ; i < n; i++) {
string x, y;
int length, xpos, ypos;
cin>>x>>y>>length;
addCity(x, xpos, cityCount);
addCity(y, ypos, cityCount);
roadLength[xpos][ypos] = roadLength[ypos][xpos] = length;
}
string startCity, endCity;
cin>>startCity>>endCity;
int startCityPos = cityPos(startCity, cityCount), endCityPos = cityPos(endCity, cityCount);
if (startCity == endCity) cout<<<<endl;
else if (startCityPos == - || endCityPos == -) cout<<-<<endl;
else cout<<dijkstra(startCityPos, endCityPos, cityCount)<<endl;
}
return ;
}

sicily 1031 Campus(图算法)的更多相关文章

  1. Sicily 1031: Campus (最短路)

    这是一道典型的最短路问题,直接用Dijkstra算法便可求解,主要是需要考虑输入的点是不是在已给出的地图中,具体看代码 #include<bits/stdc++.h> #define MA ...

  2. OpenFlow:Enabling Innovation in Campus Networks

    SDN领域,OpenFLow现在已经成为了广泛使用的南向接口协议.若想好好学习SDN,在这个领域有所进步,需要熟悉OpenFlow协议.我最近找了篇有关OpenFLow的论文,发现最早该协议是在Sig ...

  3. sicily 中缀表达式转后缀表达式

    题目描述 将中缀表达式(infix expression)转换为后缀表达式(postfix expression).假设中缀表达式中的操作数均以单个英文字母表示,且其中只包含左括号'(',右括号‘)’ ...

  4. BZOJ 1031: [JSOI2007]字符加密Cipher 后缀数组

    1031: [JSOI2007]字符加密Cipher Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 6014  Solved: 2503[Submit ...

  5. sicily 1934. 移动小球

    Description 你有一些小球,从左到右依次编号为1,2,3,...,n. 你可以执行两种指令(1或者2).其中, 1 X Y表示把小球X移动到小球Y的左边, 2 X Y表示把小球X移动到小球Y ...

  6. Light OJ 1031 - Easy Game(区间dp)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1031 题目大意:两个选手,轮流可以从数组的任意一端取值, 每次可以去任意个但仅 ...

  7. MySQLdb 1031 Error

    Python import MySQLdb 有可能报:site-packages/pkg_resources.py:1031: UserWarning: /home/***/.python-eggs ...

  8. 深度优先搜索 codevs 1031 质数环

    codevs 1031 质数环  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold   题目描述 Description 一个大小为N(N<=17)的质数环是 ...

  9. loj 1031(区间dp+记忆化搜索)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1031 思路:dp[i][j]表示从区间i-j中能取得的最大值,然后就是枚举分割点了. ...

随机推荐

  1. HH实习(hpu1287)(斐波那契运用)

    HH实习 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 44  Solved: 29 [Submit][id=1287">Status ...

  2. ValidForm的使用

    1.引入css 请查看下载文件中的style.css,把里面Validform必须部分拷贝到你的css中(文件中这个凝视 "/*==========下面部分是Validform必须的==== ...

  3. WAP 图片 lazyload

    原理是根据屏幕上的坐标找到需要做 lazyload 的区域 1,先监听 scroll 事件 ,scrolling_lt window.addEventListener('scroll', functi ...

  4. activity生命周期的onPause和onStop

    搞了这么长时间的android开发,却对一些基础的东西一直模棱两可...就比方这个onPause和onStop. 假设从一个界面,跳到还有一个界面,那么是调用哪个呢? 经过我的实验.搞清楚了.onPa ...

  5. ubuntu 下的文件校验(md5、sha256)

    在本地使用 md5sum/sha256sum 生成某待测文件的 hash 值,以跟标准文件的 hash 值做对比验证,确定经网络传输过程得到的文件是否真实无损.一般而言,hash 值如果一致,大概率上 ...

  6. CxImage学习

    官方下载地址是:http://www.xdp.it/cximage/ 打开工程后可以看到下例这些工程: - CxImage - CxImageCrtDll - CxImageMfcDll - dome ...

  7. Linux就该这么学 20181003(第四章Vim/shell/测试条件)

    参考链接https://www.linuxprobe.com/ vim文本编辑器 命令模式:控制光标移动,可对文本进行复制,黏贴,删除和查找工作 输入模式:正常的文本录入 末行模式:保存或退出文档,以 ...

  8. [jzoj NOIP2018模拟10.29]

    OI生涯的最高分,来了纪中这么多天,在经历了这么多场“NOIP难度”的模拟赛之后,终于看到了真正的NOIP 今天考场上效率很高,很快码完了全部的题目,留下了足够的时间对拍和...发呆.不得不说看着电脑 ...

  9. vue,elementUI切换主题,自定义主题

    本文介绍两种elementUI切换主题色的方法 项目示例:http://test.ofoyou.com/theme/ git代码:记得star哦,谢谢 1:官方提供的方法,直接修改scss文件达到修改 ...

  10. Combobox下拉框两级联动

    下拉框的两级联动是我们开发中经常遇到一种情况.比如一个学生管理系统中,根据年级.科目及姓名查询学生考试成绩,年级和科目都是硬盘中的有限数据(数据库)而学生则可以有用户手动指定,这时在数据库中有年级和科 ...