1 solutions

  • 0
    @ 2024-12-5 18:19:40

    C++ :

    #include<bits/stdc++.h>
    using namespace std;
    #define endl '\n'
    #define fi first
    #define se second
    #define pii pair<int,int>
    #define pb push_back
    int n,m,a,b,v[100005],vis[100005];
    vector<int>g[100005];
    int main(){
    	cin>>n>>m>>a>>b;
    	for(int i=0;i<n;i++) cin>>v[i];
    	while(m--){
    		int x,y;
    		cin>>x>>y;
    		g[x].pb(y);//有向边
    	}
        //广搜
    	queue<pii>q;
    	q.push({a,0});
    	while(!q.empty()){
    		int x=q.front().fi;
    		int y=q.front().se;
    		if(x==b){//到达终点
    			cout<<y+v[b]-v[a];
    			return 0;
    		}
    		q.pop();
    		for(auto i:g[x]){
    			if(!vis[i]){
    				vis[i]=1;
    				q.push({i,y+1});
    			}
    		}
    	}
    	cout<<"No solution";//无解
        return 0;
    }
    
    • 1

    Information

    ID
    9174
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By