HDU 1874 畅通工程续——dijkstra

网友投稿 864 2022-11-29

HDU 1874 畅通工程续——dijkstra

HDU 1874 畅通工程续——dijkstra

注意判断-1的条件

#include #include #include #include #include #include using namespace std;const int maxn = 500;const int INF = 0x3f3f3f3f;typedef pair P;struct Edge { int to, cost;};vector G[maxn];int n, m, s, t, vis[maxn], d[maxn];void dijkstra(int s) { memset(vis, 0, sizeof(vis)); for (int i = 0; i < n; i++) d[i] = INF; d[s] = 0; priority_queue, greater

> q; q.push(P(0, s)); while (!q.empty()) { P p = q-(); q.pop(); int pos = p.second; if (vis[pos]) continue; vis[pos] = 1; int len = G[pos].size(); for (int i = 0; i < len; i++) { Edge e = G[pos][i]; if (d[e.to] > d[pos] + e.cost) { d[e.to] = d[pos] + e.cost; q.push(P(d[e.to], e.to)); } } }}void input() { for (int i = 0; i < n; i++) G[i].clear(); int a, b, c; while (m--) { scanf("%d %d %d", &a, &b, &c); Edge e1, e2; e1.to = b, e1.cost = c; e2.to = a, e2.cost = c; G[a].push_back(e1); G[b].push_back(e2); } scanf("%d %d", &s, &t);}int main(){ while (scanf("%d %d", &n, &m) == 2) { input(); dijkstra(s); if (d[t] == INF) d[t] = -1; printf("%d\n", d[t]); }}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:HDU 2544 最短路——dijlstra
下一篇:Ubuntu个人记录
相关文章

 发表评论

暂时没有评论,来抢沙发吧~