#include <iostream>
#include <vector>
#include <bitset>
#include <queue>
#include <algorithm>
#include <cstdlib>
#include <functional>
#include <utility>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
int n,e;
cin>>n>>e;
vector<vector<pair<int,int> > > a(n+1);
vector<long long int> dist(n+1,1e12);
for(int i=0;i<e;i++){
int x,y,w;
cin>>x>>y>>w;
a[x].push_back({y,w});
a[y].push_back({x,w});
}
int s;
cin>>s;
priority_queue<pair<long long int,long long int>,vector<pair<long long int,long long int> >,greater<pair<long long int,long long int> > > pq;
pq.push({0,s});
dist[s]=0;
while(!pq.empty()){
pair<int,int> front=pq.top();
pq.pop();
long long int u=front.second,w=front.first;
if(w>dist[u])
continue;
for(int i=0;i<a[u].size();i++){
if(dist[a[u][i].first]>dist[u]+a[u][i].second){
dist[a[u][i].first]=dist[u]+a[u][i].second;
pq.push({dist[a[u][i].first],a[u][i].first});
}
}
}
int i;
if(1==s){
if(dist[2]!=1e12)
cout<<dist[2];
else cout<<-1;
i=3;
}
else {
if(dist[1]!=1e12)
cout<<dist[1];
else cout<<-1;
i=2;
}
for(;i<=n;i++)
if(i!=s)
if(dist[i]!=1e12)
cout<<' '<<dist[i];
else
cout<<" "<<-1;
cout<<endl;
}
}
#include <vector>
#include <bitset>
#include <queue>
#include <algorithm>
#include <cstdlib>
#include <functional>
#include <utility>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
int n,e;
cin>>n>>e;
vector<vector<pair<int,int> > > a(n+1);
vector<long long int> dist(n+1,1e12);
for(int i=0;i<e;i++){
int x,y,w;
cin>>x>>y>>w;
a[x].push_back({y,w});
a[y].push_back({x,w});
}
int s;
cin>>s;
priority_queue<pair<long long int,long long int>,vector<pair<long long int,long long int> >,greater<pair<long long int,long long int> > > pq;
pq.push({0,s});
dist[s]=0;
while(!pq.empty()){
pair<int,int> front=pq.top();
pq.pop();
long long int u=front.second,w=front.first;
if(w>dist[u])
continue;
for(int i=0;i<a[u].size();i++){
if(dist[a[u][i].first]>dist[u]+a[u][i].second){
dist[a[u][i].first]=dist[u]+a[u][i].second;
pq.push({dist[a[u][i].first],a[u][i].first});
}
}
}
int i;
if(1==s){
if(dist[2]!=1e12)
cout<<dist[2];
else cout<<-1;
i=3;
}
else {
if(dist[1]!=1e12)
cout<<dist[1];
else cout<<-1;
i=2;
}
for(;i<=n;i++)
if(i!=s)
if(dist[i]!=1e12)
cout<<' '<<dist[i];
else
cout<<" "<<-1;
cout<<endl;
}
}
Comments
Post a Comment