2018年長沙理工大學第十三屆程序設計競賽 E 小木乃伊到我家 【最短路】

時間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 32768K,其他語言65536K
64bit IO Format: %lld
題目描述
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

思路
數據好像沒那麼大。。
用廣搜 就能搜了

但是要注意的是 城市之間 有一條路 是雙向的 所以在壓入路的時候 要壓入兩次

AC代碼

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <list>
#include <numeric> 
#include <sstream>
#include <iomanip>
#include <limits>

#define CLR(a, b) memset(a, (b), sizeof(a))
#define pb push_back

using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss;
typedef pair <int, ll> pil;

const double PI = acos(-1.0);
const double E = exp(1.0);
const double eps = 1e-8;

const int INF = 0x3f3f3f3f;
const ll llINF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 2e5 + 5;
const int MOD = 1e9;

vector <pil> G[maxn];

ll v[maxn];

int n, m;

struct node
{
    int y;
    ll fee;
};

void bfs()
{
    int len = G[1].size();
    node tmp;
    queue <node> q;
    for (int i = 0; i < len; i++)
    {
        tmp.y = G[1][i].first;
        tmp.fee = G[1][i].second;
        v[tmp.y] = min(v[tmp.y], tmp.fee);
        q.push(tmp);
    }
    while (!q.empty())
    {
        node u = q.front(), w;
        q.pop();
        len = G[u.y].size();
        for (int i = 0; i < len; i++)
        {
            w.y = G[u.y][i].first;
            w.fee = u.fee + G[u.y][i].second;
            if (w.fee <= v[w.y])
            {
                q.push(w);
                v[w.y] = w.fee;
            }
        }
    }
}

int main()
{
    while (~scanf("%d%d", &n, &m))
    {
        CLR(v, 0x3f);
        G->clear();
        int x, y;
        ll c;
        for (int i = 0; i < m; i++)
        {
            scanf("%d%d%lld", &x, &y, &c);
            if (x > y)
                swap(x, y);
            G[x].pb(pii(y, c));
            G[y].pb(pii(x, c));
        }
        bfs();
        if (v[n] == llINF)
            printf("qwb baka\n");
        else
            printf("%lld\n", v[n]);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章