zoukankan      html  css  js  c++  java
  • BZOJ 1922 大陆争霸

    神奇Dij
    不是很懂
    %hzwer代码

    #include <cstdio>
    #include <algorithm>
    #include <queue>
    
    using namespace std;
    
    int read(){
    	int x=0, f=1;char ch=getchar();
    	while(ch<'0' || ch>'9'){if(ch=='-')f=-f;ch=getchar();}
    	while(ch>='0' && ch<='9'){x=x*10+(ch-'0');ch=getchar();}
    	return x*f;
    }
    
    const int MAXN=3011;
    const int MAXM=70111;
    const int INF=1034567890;
    
    int N, M;
    
    struct Vert{
    	int FE;
    	int Deg;
    	int Dis, Wait;
    	bool Vis;
    } V[MAXN];
    
    struct Edge{
    	int x, y, next;
    	int l;
    } E[MAXM];
    
    int Ecnt=0;
    
    void addE(int a, int b, int c){
    	++Ecnt;
    	E[Ecnt].x=a;E[Ecnt].y=b;E[Ecnt].l=c;
    	E[Ecnt].next=V[a].FE;V[a].FE=Ecnt;
    }
    
    int P[MAXN][MAXN], Pcnt[MAXN];
    
    priority_queue< pair<int, int>, vector< pair<int, int> >, greater< pair<int, int> > > PQ;
    
    void Dij(){
    	
    	for(int i=1;i<=N;++i)
    		V[i].Dis=INF;
    	
    	PQ.push(make_pair(0, 1));
    	V[1].Dis=0;
    	
    	int at, di;
    	while(!PQ.empty()){
    		at=PQ.top().second;PQ.pop();
    		if(V[at].Vis)	continue;
    		V[at].Vis=true;
    		di=max(V[at].Dis, V[at].Wait);
    		for(int k=V[at].FE, to;k>0;k=E[k].next){
    			to=E[k].y;
    			if(di+E[k].l<V[to].Dis){
    				V[to].Dis=di+E[k].l;
    				if(V[to].Deg==0)
    					PQ.push(make_pair(max(V[to].Dis, V[to].Wait), to));
    			}
    		}
    		for(int i=1, to;i<=Pcnt[at];++i){
    			to=P[at][i];
    			--V[to].Deg;V[to].Wait=max(V[to].Wait, di);
    			if(V[to].Deg==0)
    				PQ.push(make_pair(max(V[to].Wait, V[to].Dis), to));
    		}
    	}
    }
    
    int main(){
    	
    	N=read();M=read();
    	for(int i=1, a, b, c;i<=M;++i){
    		a=read();b=read();c=read();
    		if(a==b)	continue;/**/
    		addE(a, b, c);
    	}
    	for(int i=1, c;i<=N;++i){
    		c=read();
    		V[i].Deg=c;
    		for(int j=1, p;j<=c;++j){
    			p=read();
    			P[p][++Pcnt[p]]=i;
    		}
    	}
    	Dij();
    	
    	printf("%d
    ", max(V[N].Dis, V[N].Wait));
    	
    	return 0;
    }
    
    /*
    6 6
    1 2 1
    1 4 3
    2 3 1
    2 5 2
    4 6 2
    5 3 2
    0
    0
    0
    1 3
    0
    2 3 5
    
    5
    
    */
    
  • 相关阅读:
    java入门第一篇:h5和CSS的基础学习及思考。
    Dubbo-入门到精通(一)
    跳表(skipList)的原理和java实现
    Springboot
    zookeeper 常用命令及znode简介
    Zookeeper 实现分布式锁服务
    Golang 基础
    ipython notebook 如何修改一开始打开的文件夹路径
    Loadrunner11配置Jdk,以及是否成功配置验证
    Redis学习
  • 原文地址:https://www.cnblogs.com/Pickupwin/p/9098789.html
Copyright © 2011-2022 走看看