自己写的Dijkstra模板

话不多说,邻接表+队列优化。

#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>

using namespace std;

const int INF=0x3f3f3f3f;
const int MAXN=1000010;
const int MAXE=1000010;

struct qnode
{
    int v;
    int c;
    qnode(int _v=0,int _c=0):v(_v),c(_c){}
    bool operator <(const qnode &r)const
    {
        return c>r.c;
    }
};

struct Edge
{
    int v,cost;
    int next;
};

Edge edge[MAXE];
int head[MAXN];
bool vis[MAXN];
int dist[MAXN];
int tot;
int n,m;
priority_queue<qnode>que;

void Dijkstra(int start,int n)//点的编号从1开始
{
    dist[start]=0;
    que.push(qnode(start,0));
    qnode tmp;
    while(!que.empty())
    {
        tmp=que.top();
        que.pop();
        int u=tmp.v;
        if(vis[u])continue;
        vis[u]=true;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v;
            int cost=edge[i].cost;
            if(!vis[v]&&dist[v]>dist[u]+cost)
            {
                dist[v]=dist[u]+cost;
                que.push(qnode(v,dist[v]));
            }
        }
    }
}

void addedge(int u,int v,int w)
{
    edge[tot].v=v;
    edge[tot].cost=w;
    edge[tot].next=head[u];
    head[u]=tot;
    tot++;
}

void init()
{
    tot=0;
    memset(edge,0,sizeof(edge));
    memset(head,-1,sizeof(head));
    memset(vis,false,sizeof(vis));
    for(int i=1;i<=n;i++)dist[i]=INF;
    while(!que.empty())que.pop();
}

int main()
{
//    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        init();
        for(int i=0;i<m;i++){
            int from,to,cost;
            scanf("%d%d%d",&from,&to,&cost);
            addedge(from,to,cost);
        }
        Dijkstra(1,n);//答案在dist数组中
    }
    return 0;
}

时间: 2024-07-29 03:17:28

自己写的Dijkstra模板的相关文章

自己写快排模板与C++快排库函数使用

自己写快排模板与C++快排库函数使用 1.自己写快排模板 我理解的快速排序思想原理是: 假定待排序数组的范围是0~N 1.在一个数组中找一个数作为中心点M(可以使用固定位置的数,也可以随机的采用数组中的数) 2.把数组中所有的数与这个中心点进行比较.小于中心点的数移到中心点左边,大于中心点的数移到中心点右边. 3.经过上面两步可以得到由M点所划分的两个数组(一个数组都小于等于M,一个都大于等于M),再对这两个数组递归的进行1.2所示步骤,知道划分的数组大小为0: 快排思想实现的主要的重点难点在于

Dijkstra模板题图论书p133

#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list> #include <

UVA - 11374 Airport Express (Dijkstra模板+枚举)

Description Problem D: Airport Express In a small city called Iokh, a train service, Airport-Express, takes residents to the airport more quickly than other transports. There are two types of trains in Airport-Express, the Economy-Xpress and the Comm

最短路径之 Dijkstra模板

一:时间复杂度为O(V*V)的Dijkstra const int Max_v = 100 + 10; const int INF = 1<<30; int cost[Max_v][Max_v];//权值 int d[Max_v];//顶点s出发最短距离 bool used[Max_v];//以使用过的图 int V;//顶点数 int Edge;//边数 void dijkstra(int s) { fill(d,d+V,INF); fill(used,used+V,false); d[s]

写一个迷你模板引擎

一直想写一个模板引擎用在自己的代码上,因为之前用的一个开源的产品,每次需要变通的时候都会遇到一些局限性,不如自己写的实在,想改哪就改哪,于是今天花了一点时间造了一个很小的模板引擎,核心功能已经存在,其他的待到以后慢慢的扩充. 模板引擎说白了,就是找到页面上的占位符,然后替换掉,再插入到页面中,不管功能还是实现方法都极其简单. 占位符也就两个地方能够出现的: 文本节点 属性值 通过childNodes能够找到文本节点,通过attributes能够找到该元素下的所有存在属性值,所以请看代码,以下代码

POJ 2387 Til the Cows Come Home (dijkstra模板题)

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 Joh

Dijkstra 模板 最短路

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents ------------------------------------------------------------------------------------------------------------------------------------------------------ 欢迎光临天资小屋:http://user.qzone.qq.com/5938309

HDU1584-A strange lift-最短路(Dijkstra模板题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 这个题目很容易让人用广搜...无语... #include<iostream> #include<string> #include<cstdio> #include<cstring> #include<map> #include<queue> #include<cmath> #include<stack> #

朴素Dijkstra模板函数

朴素Dijkstra不需要用到堆,每次找最小值都得遍历一遍,时间复杂度较高. 点是从编号0到编号n-1的n个点. 以下是模板: int Dijkstra(int a,int b) //a是起点,b是终点 { bool visited[maxn]; // 记录访问点 int pos = a, min, dist[maxn]; // pos是个标记点,标记每次最小边对应的点 ,dist记录起点到各点间最短距离. for(int i = 0; i < n; i++) { dist[i] = map[a