usaco2.4 comehome 最短路 Floyd, Dijkstra, Dijkstra优先队列优化

来源:互联网 发布:希腊经济知乎 编辑:程序博客网 时间:2024/06/10 07:37

题目链接


水题,求到某一个点的最短路


(第一发还写跪了...)


后来觉得需要练练手(?并不能起到什么作用)于是分别写了Floyd,Dijkstra以及Dijkstra堆优化(即优先队列)的


Floyd

/*PROB:comehomeLANG:C++ID:fan_0111*/#include <iostream>#include <cstdio>#include <cctype>typedef long long LL;LL a[100][100];using namespace std;int tran(char ch) {    if (isupper(ch)) return ch - 'A';    else return ch - 'a' + 26;}int main() {    freopen("comehome.in", "r", stdin);    freopen("comehome.out", "w", stdout);    int n;    scanf("%d\n", &n);    for (int i = 0; i < n; ++i) {        char ch1, ch2; int d;        scanf("%c %c%d\n", &ch1, &ch2, &d);        if (ch1 == ch2) continue;        int x1 = tran(ch1), x2 = tran(ch2);//        printf("%c %c\n", ch1, ch2);        if (a[x1][x2] == 0 || a[x1][x2] > d) a[x1][x2] = a[x2][x1] = d;//        printf("a[%d][%d] = %d\n", x1, x2, a[x1][x2]);    }    const int tot = 52;    for (int k = 0; k < tot; ++k) {        for (int i = 0; i < tot; ++i) {            for (int j = i + 1; j < tot; ++j) {                if (a[i][k] != 0 && a[k][j] != 0 && (a[i][j] == 0 || a[i][j] > a[i][k] + a[k][j])) {                    a[j][i] = a[i][j] = a[i][k] + a[k][j];                }            }        }    }    int p;    int maxD = 1e9;    for (int i = 0; i < 25; ++i) {        if (a[i][25] != 0 && a[i][25] < maxD) { p = i; maxD = a[i][25]; }    }    printf("%c %lld\n", 'A' + p, a[p][25]);    return 0;}


Dijkstra

/*PROB:comehomeLANG:C++ID:fan_0111*/#include <iostream>#include <cstdio>#include <cctype>#define MAX 60000typedef long long LL;const int tot = 52;int a[100][100], dist[100];bool vis[60];using namespace std;int tran(char ch) {    if (isupper(ch)) return ch - 'A';    else return ch - 'a' + 26;}int main() {    freopen("comehome.in", "r", stdin);    freopen("comehome.out", "w", stdout);    int n;    scanf("%d\n", &n);    for (int i = 0; i < n; ++i) {        char ch1, ch2; int d;        scanf("%c %c%d\n", &ch1, &ch2, &d);        if (ch1 == ch2) continue;        int x1 = tran(ch1), x2 = tran(ch2);        if (a[x1][x2] == 0 || a[x1][x2] > d) a[x1][x2] = a[x2][x1] = d;    }    int src = 25;    for (int cnt = 0; cnt < tot - 1; ++cnt) {        int minD = MAX, p;        for (int i = 0; i < tot; ++i) {            dist[i] = a[src][i];            if (!vis[i] && dist[i] != 0 && dist[i] < minD) {                minD = dist[i];                p = i;            }        }        vis[p] = true;        for (int i = 0; i < tot; ++i) {            if (i != src && a[p][i] != 0 && (a[src][i] == 0 || a[src][i] > a[src][p] + a[p][i])) a[src][i] = a[i][src] = a[src][p] + a[p][i];        }    }    int p, minD = MAX;    for (int i = 0; i < 25; ++i) {//        printf("%d\n", a[src][i]);        if (a[src][i] != 0 && a[src][i] < minD) { p = i; minD = a[src][i]; }    }    printf("%c %d\n", 'A' + p, a[src][p]);    return 0;}


优先队列

/*PROB:comehomeLANG:C++ID:fan_0111*/#include <iostream>#include <cstdio>#include <cctype>#include <queue>#define MAX 60000typedef long long LL;const int tot = 52;int a[100][100], dist[100];bool vis[60];using namespace std;int tran(char ch) {    if (isupper(ch)) return ch - 'A';    else return ch - 'a' + 26;}struct qnode {    int v, c;    qnode(int vv = 0, int cc = 0) : v(vv), c(cc) {}    bool operator < (const qnode& node) const { return c > node.c; }};priority_queue<qnode> que;int main() {    freopen("comehome.in", "r", stdin);    freopen("comehome.out", "w", stdout);    int n;    scanf("%d\n", &n);    for (int i = 0; i < n; ++i) {        char ch1, ch2; int d;        scanf("%c %c%d\n", &ch1, &ch2, &d);        if (ch1 == ch2) continue;        int x1 = tran(ch1), x2 = tran(ch2);        if (a[x1][x2] == 0 || a[x1][x2] > d) a[x1][x2] = a[x2][x1] = d;    }    int src = 25;    vis[src] = true;    for (int i = 1; i < tot; ++i) {        for (int j = 0; j < tot; ++j) {            if (a[src][j] == 0) continue;            if (!vis[j] && (dist[j] == 0 || dist[j] > dist[src] + a[src][j])) {                dist[j] = dist[src] + a[src][j];                que.push(qnode(j, dist[j]));            }        }        while (!que.empty() && vis[que.top().v]) que.pop();        if (que.empty()) break;        vis[src = que.top().v] = true;    }    int minD = MAX, p;    for (int i = 0; i < 26; ++i) {        if (dist[i] != 0 && dist[i] < minD) { minD = dist[i]; p = i; }    }    printf("%c %d\n", 'A' + p, dist[p]);    return 0;}



阅读全文
0 0