Sicily---1031. Campus(最短路徑)

:搬運自我的csdn博客 http://blog.csdn.net/qq_30172585

題目如下:

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.

分析:
A. 就是單源最短路徑問題丰滑,這個很明顯,可以選用Dijkstra算法求解
B. 數(shù)據(jù)規(guī)模0< N <=100,最多只有100*2 = 200 個頂點,數(shù)據(jù)量較小框咙,可以用較為簡單的鄰接矩陣
C. 輸入中給出的頂點是字符串绘盟,需要轉(zhuǎn)化為對應(yīng)的數(shù)字才能用鄰接矩陣,此時可以用map< string,int >進行映射
D. 此題有坑——最后讓你求的那兩點可能與其他點都無路徑相同谦去,具體參看代碼輸出部分的判斷語句

另附幾個鏈接:
Dijkstra算法講解
單源最短路徑的動態(tài)演示

代碼如下

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<string>
#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;

#define INF 0xfffff
const int SIZE = 210;

/*說明: 圖中頂點集合V分成兩組憎蛤,第一組為已求出最短路徑的頂點集合外傅,簡稱“最短頂點集”
第二組為其余未確定最短路徑的頂點集合 ,簡稱“未確定頂點集”
*/
int graph[SIZE][SIZE];//圖的鄰接矩陣 
int visited[SIZE];//visited[i] == 1表示頂點 i 已經(jīng)加入最短頂點集
int min_length[SIZE];//min_length[i]儲存的是當前源點到頂點i的最短長度 

int Dijkstra(int n, int source, int end)//n為頂點的總數(shù) 
{
    int now = source;//當前新加入最短頂點集的頂點俩檬,初始點為源點 

    int mid = 0, m_min = INF;/*m_min以及mid用于查找未確定頂點集中min_length值最小的頂點萎胰,
                             即下一個要加入最短頂點集的頂點 */

    int cnt = 0;
    while (cnt < n - 1)//將剩下的n-1個頂點都加入最短頂點集 
    {
        m_min = INF;
        for (int i = 1; i <= n; i++)
        {
            //對頂點now的鄰接點進行更新 
            if (!visited[i] && min_length[now] + graph[now][i] < min_length[i])
            {
                min_length[i] = min_length[now] + graph[now][i];
            }
            //查找未確定頂點集中min_length值最小的頂點
            if (!visited[i] && m_min > min_length[i])
            {
                m_min = min_length[i];
                mid = i;
            }
        }
        now = mid;
        visited[mid] = 1;//min_length值最小的頂點加入最短頂點集 

        cnt++;
    }
    return min_length[end];
}

//初始化操作 
void initial()
{
    memset(visited, 0, sizeof(visited));

    for (int i = 0; i < SIZE; i++)
    {
        min_length[i] = INF;
        for (int j = 0; j < SIZE; j++)
        {
            graph[i][j] = INF;
            graph[j][i] = INF;
        }
    }
}



int main()
{
    int cases, n, len;
    string strA, strB;

    cin >> cases;
    while (cases--)
    {
        map<string, int> s2i;//s2i for "string to int"
        initial();
        int id = 0;
        cin >> n;
        while (n--)
        {
            int id_A = 0, id_B = 0;
            cin >> strA >> strB >> len;
            if (!s2i[strA])             //如果strA不在s2i里面
                id_A = s2i[strA] = ++id;
            else
                id_A = s2i[strA];
            if (!s2i[strB])             //如果strB不在s2i里面
                id_B = s2i[strB] = ++id;
            else
                id_B = s2i[strB];

            graph[id_A][id_B] = len;    //修改AB之間的長度
            graph[id_B][id_A] = len;
        }

        cin >> strA >> strB;
        int source = s2i[strA];
        int end = s2i[strB];
        visited[source] = 1;
        min_length[source] = 0;

        int shortest_len = Dijkstra(id, source, end);
        if (strA == strB)
            cout << 0 << endl;
        else if (shortest_len == INF || !s2i[strA] || !s2i[strB])
            cout << -1 << endl;
        else
            cout << shortest_len << endl;

    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市棚辽,隨后出現(xiàn)的幾起案子技竟,更是在濱河造成了極大的恐慌,老刑警劉巖屈藐,帶你破解...
    沈念sama閱讀 212,599評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件榔组,死亡現(xiàn)場離奇詭異熙尉,居然都是意外死亡,警方通過查閱死者的電腦和手機搓扯,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,629評論 3 385
  • 文/潘曉璐 我一進店門检痰,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人锨推,你說我怎么就攤上這事铅歼。” “怎么了爱态?”我有些...
    開封第一講書人閱讀 158,084評論 0 348
  • 文/不壞的土叔 我叫張陵谭贪,是天一觀的道長。 經(jīng)常有香客問我锦担,道長,這世上最難降的妖魔是什么慨削? 我笑而不...
    開封第一講書人閱讀 56,708評論 1 284
  • 正文 為了忘掉前任洞渔,我火速辦了婚禮,結(jié)果婚禮上缚态,老公的妹妹穿的比我還像新娘磁椒。我一直安慰自己,他們只是感情好玫芦,可當我...
    茶點故事閱讀 65,813評論 6 386
  • 文/花漫 我一把揭開白布浆熔。 她就那樣靜靜地躺著,像睡著了一般桥帆。 火紅的嫁衣襯著肌膚如雪医增。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 50,021評論 1 291
  • 那天老虫,我揣著相機與錄音叶骨,去河邊找鬼。 笑死祈匙,一個胖子當著我的面吹牛忽刽,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播夺欲,決...
    沈念sama閱讀 39,120評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼跪帝,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了些阅?” 一聲冷哼從身側(cè)響起伞剑,我...
    開封第一講書人閱讀 37,866評論 0 268
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎扑眉,沒想到半個月后纸泄,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體赖钞,經(jīng)...
    沈念sama閱讀 44,308評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,633評論 2 327
  • 正文 我和宋清朗相戀三年聘裁,在試婚紗的時候發(fā)現(xiàn)自己被綠了雪营。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,768評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡衡便,死狀恐怖献起,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情镣陕,我是刑警寧澤谴餐,帶...
    沈念sama閱讀 34,461評論 4 333
  • 正文 年R本政府宣布,位于F島的核電站呆抑,受9級特大地震影響岂嗓,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜鹊碍,卻給世界環(huán)境...
    茶點故事閱讀 40,094評論 3 317
  • 文/蒙蒙 一厌殉、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧侈咕,春花似錦公罕、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,850評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至熊尉,卻和暖如春罐柳,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背帽揪。 一陣腳步聲響...
    開封第一講書人閱讀 32,082評論 1 267
  • 我被黑心中介騙來泰國打工硝清, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人转晰。 一個月前我還...
    沈念sama閱讀 46,571評論 2 362
  • 正文 我出身青樓芦拿,卻偏偏與公主長得像,于是被迫代替她去往敵國和親查邢。 傳聞我的和親對象是個殘疾皇子蔗崎,可洞房花燭夜當晚...
    茶點故事閱讀 43,666評論 2 350

推薦閱讀更多精彩內(nèi)容