#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iostream>
#include<queue>
#include<vector>
#include<set>
#include<climits>
#include<map>
#include<string>
using namespace std;
int N,n,m,S,T;
const int INF=1000000000;
const int maxm=50000+10;
const int maxn=20000+10;
struct edge
{
int to,cost;
edge(int to=0,int cost=0):to(to),cost(cost){}
};
typedef pair<int,int> P;
int d[maxn];
vector<edge> G[maxn];
void Dijkstra()
{
priority_queue<P,vector<P>,greater<P> > que;
fill(d,d+n,INF);
d[S]=0;
que.push(P(0,S));
while(!que.empty())
{
P p=que.top();
que.pop();
int v=p.second;
if(d[v]<p.first)continue;
if(v==T)break;
for(int i=0;i<G[v].size();i++)
{
edge e=G[v][i];
if(d[e.to]>d[v]+e.cost)
{
d[e.to]=d[v]+e.cost;
que.push(P(d[e.to],e.to));
}
}
}
}
int main()
{
int t=1;
scanf("%d",&N);
while(N--)
{
scanf("%d%d%d%d",&n,&m,&S,&T);
for(int i=0;i<n;i++)
{
G[i].clear();
}
for(int i=0;i<m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
G[u].push_back(edge(v,w));
G[v].push_back(edge(u,w));
}
Dijkstra();
if(d[T]==INF)printf("Case #%d: unreachable\n",t++);
else printf("Case #%d: %d\n",t++,d[T]);
}
return 0;
}
UVa 10986 Sending email (最短路+Dijkstra队列优化)
最新推荐文章于 2024-08-15 12:55:32 发布