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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| #include <iostream> #include <queue>
#define INF 0x3f3f3f3f #define MAXN 3000
using namespace std;
struct Node { struct Edge *edge; int dist; int id;
Node() { edge = NULL; dist = INF; }
const bool operator <(const Node &tmp) const { return this->dist < tmp.dist; }
}node[MAXN];
struct Edge { Node *from, *to; Edge *next; int val;
Edge(Node *from, Node *to, int val): from(from), to(to), next(from->edge), val(val) {} };
int dist[MAXN];
priority_queue<Node> q; void Dijkstra(Node st) { dist[st.id] = 0; st.dist = 0;
q.push(st); while (!q.empty()) { Node tmp = q.top(); q.pop();
if (dist[tmp.id]!=tmp.dist) continue; for (Edge *e = tmp.edge; e; e = e->next) { if ( dist[e->to->id] > tmp.dist + e->val) { Node to = *e->to; dist[to.id] = tmp.dist + e->val; to.dist = dist[to.id];
q.push( to ); } } } }
int main() { int t, c, ts, te; cin >> t >> c >> ts >> te;
for (int i = 1; i<=t; i++) { dist[i] = INF; node[i].id = i; } for (int i = 0; i<c; i++) { int x, y, z; cin >> x >> y >> z;
node[x].edge = new Edge(&node[x], &node[y], z); node[y].edge = new Edge(&node[y], &node[x], z); }
Dijkstra(node[ts]);
cout << dist[ node[te].id ]; }
|