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 = 1; i <= n; i++) {
        cities[i] = "";
        for (int j = 1; j <= n; j++) roadLength[i][j] = INF;
        roadLength[i][i] = 0;
        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 = 1; i <= cityCount; i++) {
        if (cities[i] == x) return i;
    }
    return -1;
}

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 == -1) {
        cities[++cityCount] = x;
        xpos = cityCount;
    }
}

int dijkstra(int startCityPos, int endCityPos, int n) {    // n is cityCount
    len[startCityPos] = 0;
    for (int i = 1; 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 = 1; 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 = 1; 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 -1;
}

int main() {
    int t;
    cin>>t;
    while(t--) {
        int n;
        cin>>n;
        initial(n*2);
        int cityCount = 0;
        for (int i = 0; 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<<0<<endl;
        else if (startCityPos == -1 || endCityPos == -1) cout<<-1<<endl;
        else cout<<dijkstra(startCityPos, endCityPos, cityCount)<<endl;
    }
    return 0;
}
时间: 2025-01-14 10:29:57

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

Sicily 1031. Campus 解题报告

1031_Campus 题目链接: http://soj.me/1031 题目大意: 给出四个校区的一些地点之间的距离,地点名用字符串来表示,问某两个地点之间的最短路径长度,典型的单源最短路径题目 思路: 单源最短路径问题可以用dijkstra算法实现,这道题比较麻烦的是用字符串来表示地点,我用的处理方法是建立map得到地点名字到序号的映射,对于每个新输入的地点名字,先在map里面查找是否存在,如果不存在就绑定一个新的序号.地点之间的距离用邻接矩阵来存放. 代码: #include <iostr

编程题目分类(剪辑)

1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 11. 模拟 12. 算术与代数 13. 组合问题 14. 数论 15. 网格,几何,计算几何 [编程入门] PC 110101, uva 100, The 3n+1 problem, 难度 1 PC 110102, uva 10189, Minesweeper, 难度 1 PC 110103, uva 10137, The T

(转)sicily题目分类

Sicily题目分类 ·         [数据结构/图论] 1310 Right-Heavy Tree   笛卡尔树相关,复杂度O(N)或O(NlogN). ·1426 Phone List         电话号码前缀检索,trie树相关. ·1443 Printer Queue      基本队列操作. ·1149 等价表达式         判断表达式是否等价(递归求解) ·1136 山海经             n长序列里求m次区间询问的最大连续子区间和.线段树/RMQ ·1252

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

1031: [JSOI2007]字符加密Cipher Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 6014  Solved: 2503[Submit][Status][Discuss] Description 喜欢钻研问题的JS同学,最近又迷上了对加密方法的思考.一天,他突然想出了一种他认为是终极的加密办法 :把需要加密的信息排成一圈,显然,它们有很多种不同的读法.例如下图,可以读作: JSOI07 SOI07J OI07JS I07JSO 0

深度优先搜索 codevs 1031 质数环

codevs 1031 质数环 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 一个大小为N(N<=17)的质数环是由1到N共N个自然数组成的一个数环,数环上每两个相邻的数字之和为质数.如下图是一个大小为6的质数环.为了方便描述,规定数环上的第一个数字总是1.如下图可用1 4 3 2 5 6来描述.若两个质数环,数字排列顺序相同则视为本质相同.现在要求你求出所有本质不同的数环. 输入描述 Input Description 只有

1095. Cars on Campus (30)

Zhejiang University has 6 campuses and a lot of gates. From each gate we can collect the in/out times and the plate numbers of the cars crossing the gate. Now with all the information available, you are supposed to tell, at any specific time point, t

Currency campus line of digital red song celebrates victory 70 years

Currency campus line of digital red song celebrates 70 years of war the Red Army's heritage light, played youth movement! August 15, 2015 is the 70th anniversary of the anniversary of the war, as a contemporary college students, we should bear in min

【BZOJ】【1031】【JSOI2007】字符加密Cipher

后缀数组 当年感觉好神的题现在好像变水了…… 题意其实有点蛋疼……一开始没看懂<_< 将原串复制一遍接在后面,用后缀数组求一下SA,那么SA<n的就是所找到的那n个字符串,然后把它们的第n个字符抠出来就可以了…… 1 /************************************************************** 2 Problem: 1031 3 User: Tunix 4 Language: C++ 5 Result: Accepted 6 Time:7

1031 质数环(深搜练习)

题目网站:http://codevs.cn/problem/1031/ 题目描述 Description 一个大小为N(N<=17)的质数环是由1到N共N个自然数组成的一个数环,数环上每两个相邻的数字之和为质数.如下图是一个大小为6的质数环.为了方便描述,规定数环上的第一个数字总是1.如下图可用1 4 3 2 5 6来描述.若两个质数环,数字排列顺序相同则视为本质相同.现在要求你求出所有本质不同的数环. 输入描述 Input Description 只有一个数N,表示需求的质数环的大小.如: 输