HDU Today dhu-2112

Problem Description

经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强。这时候,XHD夫妇也退居了二线,并在风景秀美的诸暨市浬浦镇陶姚村买了个房子,开始安度晚年了。这样住了一段时间,徐总对当地的交通还是不太了解。有时很郁闷,想去一个地方又不知道应该乘什么公交车,在什么地方转车,在什么地方下车(其实徐总自己有车,却一定要与民同乐,这就是徐总的性格)。徐总经常会问蹩脚的英文问路:“Can you help me?”。看着他那迷茫而又无助的眼神,热心的你能帮帮他吗?请帮助他用最短的时间到达目的地(假设每一路公交车都只在起点站和终点站停,而且随时都会开)。

 

Input

输入数据有多组,每组的第一行是公交车的总数N(0<=N<=10000);第二行有徐总的所在地start,他的目的地end;接着有n行,每行有站名s,站名e,以及从s到e的时间整数t(0

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 虽然偶尔会迷路,但是因为有了你的帮助 **和**从此还是过上了幸福的生活。 ――全剧终――

这个题目是dijkstra可以解决的问题!可是这个题是双向图,所以要存两条边!一开始没有发现以为是单向图,结果wa了好久!

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define INF 0x3f3f3f3f
#define mem memset
#define sc scanf
#define pr printf
typedef long long ll;
using namespace std;
//inline int read(){
//    int X = 0, w = 0; char ch = 0;
//    while (!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
//    while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
//    return w ? -X : X;
//}
//inline double dbread(){
//    double X = 0, Y = 1.0; int w = 0; char ch = 0;
//    while (!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
//    while (isdigit(ch)) X = X * 10 + (ch ^ 48), ch = getchar();
//    ch = getchar();//读入小数点
//    while (isdigit(ch)) X += (Y /= 10) * (ch ^ 48), ch = getchar();
//    return w ? -X : X;
//}
//inline void write(int x){
//    if (x < 0) putchar('-'), x = -x;
//    if (x > 9) write(x / 10);
//    putchar(x % 10 + '0');
//}
#define maxn 152
mapmp;
struct Edge {
    int from, to, dist;
    Edge(int f, int t, int ds) :from(f), to(t), dist(ds) {}
};
struct HeapNode {
    int d, u;
    bool operator<(const HeapNode& rhs) const {
        return d > rhs.d;
    }
    HeapNode(int d, int u) :d(d), u(u) {}
};
struct Dijkstra {
    int n, m;
    vectoredges;
    vector G[maxn];
    bool done[maxn];
    int d[maxn];
    int p[maxn];
    void init(int n) {
        this->n = n;
        for (int i = 0; i < n; ++i)
            G[i].clear();
        edges.clear();
    }
    void addedge(int from, int to, int dist) {
        edges.push_back(Edge(from, to, dist));
        m = edges.size();
        G[from].push_back(m - 1);
    }
    int dijkstra(int s) {
        priority_queueQ;
        for (int i = 0; i < n; ++i)
            d[i] = INF;
        d[s] = 0;
        mem(done, 0, sizeof(done));
        Q.push(HeapNode(0, s));
        while (!Q.empty()) {
            HeapNode x = Q.top();
            Q.pop();
            int u = x.u;
            if (done[u])
                continue;
            done[u] = true;
            for (int i = 0; i < G[u].size(); ++i) {
                Edge& e = edges[G[u][i]];
                if (d[e.to] > d[u] + e.dist) {
                    d[e.to] = d[u] + e.dist;
                    p[e.to] = G[u][i];
                    Q.push(HeapNode(d[e.to], e.to));
                }
            }
        }
        return d[2];
    }
}DK;
int main() {
    ios::sync_with_stdio(false);
    string s1, s2;
    int n;
    while (cin >> n && n != -1) {
        int m;
        DK.init(152);
        mp.clear();
        cin >> s1 >> s2;
        mp[s1] = 1;
        mp[s2] = 2;
        int cnt = 3;
        bool flag = 0;
        if (s1.compare(s2) == 0)
            flag = 1;
        for (int i = 1; i <= n; ++i) {
            cin >> s1 >> s2 >> m;
            if (!mp[s1]) mp[s1] = cnt++;
            if (!mp[s2]) mp[s2] = cnt++;
            DK.addedge(mp[s1], mp[s2], m);
            DK.addedge(mp[s2], mp[s1], m);
        }
        int len = DK.dijkstra(1);
        if (flag)
            cout << 0 << endl;
        else if (len == INF)
            cout << -1 << endl;
        else
            cout << len << endl;
    }
    return 0;
}

 

你可能感兴趣的:(最短路)