啥都不会啊!怎么办啊!

Fitz

慢慢来生活总会好起来的!!!

小木乃伊到我家~最短路

链接:https://www.nowcoder.com/acm/contest/96/E
来源:牛客网

题目描述

  AA的欧尼酱qwb是个考古学家,有一天qwb发现了只白白圆圆小小的木乃伊,它是个爱哭鬼却很努力。qwb想把这么可爱的小木乃伊送给
AA,于是便找上了快递姐姐,这下可让快递姐姐犯愁了,因为去往AA家的路实在太难走了(甚至有可能没有路能走到AA家),快递姐姐
找上聪明的ACMer,想请你帮忙找出最快到达AA家的路,你行吗?

输入描述:

第一行输入两个整数n和m(2<=n<=m<=200000),分别表示有n座城市和m条路,城市编号为1~n(快递姐姐所在城市为1,AA所在城市为n)。
接下来m行,每行输入3个整数u,v,w(u,v<=n,w<=100000),分别表示城市u和城市v之间有一条长为w的路。

输出描述:

输出结果占一行,输出快递姐姐到达AA家最短需要走多远的路,如果没有路能走到AA家,则输出“qwb baka”(不用输出双引号)。
示例1

输入

4 4
1 2 1
2 3 2
3 4 3
2 3 1

输出

5

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 1e5 + 10;
 4 const int inf = 0x3f3f3f3f;
 5 struct node {
 6     int x, y;
 7     node (int x1, int y1 ) {
 8         x = x1;
 9         y = y1;
10     }
11 };
12 vector<node>g[2 * maxn];
13 int dis[2 * maxn], vis[2 * maxn];
14 
15 void spfa(int s) {
16     queue<int>q;
17     memset(dis, 0x3f, sizeof(dis));
18     memset(vis, 0, sizeof(vis));
19     dis[s] = 0;
20     q.push(s);
21     vis[s] = 1;
22     while(!q.empty()) {
23         int u = q.front();
24         q.pop();
25         vis[u] = 0;
26         for (int i = 0 ; i < g[u].size() ; i++) {
27             int v = g[u][i].x, w = g[u][i].y;
28             if (dis[v] > dis[u] + w) {
29                 dis[v] = dis[u] + w;
30                 if (!vis[v]) {
31                     q.push(v);
32                     vis[v] = 1;
33                 }
34             }
35         }
36     }
37 }
38 int main() {
39     int n, m;
40     scanf("%d%d", &n, &m);
41     for (int i = 0 ; i < m ; i++) {
42         int a, b, c;
43         scanf("%d%d%d", &a, &b, &c);
44         g[a].push_back(node(b, c));
45         g[b].push_back(node(a, c));
46     }
47     spfa(1);
48     if (dis[n] == inf) printf("qwb baka\n");
49     else printf("%d\n", dis[n]);
50     return 0;
51 }

 

posted @ 2018-04-14 21:05  Fitz~  阅读(177)  评论(0编辑  收藏  举报