zoukankan      html  css  js  c++  java
  • poj Candies

    http://poj.org/problem?id=3159

     1 #include<cstdio>
     2 #include<queue>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<cmath>
     6 #define max1 30005
     7 #define max2 150005
     8 using namespace std;
     9 int head[max1],next[max2];
    10 int dis[max1];
    11 bool vis[max1];
    12 const int inf=1<<23;
    13 struct node
    14 {
    15     int u,v;
    16     int c;
    17     node(){}
    18     node(int u,int v,int c):u(u),v(v),c(c){}
    19 }p[max2];
    20 
    21 struct node1
    22 {
    23     int v,c;
    24     node1(){}
    25     node1(int v,int c):v(v),c(c){}
    26     bool operator <(const node1 &a)const
    27     {
    28         return c>a.c;
    29     }
    30 };
    31 int e,n,m;
    32 void addnode(int u,int v,int c)
    33 {
    34     p[e]=node(u,v,c);
    35     next[e]=head[u];
    36     head[u]=e++;
    37 }
    38 
    39 bool relax(int u,int v,int c)
    40 {
    41     if(dis[v]>dis[u]+c)
    42     {
    43         dis[v]=dis[u]+c;
    44         return true;
    45     }
    46     return false;
    47 }
    48 
    49 void inti()
    50 {
    51     memset(head,-1,sizeof(head));
    52     memset(next,-1,sizeof(next));
    53     e=0;
    54     for(int i=0; i<m; i++)
    55     {
    56         int u,v,c;
    57         scanf("%d%d%d",&u,&v,&c);
    58         addnode(u,v,c);
    59     }
    60 }
    61 
    62 void dij(int src)
    63 {
    64     memset(vis,0,sizeof(vis));
    65     for(int i=1; i<=n; i++) dis[i]=inf;
    66     dis[src]=0;
    67     priority_queue<node1>que;
    68     que.push(node1(src,dis[src]));
    69     for(int i=0; i<n; i++)
    70     {
    71         while(!que.empty()&&vis[que.top().v])
    72             que.pop();
    73         if(que.empty()) break;
    74         node1 pre=que.top(); que.pop();
    75         vis[pre.v]=true;
    76         for(int j=head[pre.v]; j+1; j=next[j])
    77         {
    78             if(relax(pre.v,p[j].v,p[j].c)&&!vis[p[j].v])
    79                 que.push(node1(p[j].v,dis[p[j].v]));
    80         }
    81     }
    82 }
    83 
    84 int main()
    85 {
    86     while(scanf("%d%d",&n,&m)!=EOF){
    87         inti();
    88         dij(1);
    89         printf("%d
    ",dis[n]);
    90     }
    91     return 0;
    92 }
    View Code
  • 相关阅读:
    北京大学计算机系2009应试硕士生上机考试(DF)
    我的考研2010(一)
    这张容易看懂...
    关于招商银行信用卡的若干事宜
    20 years
    C/C++中关于qsort的使用
    有道破题~~
    POJ 4010 2011
    有道难题练习赛 Sibonacci
    北京大学计算机系2009应试硕士生上机考试(AC)
  • 原文地址:https://www.cnblogs.com/fanminghui/p/3437873.html
Copyright © 2011-2022 走看看