/* Bismillahir Rahmanir Rahim
Solution using BFS algorithm
*/
#include<bits/stdc++.h>
#define fi(n, m) for(int i=n; i<=m; i++)
#define fd(n, m) for(int i=n; i>=m; i--)
#define inf 100009
using namespace std;
vector<int>vt[505], cost[505];
int dis[505]={0};
void bfs(int st){
queue<int>q;
q.push(st);
dis[st]=0;
int p, sz, temp, xx;
while(!q.empty()){
p=q.front();
q.pop();
sz=vt[p].size();
fi(0, sz-1){
xx=vt[p][i];
temp=max(dis[p], cost[p][i]);
if(temp<dis[xx]){
dis[xx]=temp;
q.push(xx);
}
}
}
}
int main(){
int t, cs=1, n, m, u, v, w, tt;
cin>>t;
while(t--){
cin>>n>>m;
fi(0, m-1){
cin>>u>>v>>w;
vt[u].push_back(v);
vt[v].push_back(u);
cost[u].push_back(w);
cost[v].push_back(w);
}
cin>>tt;
fi(0, n-1) dis[i]=inf;
bfs(tt);
cout<<"Case "<<cs++<<":"<<endl;
fi(0, n-1){
if(dis[i]==inf)cout<<"Impossible"<<endl;
else cout<<dis[i]<<endl;
}
fi(0, n-1){
vt[i].clear();
cost[i].clear();
}
}
return 0;
}
|
Monday, February 20, 2017
Solution of Light OJ 1002-Country Roads
Subscribe to:
Post Comments (Atom)
-
#include<bits/stdc++.h> #define ll long long using namespace std ; ll n , k , t_case ; ll bigmod ( ll b , ll p , ll m...
-
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 ...
-
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 3...
No comments:
Post a Comment