传送门http://acm.hdu.edu.cn/showproblem.php?pid=2544

思路:最短路的模板题

Dijkstra 算法是一种类似于贪心的算法,步骤如下:

1、当到一个点时,图上部分的点的最短距离已确定,部分点的最短距离未确定。

2、选一个所有未确定点中离源点最近的点,把它认为成最短距离。

3、再把这个点所有出边遍历一边,更新所有的点。

朴素算法(适用于稠密图 复杂度

#include<iostream>
#include<cstdio>
using namespace std;
const int maxn = 105;
const int INF = 9999999;
int w[maxn][maxn];
int dis[maxn];
bool vis[maxn];
int n, m;
void dijkstra()
{
for(int i = 1; i <= n; i++)
vis[i] = false;
for(int i = 1; i <= n; i++)
dis[i] = (i == 1 ? 0 : INF);
for(int i = 1; i <= n; i++)
{
int x, m = INF;
for(int y = 1; y <= n; y++)
{
if(!vis[y] && dis[y] <= m)
m = dis[x = y];
}
vis[x] = true;
for(int y = 1; y <= n; y++)
{
dis[y] = min(dis[y], dis[x] + w[x][y]);
}
}
}
int main()
{
while(scanf("%d%d", &n, &m) != EOF)
{
if(n == m && n == 0)
break;
else
{
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
w[i][j] = INF;
for(int i = 1; i <= m; i++)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
w[a][b] = c;
w[b][a] = c;
}
}
dijkstra();
cout << dis[n] << endl;
}
}

优先队列优化(适用于稀疏图 复杂度E*log(V))

#include<iostream>
#include<queue>
using namespace std;
const int maxn = 2e5 + 10;
const int INF = 0x3f3f3f3f;
struct heapnode
{
int d;
int u;
//结构体内嵌函数,速度较快
bool operator <(const heapnode &a) const
{
return d > a.d;
}
};
struct node
{
int to;
int cost;
int next;
};
node edges[maxn << 1];
int head[maxn];
int d[maxn];
bool vis[maxn];
int n, m;
int tot;
void add_edges(int u, int v, int cost)
{
edges[++tot].to = v;
edges[tot].cost = cost;
edges[tot].next = head[u];
head[u] = tot;
}
void dijkstra()
{
priority_queue<heapnode>q;
for(int i = 1; i <= n; i++)
d[i] = INF, vis[i] = 0;
d[1] = 0;
q.push((heapnode)
{
0, 1
});
while(!q.empty())
{
heapnode x = q.top();// 每次取出d值最小的点
q.pop();
int u = x.u;
if(vis[u])
continue;
vis[u] = 1;
for(int i = head[u]; i; i = edges[i].next)
{
node v = edges[i];
if(d[v.to] > d[u] + v.cost)
{
d[v.to] = d[u] + v.cost;
q.push((heapnode)
{
d[v.to], v.to
});
}
}
}
} int main()
{
while(scanf("%d %d", &n, &m) != EOF)
{
if(n == m && n == 0)
break;
else
{
tot = 0;
for(int i = 1; i <= n; i++)
head[i] = 0;
for(int i = 1; i <= m; i++)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add_edges(a, b, c);
add_edges(b, a, c);
}
dijkstra();
cout << d[n] << endl;
}
}
}

HDU 2544 最短路 【Dijkstra模板题】的更多相关文章

  1. HDU 2544最短路dijkstra模板题

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  2. HDU 2544 最短路(模板题——Floyd算法)

    题目: 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你 ...

  3. ACM: HDU 2544 最短路-Dijkstra算法

    HDU 2544最短路 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descrip ...

  4. HDU-2544 最短路 Dijkstra模板题

    题目链接:https://vjudge.net/problem/HDU-2544 题意: 题目要求找到节点1到节点n之间的一条最短路 分析: Dijkstra模板题 单源最短路径,可以用dijkstr ...

  5. hdu 2544 hdu 1874 poj 2387 Dijkstra 模板题

    hdu 2544  求点1到点n的最短路  无向图 Sample Input2 1 //结点数 边数1 2 3 //u v w3 31 2 52 3 53 1 20 0 Sample Output32 ...

  6. hdu 2544 最短路 Dijkstra

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 题目分析:比较简单的最短路算法应用.题目告知起点与终点的位置,以及各路口之间路径到达所需的时间, ...

  7. HDOJ/HDU 2544 最短路---dijkstra算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 这题的思路可以见这里(同一类型):http://blog.csdn.net/xiaozhuaix ...

  8. HDU 2544 最短路(dijkstra+邻接矩阵)

    ( ̄▽ ̄)" #include<iostream> #include<cstdio> using namespace std; const int INF=10e7; ...

  9. UESTC 30 &&HDU 2544最短路【Floyd求解裸题】

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

随机推荐

  1. 从0到1完成微信小程序开发(2)

    一,小程序的文件结构 小程序包含一个描述程序的app和多个描述各自页面的page 一个小程序主体部分由三个文件组成,必须放在项目的根目录,如下: 一个小程序页面由四个文件组成,分别是: 下面是一个单页 ...

  2. 139-PHP static后期静态绑定(二)

    <?php class test{ //创建test类 public function __construct(){ static::getinfo(); //后期静态绑定 } public s ...

  3. C++ 99表

    #include<iostream> using namespace std; class Sumes { public: int sum; int i, j; }; int main() ...

  4. Scanner类的next()方法和nextLine()方法的区别(简)

    1.  空白符:回车.空格.tab等 2.  next()方法读取到空白符就结束 3. nextLine()方法读取到回车结束,也就是 "\r"

  5. Linux每日练习-crontab

  6. 每天一点点之 taro 框架开发 - taro调用组件传值

    1.调用组件 组件文件 import Taro, { Component } from '@tarojs/taro' import { View } from '@tarojs/components' ...

  7. POJ 1028:Web Navigation

    Web Navigation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 30828   Accepted: 13821 ...

  8. C/C++ SQLite 之基础篇

    文章目录:                   1. 下载 SQLite3 源码: 2. 下载 SQLite3.dll 文件: 3. 生成 SQLite3.lib 文件 : 4. 生成或者下载 SQL ...

  9. Day2-T3

    原题目 Describe:质数问题 code: #pragma GCC optimize(2) #include<bits/stdc++.h> #define KKK 1200 using ...

  10. 65.ORM查询条件:gte,gt,lte和lt的使用

    1. gte: 代表的是大于等于,英文全称为:great than equal.举例:找到文章id大于等于3等文章,示例代码如下: 定义模型的示例代码如下: from django.db import ...