-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathCLIQUED.cpp
74 lines (63 loc) · 1.28 KB
/
CLIQUED.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
Problem Link : https://www.codechef.com/problems/CLIQUED
*/
#include<iostream>
#include<vector>
#include<set>
#define pi pair< long long int, long long int >
#define llu long long int
#define INF 1e18
using namespace std;
int main()
{
llu t,n,k,x,m,s,i,j,a,b,c,curr;
cin>>t;
while(t--)
{
cin>>n>>k>>x>>m>>s;s--;
vector <pi> roads[n+1];
vector <pi>::iterator it;
for(i=0;i<k;i++)
{
roads[i].push_back(make_pair(x,n));
roads[n].push_back(make_pair(x,i));
}
for(i=0;i<m;i++)
{
cin>>a>>b>>c;
a--;b--;
roads[a].push_back(make_pair(2*c,b));
roads[b].push_back(make_pair(2*c,a));
}
llu dist[n+1];
bool visited[n+1];
for(i=0;i<=n;i++)
{
dist[i]=INF;
visited[i] = false;
}
dist[s]=0;
set <pi> spt;
spt.insert(make_pair(0,s));
while(!spt.empty())
{
curr = spt.begin()->second;
spt.erase(spt.begin());
if(visited[curr] == false)
{
for(it=roads[curr].begin();it!=roads[curr].end();it++)
{
if(dist[curr]+it->first < dist[it->second])
{
spt.erase(make_pair(dist[it->second],it->second));
dist[it->second]=dist[curr]+it->first;
spt.insert(make_pair(dist[it->second],it->second));
}
}
}
}
for(i=0;i<n;i++) cout<<dist[i]/2<<" ";
cout<<endl;
}
return 0;
}