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中能取得的最大值,然后就是枚举分割点了. ...
随机推荐
- apache rewrite 正則表達式基础
用了好几次rewrite,用的次数不是非常多,每次都忘,都得又一次上网上找一堆现看,总结一下,以免以后忘了 =====================分隔符===================== ...
- cocos2d-x ios游戏开发初认识(九) 音效、粒子系统与存储
我们知道.一个游戏少不了声音.一些好听的声音会提起你对游戏的兴趣,当然做好听的声音不是我们要学的,我们的目的是把声音在适当的时候放出来.顺便在这节中会说下简单的粒子系统和文件存储. 一.声音的播放: ...
- 跟我学设计模式视频教程——适配器模式,适配器模式VS装饰模式
课程视频 适配器模式 适配器模式VS装饰模式 唠嗑 课程笔记 课程笔记 课程代码 课程代码 新课程火热报名中 课程介绍
- bzoj2734【HNOI2012】集合选数
2734: [HNOI2012]集合选数 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 831 Solved: 487 [Submit][Stat ...
- scanf使用与运算符
scanf接收输入 #include <stdio.h> #include <stdlib.h> // 接收用户输入的小写字母,输出大写字母 int main() { char ...
- 关于webuploader跨域解决方法
1.在iis处理程序映射 2.后台ashx处理添加如下代码
- xBIM 高级03 更改日志创建
系列目录 [已更新最新开发文章,点击查看详细] 模型中发生的每一个变化都是事务的一部分,这是我们设计的核心.所有事务都是由 IModel 的实现创建的,并且从中被弱引用,因此当使用 using ...
- 循环访问 TreeView 控件的所有节点
创建测试每个节点的递归过程 . private void PrintRecursive(TreeNode treeNode) { // Print the node. System.Diagnosti ...
- Enable .Net 4.5 in IIS on Windows 8.1
Setting up a new development box for myself I had forgotten all about the necessity to use theaspnet ...
- hiho 171周 - 水题,并查集
题目链接 题目描述: 输入4 alice 2 alice@hihocoder.com alice@gmail.com bob 1 bob@qq.com alicebest 2 alice@gmail. ...