zoukankan      html  css  js  c++  java
  • luogu_1346 电车

    #include <iostream>
    #include <cstdio>
    #include <queue>
    using namespace std;
    const int maxn=110;
    const int INF=1<<30;
    struct edge{int v,t;};
    int n,s,t,d[maxn];
    queue<int> q;
    vector<edge> G[maxn];
    bool vis[maxn];
    
    void spfa(){
    	for(int i=1;i<=n;i++)d[i]=INF;
    	q.push(s); d[s]=0; vis[s]=1;
    	while(!q.empty()){
    		int u=q.front(); q.pop(); vis[u]=0;
    		for(int i=0;i<G[u].size();i++){
    			int v=G[u][i].v,k=G[u][i].t;
    			if(d[v]>d[u]+k){
    				d[v]=d[u]+k;
    				if(!vis[v]){q.push(v); vis[v]=1;}
    			}
    		}
    	}
    }
    
    int main(){
    	scanf("%d%d%d",&n,&s,&t);
    	for(int i=1;i<=n;i++){
    		int m;
    		scanf("%d",&m);
    		for(int j=1;j<=m;j++){
    			int v;
    			scanf("%d",&v);
    			if(j==1)G[i].push_back((edge){v,0});
    			else G[i].push_back((edge){v,1});
    		}
    	}
    	spfa();
    	if(d[t]==INF)puts("-1");
    	else printf("%d
    ",d[t]);
    	return 0;
    }
    

      

  • 相关阅读:
    butter
    医院设置
    NOIP 2000 进制转换
    图的M 着色问题
    闭合区域面积统计
    字符序列
    装载问题
    n皇后问题
    跳马问题
    数独
  • 原文地址:https://www.cnblogs.com/codetogether/p/7571383.html
Copyright © 2011-2022 走看看