#include <cstdio>
#include <algorithm>
#include <queue>
#define N 20002
#define M 50002
using namespace std;
const int inf = 300000000;
typedef pair<int, int> pii;
int first[N], d[N ];
int next[M * 2], u[M * 2], v[ M *2], w[M * 2];
int ca = 1;
void read_graph(int n, int m )
{
for (int i = 0;i < n; i++)first[i] = -1;
for (int e = 0; e < m; e++)
{
scanf("%d %d %d", &u[e], &v[e], &w[e]);
u[e + m] = v[e];
v[e + m] = u[e];
w[e + m] = w[e];
next[e] = first[u[e]];
first[u[e]] = e;
next[e + m] = first[v[e]];
first[v[e]] = e + m;
}
}
void dijkstra(int s, int n)
{
for (int i = 0;i < n; i++)d[i] = inf;
d[s] = 0;
priority_queue<pii, vector<pii>, greater<pii> > q;
q.push(make_pair(d[s], s));
while (!q.empty())
{
pii u = q.top();q.pop();
int x = u.second;
if (u.first != d[x])continue;
for (int e = first[x]; e != -1; e = next[e])if (d[v[e]] > d[x] + w[e])
{
d[v[e]] = d[x] + w[e];
q.push(make_pair(d[v[e]], v[e]));
}
}
}
int main()
{
int t, n, m, s, e;
scanf("%d", &t);
while (t--)
{
scanf("%d %d %d %d", &n, &m, &s, &e);
read_graph(n, m ) ;
dijkstra(s, n);
printf("Case #%d: ", ca++);
if (d[e] == inf)puts("unreachable");
else printf("%d\n", d[e]);
}
return 0;
}
UVA 10986 Sending email DIJSKRA(稀疏图)
最新推荐文章于 2013-08-03 11:18:00 发布