Pagini recente » Cod sursa (job #259181) | Cod sursa (job #2562308) | Cod sursa (job #334188) | Cod sursa (job #1081553) | Cod sursa (job #2259663)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
const int INF=(1<<30);
const int Maxx=3e4+1;
struct graph{
int node,cost;
}nw,ac;
vector <graph> A[Maxx];
vector <graph>::iterator it;
queue <graph> Q;
ifstream fin("sate.in");
ofstream fout("sate.out");
int n,m,from,to,i;
int x,y,cost;
int dp[Maxx];
void dijkstra(int from){
Q.push({from,0});
dp[from]=0;
while (!Q.empty()){
ac=Q.front();
Q.pop();
if (dp[ac.node]<ac.cost) continue;
for (it=A[ac.node].begin();it!=A[ac.node].end();++it){
nw=(*it);
if (dp[nw.node]>nw.cost+ac.cost){
dp[nw.node]=nw.cost+ac.cost;
Q.push(nw);
}
}
}
}
int main() {
fin>>n>>m>>from>>to;
for (;m;--m){
fin>>x>>y>>cost;
A[x].push_back({y,cost});
A[y].push_back({x,cost});
}
for (i=1;i<=n;++i) dp[i]=INF;
dijkstra(from);
fout<<dp[to]+1;
return 0;
}