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. sap abap 对字符串的操作

    替换字段内容 REPLACE [FIRST /ALL OCCURRENCES OF]<STR1>INTO <STR> WITH <STR2>   DATA STR ...

  2. STM32IAP升级-----编写IAP升级遇到的问题总结

    IAP的源代码等资料我上传了,压缩包内有12个文件.,http://download.csdn.net/detail/f907279313/7524849(要积分的辛苦收集的你们就给点积分吧) 还有还 ...

  3. vue 路由demo

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. node--20 moogose demo2

    db.js /** * Created by Danny on 2015/9/28 16:44. */ //引包 var mongoose = require('mongoose'); //创建数据库 ...

  5. Recovering unassigned shards on elasticsearch 2.x——副本shard可以设置replica为0在设置回来

    Recovering unassigned shards on elasticsearch 2.x 摘自:https://z0z0.me/recovering-unassigned-shards-on ...

  6. Linux常用命令之rpm安装命令

    转自:http://www.cnblogs.com/datasyman/p/6942557.html 在 Linux 操作系统下,几乎所有的软件均通过RPM 进行安装.卸载及管理等操作.RPM 的全称 ...

  7. 90.bower解决js的依赖管理

    转自:https://blog.csdn.net/u011537073/article/details/52951122 前言一个新的web项目开始,我们总是很自然地去下载需要用到的js类库文件,比如 ...

  8. xBIM 基础03 基本模型操作

    系列目录    [已更新最新开发文章,点击查看详细]  本篇将使用基本的代码示例来表示如何使用xBIM.我们将介绍持久存储的四个基本功能,即 CRUD(创建,检索,更新和删除).以下示例通常适用于IF ...

  9. vue keep-alive保存路由状态2 (高级用法,接上篇)

    接上篇 https://www.cnblogs.com/wangmaoling/p/9803960.html 本文很长,请耐心看完分析. 4.高级用法,指定从什么组件进入才缓存,以及销毁缓存:先介绍我 ...

  10. Windows窗体应用布局详解

    上回我们已经会用基本的控件创建Windows窗体应用,这才我们再来认识一些高级控件并使用ADO.NET技术连接数据库来创建功能更坚强大的窗体应用! 菜单栏控件MenuStrip .NET中提供了一个M ...