HDU - 2112 - HDU Today(最短路+链式前向星)

题目链接:https://vjudge.net/problem/HDU-2112

题目大意:就是找一条从起点到终点的最短路

  最短路裸题,要注意的一点是起点可能和终点在同一个地方一段时间没写最短路忘得差不多了,顺便也有熟悉了一下链式前向星

#include<set>
#include<map>
#include<list>
#include<stack>
#include<queue>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define endl '\n'
#define rtl rt<<1
#define rtr rt<<1|1
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define maxx(a, b) (a > b ? a : b)
#define minn(a, b) (a < b ? a : b)
#define zero(a) memset(a, 0, sizeof(a))
#define INF(a) memset(a, 0x3f, sizeof(a))
#define IOS ios::sync_with_stdio(false)
#define _test printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> P2;
const double pi = acos(-1.0);
const double eps = 1e-7;
const ll MOD =  1000000007LL;
const int INF = 0x3f3f3f3f;
const int _NAN = -0x3f3f3f3f;
const double EULC = 0.5772156649015328;
const int NIL = -1;
template<typename T> void read(T &x){
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
const int maxn = 1e5+10;
map<string, int> mark;
struct E { //链式前向星
    int to, w, next; //指向的结点,权值,指向的下一个点 
} edge[maxn];
int head[maxn], tot; //头指针,内存池标记
inline void add_edge(int u, int v, int w) { 
    edge[tot].to = v; //将值分配给新的内存
    edge[tot].w = w; 
    edge[tot].next = head[u]; //新内存块指向原来所在链的头部
    head[u] = tot++; //将头部指到新内存块
}
int dis[maxn];
void dij(int n) {
    for (int i = 2; i<10||i<=n; ++i) dis[i] = INF; //初始化,为什么价格i<10?因为可能n=0
    priority_queue<P, vector<P>, greater<P> > pq; //优先队列版本dij模板
    pq.push(make_pair(0, 1));
    while(!pq.empty()) {
        P t = pq.top(); pq.pop();
        int u = t.second;
        if (dis[u]<t.first) continue;
        for (int i = head[u]; ~i; i = edge[i].next) { //链式前向星遍历
            int v = edge[i].to, w = edge[i].w;
            if (dis[v] > dis[u] + w) {
                dis[v] = dis[u] + w;
                pq.push(make_pair(dis[v], v)); 
            }
        }
    }
}
int main(void) {
    IOS; int n;
    while(cin >> n && n>=0) {
        tot = 0;
        for (int i = 0; i<=n; ++i) head[i] = -1;
        string s1, s2;
        cin >> s1 >> s2; 
        bool ok = false;
        if(s1==s2) ok = true; //坑点!!!可能起始站和终点站在同一个地方
        int kase = 1;
        mark[s1] = kase++; //用map将地名转化为数字结点
        mark[s2] = kase++;
        for (int i = 0, d; i<n; ++i) {
            cin >> s1 >> s2 >> d;
            if (!mark[s1]) mark[s1] = kase++; //给新地名分配数字
            if (!mark[s2]) mark[s2] = kase++;
            int a = mark[s1], b = mark[s2];
            add_edge(a, b, d); //因为是无向图
            add_edge(b, a, d); //所以两个方向都要有边
        }
        mark.clear(); 
        dij(n);
        if (ok) {
            cout << 0 << endl;
            continue;
        }
        cout << (dis[2]!=INF? dis[2] : -1) << endl;
    }
    return 0;
}

 

posted @ 2020-03-08 00:11  shuitiangong  阅读(170)  评论(0编辑  收藏  举报