1 solutions

  • 0
    @ 2024-12-10 23:10:28

    C++ :

    #include <cstdio>
    #include <cmath>
    #include <queue>
    #include <cstring>
    #include <climits>
    #include <algorithm>
    using namespace std;
    typedef long long LL;
    int N=0,M=0;
    LL Dis[100010]={0};
    int len1[100010]={0},len2[100010]={0};
    
    class data{
    	public:
    		int to,next,v;
    }E[400010];
    int line[100010]={0},tot=0;
    inline void Insert(int x,int y,int v){
       E[++tot].to=y; E[tot].v=v; E[tot].next=line[x]; line[x]=tot;
    }
    
    struct node{
    	int pos;
    	bool operator < (const node& b) const {
    		Dis[pos] < Dis[b.pos];
    	}
    }now;
    priority_queue<node>Q;
    
    bool flag[100010]={0};
    int  head=1,tail=0;
    inline int F(int x){ return ((x%N)+N)%N; }
    inline bool empty(){ return head>tail; }
    
    int main(){
    	///freopen("gin.in","r",stdin);
    	//freopen("gin.out","w",stdout);
    
    	scanf("%d%d",&N,&M);
    	int t=0,a=0,b=0;
    	for(int i=1;i<=M;++i){
    		scanf("%d%d%d",&t,&a,&b);
    		if(t==1) Insert(a,b,0),Insert(b,a,0);
    		else if(t==2) Insert(a,b,1);
    		else if(t==3) Insert(b,a,0);
    		else if(t==4) Insert(b,a,1);
    		else if(t==5) Insert(a,b,0);
    	}
    	
    	for(int i=1;i<=N;++i){
    		now.pos=i; len1[i]++; flag[i]=true; Dis[i]=1; Q.push(now);
    	}
    	while(!Q.empty()){
    		int t=Q.top().pos; Q.pop(); flag[t]=false;
    		for(int i=line[t];i!=0;i=E[i].next){
    			int p=E[i].to;
    			if(Dis[t]+E[i].v>Dis[p]){
    				Dis[p]=Dis[t]+E[i].v;
    				len2[p]=len2[t]+1;
    				if(len2[p]>N) {
    					printf("-1\n"); exit(0);
    				}
    				if(!flag[p]){
    					len1[p]++;
    					if(len1[p]>N){ printf("-1\n"); exit(0); }
    					flag[p]=true;  now.pos=p; Q.push(now);//Q[F(++tail)]=p;
    				}
    			}
    		}
    	}
    	
    	LL Ans=0;
    	for(int i=1;i<=N;++i) Ans+=Dis[i];
    	printf("%lld",Ans);
    	return 0;
    }
    
    • 1

    Information

    ID
    592
    Time
    3000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By