最短路 hnu13430 Elevators

传送门:点击打开链接

题意:n个电梯,告诉每个电梯可以停的楼层,问从某一楼层到另一楼层所需要的最少距离

思路:好傻啊,,就是个最短路问题,对于某个电梯,两两枚举点,建立边,然后跑一遍最短路就做完了

#include<map>
#include<set>
#include<cmath>
#include<stack>
#include<queue>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define fuck printf("fuck")
#define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w+",stdout)
using namespace std;
typedef long long LL;
typedef pair<LL, int> PLI;

const int MX = 200 + 5;
const int ME = 1e6 + 5;
const int INF = 0x3f3f3f3f;

LL d[MX];

int Head[MX], Next[ME], rear;

struct Edge {
    int u, v, cost;
} E[ME];

void edge_init() {
    rear = 0;
    memset(Head, -1, sizeof(Head));
}

void edge_add(int u, int v, int cost) {
    E[rear].u = u;
    E[rear].v = v;
    E[rear].cost = cost;
    Next[rear] = Head[u];
    Head[u] = rear++;
}

void dijistra(int Begin) {
    memset(d, INF, sizeof(d));
    d[Begin] = 0;

    priority_queue<PLI, vector<PLI>, greater<PLI> >work;
    work.push(PLI(0, Begin));

    while(!work.empty()) {
        PLI f = work.top();
        work.pop();

        LL dist = f.first;
        int u = f.second;
        if(d[u] < dist) continue;

        for(int id = Head[u]; ~id; id = Next[id]) {
            int cost = E[id].cost, v = E[id].v;
            if(dist + cost < d[v]) {
                d[v] = dist + cost;
                work.push(PLI(dist + cost, v));
            }
        }
    }
}

int A[MX];

int main() {
    int n, m, bx, ex; //FIN;
    while(~scanf("%d%d%d", &n, &bx, &ex), n) {
        edge_init();
        for(int i = 1; i <= n; i++) {
            scanf("%d", &m);
            for(int j = 1; j <= m; j++) {
                scanf("%d", &A[j]);
            }

            for(int j = 1; j <= m; j++) {
                for(int k = j + 1; k <= m; k++) {
                    edge_add(A[j], A[k], abs(A[j] - A[k]));
                    edge_add(A[k], A[j], abs(A[j] - A[k]));
                }
            }
        }

        dijistra(bx);
        printf("%I64d\n", d[ex]);
    }
    return 0;
}


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