• Candies POJ


    // 
    #include<iostream>
    #include<cstring>
    #include<queue>
    #include<stack>
    #include<stdio.h>
    using namespace std;
    const int INF=0x3f3f3f3f;
    const int N=30010,M=150010;
    int h[N],e[M],ne[M],w[M],idx;
    int st[N];
    int dist[N];
    int n,m;
    bool vis[N];
    int Scan()
    {
        int res = 0, ch, flag = 0;
        if((ch = getchar()) == '-')             //判断正负
            flag = 1;
        else if(ch >= '0' && ch <= '9')           //得到完整的数
            res = ch - '0';
        while((ch = getchar()) >= '0' && ch <= '9' )
            res = res * 10 + ch - '0';
        return flag ? -res : res;
    }
    void add(int a,int b,int c)
    {
        e[idx]=b;
        w[idx]=c;
        ne[idx]=h[a];
        h[a]=idx++;
    }
    void spfa(int start)
    {
        memset(vis,0,sizeof vis);
        for(int i=0;i<=n;i++)
            dist[i]=INF;
        stack<int>s;
        s.push(start);
        dist[start]=0;
        vis[start]=1;
        while(s.size())
        {
            int u=s.top();
            s.pop();
            vis[u]=0;
            for(int j=h[u];j!=-1;j=ne[j])
            {
                int v=e[j];
                int w1=w[j];
                if(dist[v]>dist[u]+w1)
                {
                    dist[v]=dist[u]+w1;
                    if(vis[v]==0)
                    {
                        s.push(v);
                        vis[v]=1;
                    }
                }
            }
        }
        printf("%d
    ",dist[n]);
    }
    int main()
    {
        while(scanf("%d%d", &n, &m)!=EOF)
        {
            idx=0;
            memset(h,-1,sizeof h);
            for(int i=0;i<m;i++)
            {
                int x,y,w;
                scanf("%d%d%d",&x,&y,&w);
                add(x,y,w);
            }
            spfa(1);
        }
        return 0;
    }
  • 相关阅读:
    django http请求request详解
    HTTP协议向服务器传参
    股票交易费用及复利计算公式
    scrapy初步使用
    通过 multiprocessing Pool 线程池加速爬虫的处理
    通过 PIL 和 Python-tesseract 模拟登陆
    BeautifulSoup
    xpath
    http 请求特殊字符
    HTTP cookies
  • 原文地址:https://www.cnblogs.com/QingyuYYYYY/p/12245519.html
Copyright © 2020-2023  润新知