#include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
 
vector<pair<intint>> graph[1010];
vector<int> track[1010];
int d[1010];
int V, E;
int lineCnt;
 
void dijkstra(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});
                
                track[next].clear();
                track[next].push_back(now);
            }
        }
    }
}
 
int main(void)
{
//    freopen("B2211_input.txt", "r", stdin);
    
    cin >> V >> E;
    
    for(int i = 1; i <= V; i++)
    {
        d[i] = 199999999;
    }
 
    for(int i = 1; i <= E; i++)
    {
        int from, to, weight;
        cin >> from >> to >> weight;
        
        graph[from].push_back({to, weight});
        graph[to].push_back({from, weight});
    }
    
    dijkstra(1);
    
    for(int i = 1; i <= V; i++)
    {
        lineCnt += track[i].size();
    }
    
    cout << lineCnt;
    
    for(int i = 1; i <= V; i++)
    {
        for(int j = 0; j < track[i].size(); j++)
        {
            cout << i << " "  << track[i][j];
        }
        cout << "\n";
    }
    
    return 0;
}
cs

+ Recent posts