Pagini recente » Cod sursa (job #2842936) | Cod sursa (job #602936) | Cod sursa (job #2869472) | Cod sursa (job #2976465) | Cod sursa (job #2313759)
#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#define MAX 0x3f3f3f3f
using namespace std;
ifstream f("ubuntzei.in");
ofstream g("ubuntzei.out");
bool operator< (pair<int,int>a,pair<int,int>b)
{
if (a.second==b.second)
return a.first<b.first;
return a.second<b.second;
}
vector<pair<int,int>>graph[2005];
set<pair<int,int>>q;
int n, m, nr_prieteni, y, from, to, cost, dp[2005];
void dijkstra()
{
q.insert({1,0});
while (!q.empty())
{
int nod=q.begin()->first;
int cost=q.begin()->second;
q.erase(q.begin());
for (auto &v:graph[nod])
{
if (dp[nod]+v.second<dp[v.first])
{
if (dp[v.first]!=MAX)
{
q.erase({v.first,dp[v.first]});
}
dp[v.first]=dp[nod]+v.second;
q.insert({v.first,dp[v.first]});
}
}
}
}
int main() {
f >> n >> m;
f >> nr_prieteni;
for (int i=1; i<=nr_prieteni; i++)
f >> y;
for (int i=1; i<=m; i++)
{
f >> from >> to >> cost;
graph[from].push_back(make_pair(to,cost));
graph[to].push_back(make_pair(from,cost));
}
for (int i=2; i<=n; i++)
dp[i]=0x3f3f3f3f;
dijkstra();
g << dp[n];
return 0;
}