#include <bits/stdc++.h>
#define S 20003
using namespace std;
struct node{
int e, w;
node(int a, int b){
e = a, w = b;
}
bool operator < (const node& p) const{
return w > p.w;
}
};
vector <int> route[S], cost[S];
int src, dest, dis[S];
void djkstra(){
dis[src] = 0;
priority_queue <node> PQ;
PQ.push(node(src, 0));
while(!PQ.empty()){
node ED = PQ.top();
int u = ED.e;
PQ.pop();
if(u == dest){
cout << dis[dest] << endl;
return;
}
int sz = route[u].size();
for(int i = 0; i < sz; i++){
int v = route[u][i];
if(dis[u] + cost[u][i] < dis[v]){
dis[v] = dis[u] + cost[u][i];
PQ.push(node(v, dis[v]));
}
}
}
puts("unreachable");
}
int main(){
int N, n, m, s, t, x, y, z, cs = 0;
cin >> N;
while(N--){
cin >> n >> m >> s >> t;
for(int i = 0; i < n; i++)dis[i] = INT_MAX;
src = (s <= t)? s : t;
dest = (s >= t)? s : t;
for(int i = 1; i <= m; i++){
cin >> x >> y >> z;
route[x].push_back(y);
route[y].push_back(x);
cost[x].push_back(z);
cost[y].push_back(z);
}
cout << "Case #" << ++cs << ": ";
djkstra();
for(int i = 0; i < n; i++)cost[i].clear(), route[i].clear();
}
return 0;
}
#define S 20003
using namespace std;
struct node{
int e, w;
node(int a, int b){
e = a, w = b;
}
bool operator < (const node& p) const{
return w > p.w;
}
};
vector <int> route[S], cost[S];
int src, dest, dis[S];
void djkstra(){
dis[src] = 0;
priority_queue <node> PQ;
PQ.push(node(src, 0));
while(!PQ.empty()){
node ED = PQ.top();
int u = ED.e;
PQ.pop();
if(u == dest){
cout << dis[dest] << endl;
return;
}
int sz = route[u].size();
for(int i = 0; i < sz; i++){
int v = route[u][i];
if(dis[u] + cost[u][i] < dis[v]){
dis[v] = dis[u] + cost[u][i];
PQ.push(node(v, dis[v]));
}
}
}
puts("unreachable");
}
int main(){
int N, n, m, s, t, x, y, z, cs = 0;
cin >> N;
while(N--){
cin >> n >> m >> s >> t;
for(int i = 0; i < n; i++)dis[i] = INT_MAX;
src = (s <= t)? s : t;
dest = (s >= t)? s : t;
for(int i = 1; i <= m; i++){
cin >> x >> y >> z;
route[x].push_back(y);
route[y].push_back(x);
cost[x].push_back(z);
cost[y].push_back(z);
}
cout << "Case #" << ++cs << ": ";
djkstra();
for(int i = 0; i < n; i++)cost[i].clear(), route[i].clear();
}
return 0;
}
No comments:
Post a Comment