1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <queue> using namespace std;
#define MAXN 2500 + 5 #define DEBUG(x) cerr << #x << '=' << x
const int Inf = 2e31-1;
struct Node; struct Edge; struct Node{ Edge *firstEdge; int dist; bool inQueue; } node[MAXN];
struct Edge{ Node *s,*t; int w; Edge *next;
Edge(Node *s,Node *t,int w) : s(s),t(t),w(w),next(s->firstEdge){} };
inline void add(const int &s,const int &t,const int &w){ node[s].firstEdge = new Edge(&node[s],&node[t],w); node[t].firstEdge = new Edge(&node[t],&node[s],w); }
inline int spfa(const int &s,const int &t,const int &n){ for (int i = 1;i <= n;i++){ node[i].dist = Inf; node[i].inQueue = false; } queue<Node *> q; q.push(&node[s]); node[s].dist = 0; node[s].inQueue = true; while (!q.empty()){ Node *u = q.front(); q.pop(); u->inQueue = false;
for (Edge *e = u->firstEdge;e;e = e->next){ Node *v = e->t; if (v->dist > u->dist + e->w){ v->dist = u->dist + e->w; if (!v->inQueue){ q.push(v); v->inQueue = true; } } } } return node[t].dist; }
int main(int argc, char const *argv[]) { int n,m,s,t; scanf("%d %d %d %d",&n,&m,&s,&t); for (int i = 1;i <= m;i++){ int u,v,w; scanf("%d %d %d",&u,&v,&w); add(u,v,w); } printf("%d",spfa(s,t,n)); return 0; }
|