PKU-#2387 Til the Cows Come Home(Dijkstra+vector+优先队列)
来源:互联网 发布:linux批量管理工具 编辑:程序博客网 时间:2024/06/06 01:42
题目大意:输入边数和顶点数以及路径对应关系和权值,求小女孩从最后一个结点回到起点的最短路径。
解题思路:很明显的最短路的题,写了好几道基础的写法,这里换成了另外一种vector+邻接表+优先队列的模板写法,算法的核心部分前面几题都给出了详细注释,这里不再赘述,详见code。
题目来源:http://poj.org/problem?id=2387
code:
#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <vector>using namespace std;const int MAXN = 1000+10;const int INF = 0x3fffffff;int t,n,m,a,b,w;struct edge{ int from,to,dist; };struct heapnode{ //优先队列结点 int d,u; bool operator < (const heapnode& rhs) const{ return d>rhs.d; }};vector<edge> edges; //边列表vector<int> g[MAXN]; //每个结点出发的边编号bool done[MAXN]; //是否标记int d[MAXN]; //s到各个点的距离int p[MAXN]; //最短中的上一条边void init(int 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(int s){ priority_queue<heapnode> q; for(int i=0;i<=n;i++) d[i]=INF; d[s]=0; memset(done,0,sizeof(done)); q.push((heapnode){0,s}); 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; p[e.to]=g[u][i]; q.push((heapnode){d[e.to],e.to}); } } }}int main(){ while(scanf("%d%d",&t,&n)!=EOF){ init(n); while(t--){ scanf("%d%d%d",&a,&b,&w); addedge(a,b,w); addedge(b,a,w); } dijkstra(1); printf("%d\n",d[n]); } return 0;}
0 0
- PKU-#2387 Til the Cows Come Home(Dijkstra+vector+优先队列)
- POJ 2387-Til the Cows Come Home(最短路Dijkstra+优先队列)
- poj-2387-Til the Cows Come Home(Dijkstra优先队列实现)
- Til the Cows Come Home (Dijkstra)
- POJ 2387 Til the Cows Come Home(Dijkstra判重边)
- POJ 2387 Til the Cows Come Home(Dijkstra)
- poj 2387 Til the Cows Come Home(Dijkstra)
- POJ - 2387 Til the Cows Come Home(Dijkstra)
- POJ 2387 Til the Cows Come Home(Dijkstra算法)
- POJ 2387 Til the Cows Come Home(Dijkstra算法)
- POJ 2387 Til the Cows Come Home(迪杰斯特拉/优先队列/最短路)
- POJ2387 Til the Cows Come Home【Dijkstra】
- POJ2387----Til the Cows Come Home(dijkstra)
- Til the Cows Come Home (dijkstra)
- poj 2387-Til the Cows Come Home-dijkstra
- POJ 2387 Til the Cows Come Home 最短路-Dijkstra
- POJ 2387 Til the Cows Come Home (Dijkstra)
- POJ 2387 Til the Cows Come Home(Dijkstra简单题)
- 【编程珠玑——01】位向量
- 主键和外键
- wxPython学习笔记(四)
- linux驱动学习笔记(linux驱动头文件说明)
- 一个简单的测试程序健壮性的c程序
- PKU-#2387 Til the Cows Come Home(Dijkstra+vector+优先队列)
- Python正则表达式指南
- 第五周训练赛1--C题
- 4.JavaWeb基础 page指令和Tomcat连接数据库
- 剑指Offer30最小的k个数
- Mysql数据库修改大小写敏感性问题
- 成为优秀程序员的十个有效方法
- 嵌入式linux开发
- [JZOJ 1281]旅行