sicily 1031 Campus(图算法)
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(图算法)的更多相关文章
- Sicily 1031: Campus (最短路)
这是一道典型的最短路问题,直接用Dijkstra算法便可求解,主要是需要考虑输入的点是不是在已给出的地图中,具体看代码 #include<bits/stdc++.h> #define MA ...
- OpenFlow:Enabling Innovation in Campus Networks
SDN领域,OpenFLow现在已经成为了广泛使用的南向接口协议.若想好好学习SDN,在这个领域有所进步,需要熟悉OpenFlow协议.我最近找了篇有关OpenFLow的论文,发现最早该协议是在Sig ...
- sicily 中缀表达式转后缀表达式
题目描述 将中缀表达式(infix expression)转换为后缀表达式(postfix expression).假设中缀表达式中的操作数均以单个英文字母表示,且其中只包含左括号'(',右括号‘)’ ...
- BZOJ 1031: [JSOI2007]字符加密Cipher 后缀数组
1031: [JSOI2007]字符加密Cipher Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 6014 Solved: 2503[Submit ...
- sicily 1934. 移动小球
Description 你有一些小球,从左到右依次编号为1,2,3,...,n. 你可以执行两种指令(1或者2).其中, 1 X Y表示把小球X移动到小球Y的左边, 2 X Y表示把小球X移动到小球Y ...
- Light OJ 1031 - Easy Game(区间dp)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1031 题目大意:两个选手,轮流可以从数组的任意一端取值, 每次可以去任意个但仅 ...
- MySQLdb 1031 Error
Python import MySQLdb 有可能报:site-packages/pkg_resources.py:1031: UserWarning: /home/***/.python-eggs ...
- 深度优先搜索 codevs 1031 质数环
codevs 1031 质数环 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 一个大小为N(N<=17)的质数环是 ...
- loj 1031(区间dp+记忆化搜索)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1031 思路:dp[i][j]表示从区间i-j中能取得的最大值,然后就是枚举分割点了. ...
随机推荐
- iOS-UIImage imageWithContentsOfFile 和 imageName 对照
1.imageWithContentsOfFile NSString *imagePath = [NSString stringWithFormat:@"%@/%@",[[NSBu ...
- 中科燕园arcgis外包案例之12---供水供热管线GIS系统
项目背景 绍兴县是浙江省第一个"数字城管"试点城市,也是全国第一个"数字城管"县级城市.随着经济的飞速发展.城市化步伐的加快,以及城市规模的扩大和现代化程度的不 ...
- C++编写绚丽的界面
近期项目特别的操蛋,要用C++写出各种变态界面,今晚上赶工总算有了一点小的收货. 因为没有时间去写博文 ,等项目期完了 准备 写一系列 怎样在C++/win32/mfc开发高质量 可扩展界面组建 ...
- php 设计模式之观察者模式(订阅者模式)
php 设计模式之观察者模式 实例 没用设计模式的代码,这样的代码要是把最上面那部分也要符合要求加进来,就要修改代码,不符合宁增不改的原则 介绍 观察者模式定义对象的一对多依赖,这样一来,当一个对象改 ...
- ES6 | class类的基本语法总结
类和模块的内部,默认就是严格模式,所以不需要使用use strict指定运行模式.只要你的代码写在类或模块之中,就只有严格模式可用. 考虑到未来所有的代码,其实都是运行在模块之中,所以 ES6 实际上 ...
- Kotlin 中文文档
Kotlin 中文文档 标签: Kotlinkotlin中文文档 2017-02-14 18:14 4673人阅读 评论(0) 收藏 举报 分类: kotlin 转载地址:http://www.tu ...
- SQLServer 错误: 15404,维护计划无法执行
错误症状: D:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\LOG下面的ERROELOG,用文本打,查看运行维维计划不成功是生成的错误日志详细信 ...
- SpringBoot学习笔记(11)-----SpringBoot中使用rabbitmq,activemq消息队列和rest服务的调用
1. activemq 首先引入依赖 pom.xml文件 <dependency> <groupId>org.springframework.boot</groupId& ...
- yii2.0 利用Excel类做导入导出
1.在 common 目录下 创建一个 components 将 Classes目录(改名为PHPExcel)和PHPExcel.php 放在新创建的目录下.再在 components 下创建一个Co ...
- 解决css兼容性
关于CSS对各个浏览器兼容已经是老生常谈的问题了, 网络上的教程遍地都是.以下内容没有太多新颖, 纯属个人总结, 希望能对初学者有一定的帮助. 一.CSS HACK 以下两种方法几乎能解决现今所有HA ...