#include <stdio.h>
#include <iostream>
#include <vector>
#include <map>
#include <queue>
using namespace std;
 
vector<pair<intint>> graph[510];
map<pair<intint>int> not_go;
vector<int> min_track[510];
int d[510];
int V, E, start, arrive;
 
void find_min_track(int start)
{
    priority_queue<pair<intint>vector<pair<intint>>, greater<pair<intint>>> pq;
    pq.push({0, start});
    d[start] = 0;
    
    while(!pq.empty())
    {
        int now = pq.top().second;
        int nowCost = pq.top().first;
        pq.pop();
        
        if(nowCost != d[now])
        {
            continue;    
        }
        
        for(int i = 0; i < graph[now].size(); i++)
        {
            int next = graph[now][i].first;
            int nextCost = graph[now][i].second;
            
            if(nowCost + nextCost < d[next])
            {
                d[next] = nowCost + nextCost;
                pq.push({d[next], next});
                
                min_track[next].clear();
                min_track[next].push_back(now);
            }
            else if(nowCost + nextCost == d[next])
            {
                min_track[next].push_back(now);
            }
        }
    }
}
 
void find_semi_min_track(int start)
{
    priority_queue<pair<intint>vector<pair<intint>>, greater<pair<intint>>> pq;
    pq.push({0, start});
    d[start] = 0;
    
    while(!pq.empty())
    {
        int now = pq.top().second;
        int nowCost = pq.top().first;
        pq.pop();
        
        if(nowCost != d[now])
        {
            continue;    
        }
        
        for(int i = 0; i < graph[now].size(); i++)
        {
            int next = graph[now][i].first;
            int nextCost = graph[now][i].second;
            
            if(not_go[{now, next}] == 1)
            {
                continue;
            }
            
            if(nowCost + nextCost < d[next])
            {
                d[next] = nowCost + nextCost;
                pq.push({d[next], next});
            }
        }
    }
}
 
void del_min_track(int node)
{
    if(node == start)
    {
        return;
    }
    
    for(int i = 0; i < min_track[node].size(); i++)
    {
        not_go[{min_track[node][i], node}] = 1;
        
        del_min_track(min_track[node][i]);
    }
}
 
int main(void)
{
//    freopen("B5719_input.txt", "r", stdin);
    
    while(1)
    {
        cin >> V >> E;
        
        if(V == 0 && E == 0)
        {
            break;
        }
        
        cin >> start >> arrive;
        
        for(int i = 0; i < V; i++)
        {
            d[i] = 199999999;
            graph[i].clear();
            min_track[i].clear();
        }
        not_go.clear();
    
        for(int i = 1; i <= E; i++)
        {
            int from, to, weight;
            cin >> from >> to >> weight;
            
            graph[from].push_back({to, weight});
        }
        
        find_min_track(start);
        del_min_track(arrive);
        
        for(int i = 0; i < V; i++)
        {
            d[i] = 199999999;
        }
        
        find_semi_min_track(start);
        
        if(d[arrive] == 199999999)
        {
            cout << "-1" << "\n";
        }
        else
        {
            cout << d[arrive] << "\n";
        }
    }
    
    return 0;
}
cs

+ Recent posts