#include <iostream>
#include <vector>
#include <queue>
#include <utility>
using namespace std;

int V,E,r;
const int Inf = 10000*100000+100;
int C[100010];
int main(){
	while(cin >> V >> E >> r && V >0){
		vector< vector< pair<int,int> > > v(V+10);
		for(int i=0;i<E;i++){
			int s,t,d;
			cin >> s >> t >> d;
			v[s].push_back(make_pair(d,t));
		}
		priority_queue<pair <int,int> > P;
		P.push(make_pair(0,r));
		for(int i=0;i<V;i++){
			C[i] = Inf;
		}
		C[r] = 0;
		while(!P.empty()){
			pair<int,int> t = P.top();
			int s = t.second;
			P.pop();
			if(C[s] < t.first) continue;
			for(int i=0;i<v[s].size();i++){
				pair<int,int> e = v[s][i];
				if(C[e.second] > C[s] + e.first){
					C[e.second] = C[s] + e.first;
					P.push(make_pair(-C[e.second],e.second));
				}
			}
		}
		for(int i=0;i<V;i++){
			if(C[i] < Inf) cout << C[i] << endl;
			else cout << "INF" << endl;
		}
	}
}