• POJ 3255 Roadblocks 次短路


    和Dijksta求最短路一样,只是要维护两个数组:最短路d1,次短路d2。然后更新的时候注意细节。

    //#pragma comment(linker, "/STACK:1024000000,1024000000")
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    #include<iostream>
    #include<sstream>
    #include<cmath>
    #include<climits>
    #include<string>
    #include<map>
    #include<queue>
    #include<vector>
    #include<stack>
    #include<set>
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    typedef pair<int,int> pii;
    #define pb(a) push(a)
    #define INF 0x1f1f1f1f
    #define lson idx<<1,l,mid
    #define rson idx<<1|1,mid+1,r
    #define PI  3.1415926535898
    template<class T> T min(const T& a,const T& b,const T& c) {
        return min(min(a,b),min(a,c));
    }
    template<class T> T max(const T& a,const T& b,const T& c) {
        return max(max(a,b),max(a,c));
    }
    void debug() {
    #ifdef ONLINE_JUDGE
    #else
    
        freopen("in.txt","r",stdin);
        //freopen("d:\out1.txt","w",stdout);
    #endif
    }
    int getch() {
        int ch;
        while((ch=getchar())!=EOF) {
            if(ch!=' '&&ch!='
    ')return ch;
        }
        return EOF;
    }
    
    struct HeapNode
    {
        int d,u;
        bool operator < (const HeapNode &ant) const
        {
            return ant.d<d;
        }
    };
    
    struct Edge
    {
        int from,to,dist;
    };
    
    const int maxn=5005;
    
    vector<int> g[maxn];
    vector<Edge> edge;
    int n;
    int d1[maxn],d2[maxn];
    
    void init()
    {
        for(int i=1;i<=n;i++)
            g[i].clear();
        edge.clear();
    }
    
    void add(int u,int v,int w)
    {
        Edge e=(Edge){u,v,w};
        edge.push_back(e);
        g[u].push_back(edge.size()-1);
    }
    void solve(int s)
    {
        for(int i=1;i<=n;i++)
            d1[i]=d2[i]=INF;
        priority_queue<HeapNode> q;
        d1[s]=0;
        q.push((HeapNode){0,s});
        while(!q.empty())
        {
            HeapNode x=q.top(); q.pop();
            if(x.d>d2[x.u])continue;
            int u=x.u;
            for(int i=0;i<g[u].size();i++)
            {
                Edge &e=edge[g[u][i]];
                int v=e.to;
                int d=x.d;
                if(e.dist+d<d1[v])
                {
                    d2[v]=d1[v];
                    d1[v]=d+e.dist;
                    q.push((HeapNode){d1[v],v});
                }else if(e.dist+d<d2[v]&&e.dist+d!=d1[v])
                {
                    d2[v]=d+e.dist;
                    q.push((HeapNode){d2[v],v});
                }
            }
        }
    }
    int main()
    {
        int m;
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            init();
            for(int i=1;i<=m;i++)
            {
                int u,v,w;
                scanf("%d%d%d",&u,&v,&w);
                add(u,v,w);
                add(v,u,w);
            }
    
            solve(1);
            printf("%d
    ",d2[n]);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    uva 442 Matrix Chain Multiplication
    结对编程项目之队友代码分析
    [转] 为什么要使用NoSQL
    Compare Linq2Sql with NHibernate
    使用linq2sql 的DetailView 如何保存多对多关系
    工作流入门
    XML字段的用处
    DesignTimeResourceProviderFactory 不给力啊
    如何给XMLDatasource做分页和排序
    ORM的烦恼
  • 原文地址:https://www.cnblogs.com/BMan/p/3647537.html
Copyright © 2020-2023  润新知