热浪

。。。

#include <iostream>
#include <cstring>
#include <cstdio>
#define Max 12222
#define INF 99999999
using namespace std;
int N, M, start, end;
int Count;
int dis [Max], queue [Max];
bool visit [Max];
int head [Max], from, now;
char word; 
struct node 
{
    int to;
    int next;
    int dis;
}Edge [Max];
void AddEdge (int x, int y, int w)
{
    Count++;
    Edge [Count].to = y;
    Edge [Count].dis = w;
    Edge [Count].next = head [x];
    head [x] = Count;
}
void SPFA (int start)
{
    int head_cur = 0, tail_cur = 1;
    for (int i = 1; i <= N; i++)
    {
        dis [i] = INF;
        visit [i] = false;
    }
    dis [start] = 0;
    queue [1] = start;
    while (head_cur <= tail_cur)
    {
        head_cur++;
        now = queue [head_cur];
        for (int i = head [now]; i ; i = Edge [i].next )
            if (dis [now] + Edge[i].dis < dis [Edge [i].to ])
            {
                dis [Edge[i].to ] = dis [now] + Edge [i].dis;
                if (visit [Edge [i].to ] == false)
                {
                    visit [Edge [i].to] = true;
                    queue [++tail_cur] = Edge[i].to; 
                }
            }
        visit [now] = false;
    }
}
inline void read (int &now)
{
    now = 0;
    word = getchar ();
    while (word < '0' || word > '9')
        word = getchar ();
    while (word <= '9' && word >= '0')
    {
        now = now * 10 + (int)(word - '0');
        word = getchar ();
    }
}
int main()
{
    ios :: sync_with_stdio (false );
    read (N); read (M); read (start); read (end);
    int x, y, w;
    for (int i = 1; i <= M; i++)
    {
        read (x); read (y); read (w);
        AddEdge (x, y, w);
        AddEdge (y, x, w);
    }
    SPFA (start);
    cout << dis [end];
    return 0;
}

 

posted @ 2016-11-14 17:21  ZlycerQan  阅读(229)  评论(0编辑  收藏  举报