HDU 2544 最短路(简单Dijkstra)
http://acm.hdu.edu.cn/showproblem.php?pid=2544
题意:给你一个N个顶点的无向图,现在要你输出从1号点到N号点的最短距离.
分析:
直接Dijkstra模板,不谈了.
AC代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxn=100+10;
#define INF 1e9
struct Edge
{
int from,to,dist;
Edge(int f,int t,int d):from(f),to(t),dist(d){}
};
struct HeapNode
{
int d,u;
HeapNode(int d,int u):d(d),u(u){}
bool operator <(const HeapNode &rhs)const
{
return d > rhs.d;
}
};
struct Dijkstra
{
int n,m;
vector<Edge> edges;
vector<int> G[maxn];
bool done[maxn];
int d[maxn];
void init(int n)
{
this->n=n;
for(int i=0;i<n;i++) G[i].clear();
edges.clear();
}
void AddEdge(int from,int to,int dist)
{
edges.push_back(Edge(from,to,dist));
m=edges.size();
G[from].push_back(m-1);
}
void dijkstra()
{
priority_queue<HeapNode> Q;
for(int i=0;i<n;i++) d[i]=INF;
d[0]=0;
memset(done,0,sizeof(done));
Q.push(HeapNode(d[0],0));
while(!Q.empty())
{
HeapNode x=Q.top(); Q.pop();
int u=x.u;
if(done[u]) continue;
done[u]=true;
for(int i=0;i<G[u].size();i++)
{
Edge &e=edges[G[u][i]];
if(d[e.to] > d[u]+e.dist)
{
d[e.to] = d[u]+e.dist;
Q.push(HeapNode(d[e.to],e.to));
}
}
}
}
}DJ;
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)==2)
{
if(n==0 && m==0) break;
DJ.init(n);
while(m--)
{
int u,v,d;
scanf("%d%d%d",&u,&v,&d);
u--,v--;
DJ.AddEdge(u,v,d);
DJ.AddEdge(v,u,d);
}
DJ.dijkstra();
printf("%d\n",DJ.d[n-1]);
}
return 0;
}
分享到:
相关推荐
最短路(HDU-2544).rar
HDU最全ac代码
最短路径 贪心实现代码 hdu 最短路 dijkstra 算法
NULL 博文链接:https://128kj.iteye.com/blog/1716470
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
HDU1059的代码
杭电ACMhdu1163
hdu1001解题报告
hdu 1574 passed sorce
hdu 1005.比较简单的一道题,有兴趣的可以看看。
HDU的一题........HDU DP动态规
hdu acm 教案 搜索入门 hdu acm 教案 搜索入门
hdu2101AC代码
hdu ACM 各种排序
搜索 dfs 解题代码 hdu1241
hdu 5007 Post Robot 字符串枚举。 暴力一下就可以了。
hdu acm 教案 动态规划(1) hdu acm 教案 动态规划(1)
ACM HDU题目分类,我自己总结的大概只有十来个吧
hdu 1166线段树代码
一个十分简单的程序,能够ac杭电hdu的第2050题,无注释,简单明了