zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    qt运行时连接signal和slot
    linux使用usb转串口调试ARM开发板
    qwt自定义时间标尺TimeScale
    关于mysql 导入大型数据问题的解决(转载,出处以忘)
    JavaScript replace(RegExp, Function)详解
    有关高度和宽度的对象
    前自增和后自增(chrome js 为了保险,还没测过其它的)
    JavaScript效率PK——统计特定字符在字符串中出现的次数
    getStyle函数
    解决CHM文件在WIN7下崩溃和自动生成CHW文件的问题
  • 原文地址:https://www.cnblogs.com/zmj97/p/6261949.html
Copyright © 2011-2022 走看看