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中能取得的最大值,然后就是枚举分割点了. ...
随机推荐
- vijos-1382 寻找主人
题意: 给出两个同样长度的数字串: 求两个串是否本质同样.同样则输出最小表示. 长度L似乎给的不正确,大概是2000000左右吧: 题解: 最小表示法裸题.证明正确性啥的详见论文吧: 这东西大体的思路 ...
- php利用href进行页面传值的正确姿势
首先在a.php中 <?php $a = "world"; echo "<a href='b.php?m=$a'>删除</a>"; ...
- 计算sigma
1.计算平均值Avg Avg = (a0 + a1 + ......+ an-1) / n 2.计算sigma sigma = sqrt( ( (a0-avg) ^2 + (a1-avg) ^2 ...
- JS的数据类型(包含:7种数据类型的介绍、数据类型的转换、数据类型的判断)
前言 最新的 ECMAScript 标准定义了JS的 7 种数据类型,其中包括: 6 种基本类型:Boolean.Null.Undefined.Number.String.Symbol (ECMASc ...
- lftp简单使用
连接服务器: lftp -e "参数;" "username":"password"@"ip" -p port lftp ...
- vue项目,封装api并使用
封装api index.js let uploadBase = '' if(process.env.NODE_ENV === 'production'){ uploadBase = 'https:// ...
- JavaScript设计模式(biaoyansu)(2)
单例模式实例 (创建类模式): let elBalance = document.getElementById('balance') function init () { var a = new Di ...
- [arc082e]ConvexScore
题意: 给出直角坐标系中的$N$个点$(X_i,Y_i)$,定义由其中部分点构成的点集为“凸点集”当且仅当这些点恰好能构成一个凸多边形(内部没有其他点). 如图,点集$\{A,C,E\}$和$\{B, ...
- Docker yum 安装
[liwm@Eren ~]$ sudo su[root@Eren liwm]# yum install -y docker 已加载插件:fastestmirror, langpacks, prod ...
- WPF 一个空的 WPF 程序有多少个窗口
原文:WPF 一个空的 WPF 程序有多少个窗口 好多小伙伴说 WPF 的程序有五个窗口,但是我尝试使用了 EnumThreadWindows 去获取的时候居然拿到了 10 多个窗口 在 WPF 内部 ...