HDU 2544 --最短路

題目:

Description

在每年的校賽裏,所有進入決賽的同學都會獲得一件很漂亮的t-shirt。但是每當我們的工作人員把上百件的衣服從商店運回到賽場的時候,卻是非常累的!所以現在他們想要尋找最短的從商店到賽場的路線,你可以幫助他們嗎?

Input

輸入包括多組數據。每組數據第一行是兩個整數N、M(N<=100,M<=10000),N表示成都的大街上有幾個路口,標號爲1的路口是商店所在地,標號爲N的路口是賽場所在地,M則表示在成都有幾條路。N=M=0表示輸入結束。接下來M行,每行包括3個整數A,B,C(1<=A,B&lt;=N,1<=C<=1000),表示在路口A與路口B之間有一條路,我們的工作人員需要C分鐘的時間走過這條路。
輸入保證至少存在1條商店到賽場的路線。

Output

對於每組輸入,輸出一行,表示工作人員從商店走到賽場的最短時間

Sample Input

2 1
1 2 3
3 3
1 2 5
2 3 5
3 1 2
0 0

Sample Output

3
2

題意:裸的單源最短路。


實現:由於很久沒寫,所以幾乎都忘了,因此最近熟悉一下,然後用鄰接表和鄰接矩陣寫了一下,其實也熟悉了一下vector數組把。


鄰接表:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <math.h>
using namespace std;

const int MAX = 105;
const int INF = 0x3f3f3f3f;

int n, m;

struct graph {
    int vv, key;
};

struct node {
    int v;
    int value;
    bool friend operator < (node n1, node n2) {
        return n1.value > n2.value;
    }
}visit[MAX];

vector <graph> path[MAX];

void Dij() {
    priority_queue <node> q;
    q.push(visit[1]);
    while (!q.empty()) {
        node head = q.top();
        q.pop();
        for (int i = 0; i < path[head.v].size(); i++) {
            if (visit[head.v].value + path[head.v][i].key < visit[path[head.v][i].vv].value) {
                visit[path[head.v][i].vv].value = visit[head.v].value + path[head.v][i].key;
                q.push(visit[path[head.v][i].vv]);
            }
        }
    }
}

int main() {
    while (scanf("%d%d", &n, &m) != EOF) {
        if (n == 0 && m == 0)
            break;
        for (int i = 1; i <= n; i++)
            path[i].clear();
        int v1, v2, key;
        graph g1, g2;
        for (int i = 0; i < m; i++) {
            scanf("%d%d%d", &v1, &v2, &key);
            g1.vv = v2;
            g1.key = key;
            g2.vv = v1;
            g2.key = key;
            path[v1].push_back(g1);
            path[v2].push_back(g2);
        }
        for (int i = 1; i <= n; i++) {
            visit[i].v = i;
            visit[i].value = INF;
        }
        visit[1].value = 0;
        Dij();
        printf("%d\n", visit[n].value);

    }
    return 0;
}

鄰接矩陣:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <math.h>
using namespace std;

const int MAX = 105;
const int INF = 0x3f3f3f3f;

int graph[MAX][MAX];
int n, m;

struct node {
    int v;
    int value;
    bool friend operator < (node n1, node n2) {
        return n1.value > n2.value;
    }
}visit[MAX];

void Dij() {
    priority_queue <node> q;
    for (int i = 1; i <= n; i++) {
        q.push(visit[i]);
    }
    while (!q.empty()) {
        node head = q.top();
        q.pop();
        for (int j = 1; j <= n; j++) {
            if (graph[head.v][j] + head.value < visit[j].value) {
                visit[j].value = graph[head.v][j] + head.value;
                q.push(visit[j]);
            }
        }
    }
}

int main() {
    while (scanf("%d%d", &n, &m) != EOF) {
        if (n == 0 && m == 0)
            break;
        memset(graph, 0, sizeof(graph));
        int v1, v2, key;
        for (int i = 0; i < m; i++) {
            scanf("%d%d%d", &v1, &v2, &key);
            if (graph[v1][v2] == 0 || graph[v1][v2] > key) {
                graph[v1][v2] = key;
                graph[v2][v1] = key;
            }
        }
        for (int i = 1; i <= n; i++) {
            visit[i].v = i;
            visit[i].value = INF;
        }
        visit[1].value = 0;
        Dij();
        printf("%d\n", visit[n].value);

    }
    return 0;
}


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