제출 #1352720

#제출 시각아이디문제언어결과실행 시간메모리
1352720kokorooCurrents (EGOI25_currents)C++20
100 / 100
197 ms32604 KiB
#include<bits/stdc++.h>
//#include<atcoder/all>

using namespace std;
//using namespace atcoder;
#define rep(i,n) for(ll i=0; i<n; i++)
#define rrep(i,n) for(ll i=n-1; i>=0; i--)
#define print(a) cout<<a<<endl
typedef long long ll;
#define yn(flg) if(flg){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}
#define YN(flg) if(flg){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}
#define so(a) sort(a.begin(),a.end())
#define mp make_pair
#define vi vector<int>
#define vl vector<ll>
#define vs vector<string>
#define pb push_back
#define a2i(a,s) (ll)(a-s)
#define i2a(s,a) (char)(s+a)
#define ssize(a) a.size()
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<pair<ll,ll>,ll> P3;
typedef pair<pair<ll,ll>,pair<ll,ll>> P4;

typedef pair<ll, ll> Pll;
typedef pair<ll,Pll> Plll;
typedef pair<Pii, int> Piii;
const ll INF = 1000000000000000000;

template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;

}
using ull=unsigned long long;



vector<ll> a[200009];
vector<ll> b[200009];//反対の辺


int main(){
//入力

	cin.tie(0);
	ios::sync_with_stdio(0);

	ll n,m;
	cin>>n>>m;

	for(ll i=0;i<m;i++){
		ll A,B;
		cin>>A>>B;
		a[A].push_back(B);
		b[B].push_back(A);
	}

//0からの距離
	vector<ll> v(n+1,INF);
	priority_queue<Pll,vector<Pll>,greater<Pll> > que;
	que.push({0,0});
	v[0]=0;

	while(!que.empty()){
		ll cost=que.top().first,pos=que.top().second;
		que.pop();

		for(ll i=0;i<a[pos].size();i++){
			ll nx=a[pos][i];
			if(v[nx]!=INF)continue;
			v[nx]=cost+1;
			que.push({cost+1,nx});
		}
	}

//	for(ll i=0;i<n;i++)cout<<v[i]<<endl;

//n-1からの距離
	vector<ll> ans(n+1,INF);

	que.push({0,n-1});

	while(!que.empty()){
		ll cost=que.top().first,pos=que.top().second;
		que.pop();
//		cout<<pos<<endl;
		if(ans[pos]<=cost)continue;
		ans[pos]=cost;
		for(ll i=0;i<b[pos].size();i++){
			ll nx=b[pos][i];
			if(ans[nx]!=INF)continue;
			que.push({max(v[nx],cost+1),nx});
		}
	}

	for(ll i=0;i<n-1;i++)cout<<ans[i]<<" ";
	cout<<endl;

	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...