hdu 2544 最短路裸题 SPFA
2016-11-17 23:49
316 查看
#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <cmath> #include <queue> #include <vector> using namespace std; #define N 100010 #define INF 0x3f3f3f3f int n,m,q; struct Edge{ int next,to,value; }edge ; int head ,tot; bool exist ; int dis ; queue<int> Q; inline int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();} return x*f; } void Addedge(int u,int v,int w) { tot++;edge[tot].next=head[u];edge[tot].to=v;edge[tot].value=w;head[u]=tot; tot++;edge[tot].next=head[v];edge[tot].to=u;edge[tot].value=w;head[v]=tot; } void SPFA() { memset(dis,INF,sizeof(dis)); dis[1]=0;exist[1]=1; Q.push(1); while(!Q.empty()) { int now=Q.front();Q.pop(); exist[now]=false; for(int i=head[now];i;i=edge[i].next) { int v=edge[i].to; if(dis[v]>dis[now]+edge[i].value) { dis[v]=dis[now]+edge[i].value; if(!exist[v]) { exist[v]=true; Q.push(v); } } } } } int main() { while(scanf("%d%d",&n,&m)!=EOF) { if(n==0&&m==0) break; tot=0; memset(head,0,sizeof(head)); for(int i=1;i<=m;i++) { int x=read(),y=read(),z=read(); Addedge(x,y,z); } SPFA(); cout<<dis <<endl; } return 0; }
相关文章推荐
- HDu 2544 最短路【dijkstra & floyed & SPFA 】
- Spfa 最短路 HDU 2544
- hdu 2544 最短路(最短路径)(flody、dij、dij+priority queue、bellman、spfa)
- HDU 2544 最短路 (最短路,spfa)
- HDU 2544 最短路(Floyd + Dijkstra + SPFA + 使用链式前向星优化的SPFA + 浅谈最短路)
- hdu 2544 最短路 spfa
- HDU 2544 最短路(dijkstra or Floyd or bellman or spfa )
- HDU 2544 最短路 SPFA 邻接表 模板
- 【HDU - 2544 最短路】 最短路 dijkstra,spfa
- hdu-2544-最短路(Dijkstra + Dijkstra优先队列 + Bellman-ford + SPFA +Floyd) 纯模板题
- hdu 2544 最短路 spfa模板题
- HDU 2544 最短路【SPFA】
- HDU 2544-最短路(最短路spfa)
- HDU - 2544 - 最短路 (最基础单源最短路问题!!dijkstra+floyd+SPFA)
- hdu 2544 最短路-spfa
- hdu 2544 最短路 (spfa)
- HDU 2544-最短路(SPFA,Dijkstra,Floyd)
- HDU2544---最短路(dijkstra&&floyd&&spfa)
- HDU 2544 最短路(dijkstra,Floyd,spfa)
- HDU 2544 最短路【SPFA + SLF】