zoukankan      html  css  js  c++  java
  • C++-POJ2159-Candies[spfa][栈优化][邻接表]

     1 #include <cstdio>
     2 const int M=150010,N=30010;
     3 struct edge{int v,w,next;}e[M];int head[N],cnt;
     4 void add(int u,int v,int w){e[++cnt].v=v,e[cnt].w=w,e[cnt].next=head[u],head[u]=cnt;}
     5 int d[N],vis[N],stack[M],top;
     6 int spfa(int n){
     7     for(int i=1;i<=n;i++)vis[i]=0,d[i]=2147483647;
     8     top=0,stack[++top]=1,vis[1]=1,d[1]=0;
     9     for(int u;top>=0;){
    10         u=stack[top--],vis[u]=0;
    11         for(int i=head[u],v,w;v=e[i].v,w=e[i].w,i;i=e[i].next)
    12             if(d[v]>d[u]+w){
    13                 d[v]=d[u]+w;
    14                 if(!vis[v])stack[++top]=v,vis[v]=1;
    15             }
    16     }
    17     return d[n];
    18 }
    19 int main(){    
    20     for(int n,m,u,v,w;scanf("%d%d",&n,&m)!=EOF;){
    21         for(int i=1;i<=n;i++)head[i]=0;
    22         while(m--)scanf("%d%d%d",&u,&v,&w),add(u,v,w);
    23         printf("%d
    ",spfa(n));        
    24     }
    25     return 0;
    26 }
    ~~Jason_liu O(∩_∩)O
  • 相关阅读:
    学习进度笔记06
    学习进度笔记05
    学习进度笔记04
    学习进度笔记03
    学习进度笔记02
    周总结13
    周总结12
    周总结11
    人月神话阅读笔记3
    第一阶段冲刺10
  • 原文地址:https://www.cnblogs.com/JasonCow/p/12349926.html
Copyright © 2011-2022 走看看