hdu 2112 HDU Today(映射+spfa)

HDU Today

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3816    Accepted Submission(s): 908

Problem Description
經過錦囊相助,海東集團終於度過了危機,從此,HDU的發展就一直順風順水,到了2050年,集團已經相當規模了,據說進入了錢江肉絲經濟開發區500強。這時候,XHD夫婦也退居了二線,並在風景秀美的諸暨市浬浦鎮陶姚村買了個房子,開始安度晚年了。
這樣住了一段時間,徐總對當地的交通還是不太瞭解。有時很鬱悶,想去一個地方又不知道應該乘什麼公交車,在什麼地方轉車,在什麼地方下車(其實徐總自己有車,卻一定要與民同樂,這就是徐總的性格)。
徐總經常會問蹩腳的英文問路:“Can you help me?”。看着他那迷茫而又無助的眼神,熱心的你能幫幫他嗎?
請幫助他用最短的時間到達目的地(假設每一路公交車都只在起點站和終點站停,而且隨時都會開)。
 
Input
輸入數據有多組,每組的第一行是公交車的總數N(0<=N<=10000);
第二行有徐總的所在地start,他的目的地end;
接着有n行,每行有站名s,站名e,以及從s到e的時間整數t(0<t<100)(每個地名是一個長度不超過30的字符串)。
note:一組數據中地名數不會超過150個。
如果N==-1,表示輸入結束。
 
Output
如果徐總能到達目的地,輸出最短的時間;否則,輸出“-1”。
 
Sample Input
6 xiasha westlake xiasha station 60 xiasha ShoppingCenterofHangZhou 30 station westlake 20 ShoppingCenterofHangZhou supermarket 10 xiasha supermarket 50 supermarket westlake 10 -1


 

Sample Output
50 Hint: The best route is: xiasha->ShoppingCenterofHangZhou->supermarket->westlake
        
  
          其實就是求最短路,出入的是城市,用映射比較方便,最短路用spfa,這樣輕鬆搞定。
代碼:
 
#include <iostream>
#include <stdio.h>
#include <memory.h>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;

const int N = 155;
const int INF = 99999999;

map<string, int> mp;
map<string, bool> bp;
int way[N][N], dist[N];
bool visit[N];
string begin, end;
int n, ans;

void init()     //初始化函數
{
    int i, j;
    mp.clear(); //清空映射mp
    bp.clear(); //清空映射bp
    for(i = 0; i < N; i++)
        for(j = 0; j < N; j++)
            if(i == j) way[i][j] = 0;
            else way[i][j] = INF;
}

void input()    //輸入函數
{
    int i, cost;
    ans = 1;
    string str1, str2;
    cin >> begin >> end;
    mp[begin] = 1;      //mp映射該string爲1
    bp[begin] = true;   //bp映射該string爲true
    if(!bp[end])
    {
        mp[end] = ++ans;
        bp[end] = true;
    }
    for(i = 1; i <= n; i++)
    {
        cin >> str1 >> str2 >> cost;
        if(!bp[str1])
        {
            mp[str1] = ++ans;
            bp[str1] = true;
        }
        if(!bp[str2])
        {
            mp[str2] = ++ans;
            bp[str2] = true;
        }
        way[mp[str1]][mp[str2]] = way[mp[str2]][mp[str1]] = cost;
    }
}

void spfa()
{
    int i, now;
    memset(visit, false, sizeof(visit));
    for(i = 0; i <= ans; i++) dist[i] = INF;
    dist[1] = 0;
    queue<int> Q;
    Q.push(1);
    visit[1] = true;
    while(!Q.empty())
    {
        now = Q.front();
        Q.pop();
        visit[now] = false;
        for(i = 1; i <= ans; i++)
        {
            if(dist[i] > dist[now] + way[now][i])
            {
                dist[i] = dist[now] + way[now][i];
                if(visit[i] == false)
                {
                    Q.push(i);
                    visit[i] = true;
                }
            }
        }
    }
}

int main()
{
    while(scanf("%d", &n))
    {
        if(n == -1) break;
        init();
        input();
        spfa();
        if(dist[mp[end]] != INF) printf("%d\n", dist[mp[end]]);
        else printf("-1\n");
    }
    return 0;
}

 


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章