zoukankan      html  css  js  c++  java
  • BZOJ 1922: [Sdoi2010]大陆争霸 Dijkstra

    Code: 

    #include <queue>  
    #include <vector>   
    #include <cstdio> 
    #include <cstring>  
    #include <algorithm>   
    #define N 3004 
    #define M 70004 
    #define ll long long  
    #define setIO(s) freopen(s".in","r",stdin) 
    using namespace std;    
    int edges,n,m; 
    vector<int>G[N];    
    ll arrive[N],into[N],d[N];     
    int hd[N],to[M<<1],nex[M<<1],val[M<<1],deg[N],done[N];         
    void addedge(int u,int v,int c) 
    {
        nex[++edges]=hd[u],hd[u]=edges,to[edges]=v,val[edges]=c;   
    }
    struct Node 
    { 
        int u;    
        ll dis; 
        Node(int u=0,ll dis=0):u(u),dis(dis){}    
        bool operator<(Node b) const 
        {
            return b.dis<dis;   
        }
    }; 
    priority_queue<Node>q;       
    void Dijkstra() 
    { 
        int s = 1;    
        memset(d,0x3f,sizeof(d));  
        memset(arrive,0x3f,sizeof(arrive));   
        d[s]=arrive[s]=into[s]=deg[s]=0, q.push(Node(s,0));   
        for(;!q.empty();) 
        {
            Node e=q.top(); q.pop(); 
            int u=e.u; 
            if(done[u]) continue;  
            done[u]=1;   
            for(int i=hd[u];i;i=nex[i]) 
            {
                int v=to[i]; 
                if(arrive[v]>d[u]+val[i]) 
                {
                    arrive[v]=d[u]+val[i];  
                    if(deg[v]==0) 
                    {
                        d[v]=max(into[v], arrive[v]);    
                        q.push(Node(v, d[v]));     
                    }             
                }
            }   
            for(int i=0;i<G[u].size();++i) 
            {
                int v=G[u][i]; 
                --deg[v];   
                into[v]=max(into[v], d[u]);    
                if(deg[v]==0) 
                {
                    d[v]=max(into[v], arrive[v]);    
                    q.push(Node(v, d[v]));   
                }
            }
        }
    }
    int main() 
    {  
        int i,j; 
        // setIO("input"); 
        scanf("%d%d",&n,&m);   
        for(i=1;i<=m;++i) 
        {
            int a,b,c; 
            scanf("%d%d%d",&a,&b,&c),addedge(a,b,c);   
        } 
        for(i=1;i<=n;++i) 
        {
            int t,k; 
            scanf("%d",&t); 
            for(j=1;j<=t;++j) 
            {
                ++deg[i],scanf("%d",&k),G[k].push_back(i);      
            }
        } 
        Dijkstra();   
        printf("%lld
    ",d[n]);      
        return 0;   
    }
    

      

  • 相关阅读:
    想你了
    新华都总裁兼CEO唐骏演讲
    中国99%的白领以及他们的家庭即将面临破产
    你的英语水平就可以达到跟美国人交流的水平啦
    经验语录
    荒谬的加息传言
    人生格言
    不要为油荒找借口
    房地产调控失利 三次调控势不可免
    假设你的月收入2000元,你应该这样用
  • 原文地址:https://www.cnblogs.com/guangheli/p/11546691.html
Copyright © 2011-2022 走看看