• UVALive 6885 spfa


    #include<stdio.h>
    #include<algorithm>
    #include<string.h>
    #include<vector>
    #include<queue>
    using namespace std;
    const int inf=0x7fffffff;
    const int N=10000+5;
    
    int p,t,f1[N],f2[N],dis1[N],dis2[N];
    struct
    {
        int u,v,c;
    }edge[250000+5];
    vector<int>g[10000+5];
    
    void spfa1()
    {
        queue<int>q;
        int i,u,v,k;
        memset(f1,0,sizeof(f1));
        for(i=0;i<p;i++)
            dis1[i]=inf;
        dis1[0]=0;
        q.push(0);
        while(!q.empty())
        {
            u=q.front();
            q.pop();
            f1[u]=0;
            for(i=0;i<g[u].size();i++)
            {
                k=g[u][i];
                if(edge[k].u==u)
                {
                    v=edge[k].v;
                    if(dis1[u]+edge[k].c<=dis1[v])
                    {
                        dis1[v]=dis1[u]+edge[k].c;
                        if(f1[v]==0)
                        {
                            f1[v]=1;
                            q.push(v);
                        }
                    }
                }
                else if(edge[k].v==u)
                {
                    v=edge[k].u;
                    if(dis1[u]+edge[k].c<=dis1[v])
                    {
                        dis1[v]=dis1[u]+edge[k].c;
                        if(f1[v]==0)
                        {
                            f1[v]=1;
                            q.push(v);
                        }
                    }
                }
            }
        }
    }
    
    void spfa2()
    {
        queue<int>q;
        int i,u,v,k;
        memset(f2,0,sizeof(f2));
        for(i=0;i<p;i++)
            dis2[i]=inf;
        dis2[p-1]=0;
        q.push(p-1);
        while(!q.empty())
        {
            u=q.front();q.pop();
            f2[u]=0;
            for(i=0;i<g[u].size();i++)
            {
                k=g[u][i];
                if(edge[k].u==u)
                {
                    v=edge[k].v;
                    if(dis2[u]+edge[k].c<=dis2[v])
                    {
                        dis2[v]=dis2[u]+edge[k].c;
                        if(f2[v]==0)
                        {
                            f2[v]=1;
                            q.push(v);
                        }
                    }
                }
                else if(edge[k].v==u)
                {
                    v=edge[k].u;
                    if(dis2[u]+edge[k].c<=dis2[v])
                    {
                        dis2[v]=dis2[u]+edge[k].c;
                        if(f2[v]==0)
                        {
                            f2[v]=1;
                            q.push(v);
                        }
                    }
                }
            }
        }
    }
    
    int main()
    {
        int i,dis,ans,u,v,c;
        while(~scanf("%d%d",&p,&t))
        {
            for(i=0;i<p;i++)
                g[i].clear();
    
            for(i=0;i<t;i++)
            {
                scanf("%d%d%d",&u,&v,&c);
                edge[i].u=u;
                edge[i].v=v;
                edge[i].c=c;
                g[u].push_back(i);
                g[v].push_back(i);
            }
            spfa1();
            spfa2();
            dis=dis1[p-1];
            ans=0;
            //for(i=0;i<p;i++) printf("%d ",dis1[i]);
            for(i=0;i<t;i++)
            {
                u=edge[i].u;
                v=edge[i].v;
                c=edge[i].c;
                if(dis1[u]+dis2[v]+c==dis||dis1[v]+dis2[u]+c==dis)
                    ans+=c;
            }
            printf("%d
    ",2*ans);
        }
        return 0;
    }

    版权声明:本文为博主原创文章,未经博主允许不得转载。http://xiang578.top/

  • 相关阅读:
    for...in 循环对象原型链问题
    移动端表单禁用软键盘
    将一个普通数组映射为对象数组
    npm install命令详解
    Elasticsearch High Level REST Client
    Guava: Google Core Libraries for Java
    Java Interview Programming Questions
    2017 OWASP TOP 10
    17 Popular Java Frameworks in 2018
    10 Popular PHP frameworks in 2019
  • 原文地址:https://www.cnblogs.com/xryz/p/4847761.html
Copyright © 2020-2023  润新知