#include<bits/stdc++.h>
using namespace std;
vector<long>vec[200002],cost[200002];
long sorc,desti;
long dist[200002];
struct node
{
long u,w;
node(long a,long b)
{
u=a;
w=b;
}
bool operator < ( const node& p ) const
{
return w > p.w;
}
};
long dijkstra(long n)
{
for(long i=0; i<=n; i++)
dist[i]=10000000;
priority_queue<node>pq;
dist[sorc]=0;
pq.push(node(sorc,0));
while(!pq.empty())
{
node top=pq.top();
pq.pop();
long u=top.u;
if(u==desti)
{
return dist[desti];
}
for(long i=0;i<(long)vec[u].size();i++)
{
long v=vec[u][i];
if(dist[u]+cost[u][i]<dist[v])
{
dist[v]=dist[u]+cost[u][i];
pq.push(node(v,dist[v]));
}
}
}
return -1;
}
main()
{
long ts,cs=1;
cin>>ts;
while(ts--)
{
long i,n,e;
cin>>n>>e>>sorc>>desti;
for(i=0;i<e;i++)
{
long u,v,w;
cin>>u>>v>>w;
vec[u].push_back(v);
vec[v].push_back(u);
cost[u].push_back(w);
cost[v].push_back(w);
}
long ans=dijkstra(n);
if(ans==-1)
printf("Case #%ld: unreachable\n",cs++);
else
printf("Case #%ld: %ld\n",cs++,ans);
for(i=0;i<n;i++)
{
cost[i].clear();
vec[i].clear();
}
}
}
using namespace std;
vector<long>vec[200002],cost[200002];
long sorc,desti;
long dist[200002];
struct node
{
long u,w;
node(long a,long b)
{
u=a;
w=b;
}
bool operator < ( const node& p ) const
{
return w > p.w;
}
};
long dijkstra(long n)
{
for(long i=0; i<=n; i++)
dist[i]=10000000;
priority_queue<node>pq;
dist[sorc]=0;
pq.push(node(sorc,0));
while(!pq.empty())
{
node top=pq.top();
pq.pop();
long u=top.u;
if(u==desti)
{
return dist[desti];
}
for(long i=0;i<(long)vec[u].size();i++)
{
long v=vec[u][i];
if(dist[u]+cost[u][i]<dist[v])
{
dist[v]=dist[u]+cost[u][i];
pq.push(node(v,dist[v]));
}
}
}
return -1;
}
main()
{
long ts,cs=1;
cin>>ts;
while(ts--)
{
long i,n,e;
cin>>n>>e>>sorc>>desti;
for(i=0;i<e;i++)
{
long u,v,w;
cin>>u>>v>>w;
vec[u].push_back(v);
vec[v].push_back(u);
cost[u].push_back(w);
cost[v].push_back(w);
}
long ans=dijkstra(n);
if(ans==-1)
printf("Case #%ld: unreachable\n",cs++);
else
printf("Case #%ld: %ld\n",cs++,ans);
for(i=0;i<n;i++)
{
cost[i].clear();
vec[i].clear();
}
}
}
কোন মন্তব্য নেই:
একটি মন্তব্য পোস্ট করুন