spfa+slf
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstring>
using namespace std;
int T,R,P,S;
const int maxn=200000+5;
deque<int>q;
bool used[maxn];
int first[maxn],next[maxn<<1],tot=0,dis[maxn];
struct edge
{
int f,t,v;
}es[maxn<<1];
void build(int f,int t,int v)
{
es[++tot]=(edge){f,t,v};
next[tot]=first[f];
first[f]=tot;
}
void read(int &a)
{
a=0;
int h=1;
char c=getchar();
while(c<'0'||c>'9')
{
if(c=='-')
h=-1;
c=getchar();
}
while(c>='0'&&c<='9')
{
a*=10;
a+=c-'0';
c=getchar();
}
a*=h;
}
void spfa()
{
while(!q.empty())
{
int x=q.front();
q.pop_front();
used[x]=0;
for(int i=first[x];i;i=next[i])
{
int v=es[i].v,t=es[i].t;
if(dis[t]>dis[x]+v)
{
dis[t]=dis[x]+v;
if(!used[t])
{
if(q.empty()) q.push_front(t);
else if(dis[t]>dis[q.front()] ) q.push_back(t);
else q.push_front(t);
used[t]=1;
}
}
}
}
}
int main()
{
memset(used,0,sizeof(used));
memset(dis,0x3f3f3f3f,sizeof(dis));
int x,y,z;
read(T),read(R),read(P),read(S);
for(int i=1;i<=R;i++)
{
read(x),read(y),read(z);
build(x,y,z);
build(y,x,z);
}
for(int i=1;i<=P;i++)
{
read(x),read(y),read(z);
build(x,y,z);
}
q.push_front(S);
used[S]=1;
dis[S]=0;
spfa();
int cnt=0;
for(int i=1;i<=T;i++)
{
if(dis[i]==0x3f3f3f3f) printf("Need Teleport To Repair\n"),cnt++;
else printf("%d\n",dis[i]);
}
printf("%d\n",cnt);
return 0;
}