#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <algorithm>
#define INF 0x3F3F3F3F
using namespace std;
const int MAXN = (int)(1e4 + 10);
const int MAXM = (int)(1e5 + 10);
int n, ml, md, dist[MAXN];
int size, head[MAXN], point[MAXM], val[MAXM], nxt[MAXM];
void add (int from, int to, int value)
{
val[size] = value;
point[size] = to;
nxt[size] = head[from];
head[from] = size++;
}
bool spfa(int s)
{
int cnt[MAXN];
bool vis[MAXN];
queue<int> q;
memset(vis, false, sizeof vis);
memset(dist, 0x3f, sizeof dist);
memset(time, 0, sizeof time);
q.push(s);
vis[s] = true;
dist[s] = 0;
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = false;
for(int i = head[u]; ~i; i = nxt[i]){
if(dist[point[i]] > dist[u] + val[i]){
dist[point[i]] = dist[u] + val[i];
if(!vis[point[i]]){
vis[point[i]] = true;
q.push(point[i]);
if(++cnt[point[i]] > n) return false;
}
}
}
}
return true;
}