HDU 2544 最短路 【Dijkstra模板题】
传送门: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模板题】的更多相关文章
- HDU 2544最短路dijkstra模板题
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- HDU 2544 最短路(模板题——Floyd算法)
题目: 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你 ...
- ACM: HDU 2544 最短路-Dijkstra算法
HDU 2544最短路 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descrip ...
- HDU-2544 最短路 Dijkstra模板题
题目链接:https://vjudge.net/problem/HDU-2544 题意: 题目要求找到节点1到节点n之间的一条最短路 分析: Dijkstra模板题 单源最短路径,可以用dijkstr ...
- 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 ...
- hdu 2544 最短路 Dijkstra
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 题目分析:比较简单的最短路算法应用.题目告知起点与终点的位置,以及各路口之间路径到达所需的时间, ...
- HDOJ/HDU 2544 最短路---dijkstra算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 这题的思路可以见这里(同一类型):http://blog.csdn.net/xiaozhuaix ...
- HDU 2544 最短路(dijkstra+邻接矩阵)
( ̄▽ ̄)" #include<iostream> #include<cstdio> using namespace std; const int INF=10e7; ...
- UESTC 30 &&HDU 2544最短路【Floyd求解裸题】
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
随机推荐
- POJ3616:Milking Time
Milking Time Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5682 Accepted: 2372 Desc ...
- 英语学习 - 进行时态的被动 ( be being done )
be being done 例: The story book was being read by him .
- Elasticsearch开启试用x-pack license
一.Elasticsearch 6.7.2开启trial x-pack license:x-pack的license试用期只有30天 1.ES6.7.2版本默认已经安装了x-pack插件,这里就没有 ...
- bzoj 2796: [Poi2012]Fibonacci Representation
结论貌似是,,,肯定只有没有重复的数字.http://hzwer.com/6426.html 一开始猜的是贪心,感觉也是可以的啊...(想想都有道理,然而看到是神奇的(dp类)记忆化搜索,直接虚的不敢 ...
- Elasticsearch 使用集群 - 创建索引
章节 Elasticsearch 基本概念 Elasticsearch 安装 Elasticsearch 使用集群 Elasticsearch 健康检查 Elasticsearch 列出索引 Elas ...
- BurpSuite详解
转载自:http://www.nxadmin.com/tools/689.html 本文由阿德马翻译自国外网站,请尊重劳动成果,转载注明出处 Burp Suite是Web应用程序测试的最佳工具之一,其 ...
- P 1016 部分A+B
转跳点:
- POJ 1944:Fiber Communications
Fiber Communications Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 4236 Accepted: 1 ...
- 【LeetCode】最长公共子序列
[问题]给定两个字符串A和B,长度分别为m和n,要求找出它们最长的公共子串,并返回其长度.例如:A = "HelloWorld"B = "loop"则A与B的最 ...
- cf 525D.Arthur and Walls
判断2*2的正方形,是不是3个"."1个"*"然后暴力bfs就好.(这种处理也是挺神奇的2333%%题解) #include<bits/stdc++.h& ...