HDU 2544 最短路(简单Dijkstra)
2014-07-11 20:38
351 查看
HDU 2544 最短路(简单Dijkstra)
http://acm.hdu.edu.cn/showproblem.php?pid=2544
题意:
给你一个N个顶点的无向图,现在要你输出从1号点到N号点的最短距离.
分析:
直接Dijkstra模板,不谈了.
AC代码:
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 最短路——简单的模板题 dijkstra
- HDU 2544 最短路——简单的模板题 dijkstra
- HDU 2544 最短路 dijkstra
- hdu 2544 最短路(Dijkstra 备忘)
- Hdu 2544 最短路 (Dijkstra+SPFA+Floyd模板)
- HDU_2544——最短路,Dijkstra模版
- 无向图邻接表怎么建,求指点!! HDU 2544 最短路(Dijkstra、结题报告 精简版!)
- 简单dijkstra http://acm.hdu.edu.cn/showproblem.php?pid=2544
- (重刷)HDU 1874 畅通工程续 + HDU 2544 最短路 最短路水题,dijkstra解法。
- hdu 2544 最短路(Dijkstra实现)
- HDU 2544 最短路【dijkstra+floyd+spfa+bellman-ford】
- 无向图邻接表怎么建,求指点!! HDU 2544 最短路(Dijkstra、结题报告 精简版!)
- hdu 2544 dijkstra最短路
- HDU 2544 Dijkstra 最短路
- HDU 2544 最短路 dijkstra floyd
- 【hdu 2544】 最短路 (dijkstra 写的第一个最短路,也是dijkstra算法,纪念一下)
- HDU 2544 Dijkstra 最短路
- hdu 2544最短路dijkstra
- (重刷)HDU 1874 畅通工程续 + HDU 2544 最短路 最短路水题,dijkstra解法。
- hdu 2544 最短路(简单的Floyd)