POJ_2387_Til the Cows Come Home(USACO 2004 November)_最短路径
2016-06-10 18:17
513 查看
这是最简单的最短路径裸题之一…建议初学者做或者用来测速…
题目的意思是给一个有 T 条双向边、N 个点的图,问从1号结点到 N 号结点最短路径长度…赤果果的一道题…
用SPFA解决:
题目的意思是给一个有 T 条双向边、N 个点的图,问从1号结点到 N 号结点最短路径长度…赤果果的一道题…
用SPFA解决:
#include<iostream> #include<cstdio> #include<queue> #include<vector> #include<cstring> using namespace std; const int N =1050; int t,n; vector <int> E ,W ; int d ; bool vd ; void spfa(int s) { memset(d,0x7f,sizeof(d)); memset(vd,false,sizeof(vd)); queue <int> q; q.push(s); vd[s]=true; d[s]=0; while (q.size()) { int u=q.front(); vd[u]=false; q.pop(); for (int i=0;i<E[u].size();i++) { int v=E[u][i]; int w=W[u][i]; if (d[u]+w<d[v]) { d[v]=w+d[u]; if (vd[v]==false) { q.push(v); vd[v]=true; } } } } } int main() { int a,b,c; scanf ("%d%d",&t,&n); for (int i=1;i<=t;i++) { scanf ("%d%d%d",&a,&b,&c);//储存为双向边 E[a].push_back(b); E[b].push_back(a); W[a].push_back(c); W[b].push_back(c); } spfa (1); printf ("%d\n",d ); return 0; }
相关文章推荐
- 详解图的应用(最小生成树、拓扑排序、关键路径、最短路径)
- python编写的最短路径算法
- 【算法】最短路径之A*搜索
- Dijkstra和floyd——求单源点最短路径
- 初学图论-Dijkstra单源最短路径算法基于优先级队列(Priority Queue)的实现
- 初学ACM - 组合数学基础题目PKU 1833
- POJ ACM 1001
- POJ ACM 1002
- 1611:The Suspects
- POJ1089 区间合并
- POJ 2159 Ancient Cipher
- POJ 2635 The Embarrassed Cryptographe
- POJ 3292 Semi-prime H-numbers
- POJ 2773 HAPPY 2006
- POJ 3090 Visible Lattice Points
- USACO 3.2.2:Stringsobits
- POJ-2409-Let it Bead&&NYOJ-280-LK的项链
- POJ-1695-Magazine Delivery-dp
- POJ1523 SPF dfs
- POJ-1001 求高精度幂-大数乘法系列