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 93 94 95 96 97 98 99 100 101 102 103
| #include<bits/stdc++.h> using namespace std;
typedef long long ll; #define endl "\n" #define IOS ios::sync_with_stdio(false); cin.tie(0) #define inf 0x3f3f3f3f #define pii pair<int, int> #define pll pair<ll, ll> #define pdd pair<double, double> #define debug(a) cout<<"\tdebug:"<<a<<endl #define for1(i, a, b) for(int i = a; i <= b; i++) #define for2(i, b, a) for(int i = b; i >= a; i--) const double PI = acos(-1.0); const int mod = 998244353; const int eps = 1e-8; const int N = 10 + 1e5;
void slove(); template<typename T>void read(T &x) { x = 0;char ch = getchar();ll f = 1; while(!isdigit(ch)){if(ch == '-') f *= -1; ch = getchar();} while(isdigit(ch)){x = x*10+ch-48; ch = getchar();} x *= f; } template<typename T>void print(T x) { if(x < 0) putchar('-'), x = -x; if(x >= 10) print(x/10); putchar(x % 10 + '0'); }
int main() { #ifndef ONLINE_JUDGE freopen("in.in", "r", stdin); freopen("out.out", "w", stdout); #endif IOS; slove(); return 0; }
int n, m, tot, s, t; int vis[N], head[N], dis[N];
struct edge { int to, next, val; }e[N];
struct node { int to, val; bool operator < (const node&b) const { return val > b.val; } };
void add(int u, int v, int val) { e[tot].to = v; e[tot].val = val; e[tot].next = head[u]; head[u] = tot++; }
void dijkstra() { memset(dis, 0x3f, sizeof dis); dis[s] = 0; priority_queue<node> q; q.push(node{s, 1}); while(!q.empty()) { node x = q.top(); q.pop(); int u = x.to; if(vis[u]) continue; vis[u] = 1; for(int i = head[u]; ~i; i = e[i].next) { int v = e[i].to; if(dis[v] > dis[u] + e[i].val) { dis[v] = dis[u]+e[i].val; q.push(node{v, dis[v]}); } } } }
void slove() { memset(head, -1, sizeof head); cin>>n>>m>>s>>t; while(m--) { int u, v, w; cin>>u>>v>>w; add(u, v, w); add(v, u, w); } dijkstra(); cout<<dis[t]<<endl; }
|