码迷,mamicode.com
首页 > 其他好文 > 详细

链式前向星版DIjistra POJ 2387

时间:2018-10-04 18:22:54      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:函数   nts   堆优化   rom   from   https   一起   imu   个数   

链式前向星


在做图论题的时候,偶然碰到了一个数据量很大的题目,用vector的邻接表直接超时,上网查了一下发现这道题数据很大,vector可定会超的,不会指针链表的我找到了链式前向星这个好东西,接下来就由一道裸模板题看看链式前向星怎么写,他的优势又在哪里!

题目链接:POJ 2387

Description

Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible.

Farmer John‘s field has N (2 <= N <= 1000) landmarks in it, uniquely numbered 1..N. Landmark 1 is the barn; the apple tree grove in which Bessie stands all day is landmark N. Cows travel in the field using T (1 <= T <= 2000) bidirectional cow-trails of various lengths between the landmarks. Bessie is not confident of her navigation ability, so she always stays on a trail from its start to its end once she starts it.

Given the trails between the landmarks, determine the minimum distance Bessie must walk to get back to the barn. It is guaranteed that some such route exists.

Input

Line 1: Two integers: T and N

Lines 2..T+1: Each line describes a trail as three space-separated integers. The first two integers are the landmarks between which the trail travels. The third integer is the length of the trail, range 1..100.

Output

Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.

Sample Input

5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100

Sample Output

90

题意

很简单的最短路模板题,输一个图进去,不过是先输边数m后输点数n,也算是一个坑吧。

题解:

用链式前向星存图再用Dijistra跑一遍,首先比起邻接表存图,在结构体的构造上就有所不同。

  • 这是链式前向星
struct Edge {
    int to;
    int w;
    int next;
}edge[MAXN];
  • 这是普通的邻接表(vector)
struct Edge {
    int to;
    int w;
    Edge(){}
    Edge(int a, int b) {
        to = a;
        w = b;
    }
    bool operator<(const Edge a)const {
        return w == a.w ? to > a.to:w > a.w;
    }
};

其实区别最大的地方就在于链式前向星有了next这个属性,来表明他的下一个边在边数组中的位置,而邻接表是给每个点都分别存他相连的每条边,分开存的,链式前向星全都存在了一起,所有在双向边的时候,要个maxn开2倍大小,邻接表不用考虑这个情况,也算新手的坑吧。
然后是建图操作,用函数封装起来

int head[MAXN];
long long dis[MAXN];
int vis[MAXN];
int cut;
int n, m;
void addedge(int s, int g, int w) {
    edge[cut].to = g;
    edge[cut].w = w;
    edge[cut].next = head[s];
    head[s] = cut++;
}

用cut存边的个数,然后进行加边操作,完全不懂加边原理的看这里传送门,这网上的模板用到了两个结构体,一个用来存所有的边,一个用来堆优化的时候构造边,这样也能节省时间和内存吧,让其中一个结构体很轻量,其实链式前向星的原理是有点难搞懂,但是这个代码还是比较简单的。最可怕的是原来79ms的题直接0ms过了,恐怖如斯!!

代码

//#include<bits/stdc++.h>
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#define lson i<<2
#define rson i<<2|1
#define LS l,mid,lson
#define RS mid+1,r,rson
#define mem(a,x) memset(a,x,sizeof(a))
#define gcd(a,b) __gcd(a,b)
#define ll long long
#define ull unsigned long long
#define lowbit(x) (x&-x)
#define pb(x) push_back(x)
#define enld endl
#define mian main
#define itn int
#define prinft printf
#pragma GCC optimize(2)
#pragma comment(linker, "/STACK:102400000,102400000")

const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int EXP = 1e-8;
const int N = 1e5 + 5;
const int MOD = 1e9 + 7;
const int MAXN = 4e3 + 5;

using namespace std;
struct Edge {
    int to;
    int w;
    int next;
}edge[MAXN];
struct Node {
    int to;
    int w;
    Node(){}
    Node(int a, int b) {
        to = a;
        w = b;
    }
    bool operator<(const Node a)const {
        return w == a.w ? to > a.to:w > a.w;
    }
};
int head[MAXN];
long long dis[MAXN];
int vis[MAXN];
int cut;
int n, m;
void addedge(int s, int g, int w) {
    edge[cut].to = g;
    edge[cut].w = w;
    edge[cut].next = head[s];
    head[s] = cut++;
}
void Init() {
    cut = 0;
    mem(vis, false);
    mem(dis, INF);
    mem(head, -1);
    /*for (int i(0); i <= n; i++) {
        vis[i] = false;
        dis[i] = INF;
        head[i] = -1;
    }*/
}
Node cur;
void Dijistra(int s) {
    priority_queue<Node>P;
    P.push(Node(s, 0));
    dis[s] = 0;
    while (!P.empty()) {
        cur = P.top();
        P.pop();
        int u = cur.to;
        if (vis[u])continue;
        vis[u] = true;
        for (int i = head[u]; ~i; i = edge[i].next) {
            int to = edge[i].to;
            int w = edge[i].w;
            if (!vis[to] && dis[to] > dis[u] + w) {
                dis[to] = dis[u] + w;
                P.push(Node(to, dis[to]));
            }
        }
    }
}
int main() {
    int a, b, c;
    while (~scanf("%d%d",&m,&n)) {
        Init();
        while (m--) {
            //cin >> a >> b >> c;
            scanf("%d%d%d", &a, &b, &c);
            addedge(a, b, c);
            addedge(b, a, c);
        }
        Dijistra(1);
        printf("%lld\n", dis[n]);
        //cout << dis[n] << endl;
    }


    return 0;
}

链式前向星版DIjistra POJ 2387

标签:函数   nts   堆优化   rom   from   https   一起   imu   个数   

原文地址:https://www.cnblogs.com/Titordong/p/9742684.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!