Pagini recente » Cod sursa (job #2462620) | Cod sursa (job #565640) | Cod sursa (job #1672516) | Cod sursa (job #68789) | Cod sursa (job #2357855)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#define NMAX 2005
using namespace std;
ifstream fi("ubuntzei.in");
ofstream fo("ubuntzei.out");
struct Edge
{
int node;
int cost;
int city;
Edge(int aNode, int aCost)
{
node = aNode;
cost = aCost;
}
Edge(int aNode, int aCost, int aCity)
{
node = aNode;
cost = aCost;
city = aCity;
}
bool operator <(const Edge & other) const
{
if(city < other.city)
return true;
else if(city > other.city)
return false;
else
return cost > other.cost;
}
};
int N, M, K;
int dist[NMAX][NMAX];
bool locations[NMAX];
vector<Edge> graph[NMAX];
priority_queue<Edge> q;
int main()
{
int a, b, c;
fi >> N >> M;
fi >> K;
for(int i = 1; i <= K; ++i)
{
fi >> a;
locations[a] = 1;
}
for(int i = 1; i <= M; ++i)
{
fi >> a >> b >> c;
graph[a].push_back(Edge(b, c));
graph[b].push_back(Edge(a, c));
}
for(int i = 1; i <= N; ++i)
for(int j = 1; j <= N; ++j)
dist[i][j] = 0x3f3f3f3f;
q.push(Edge(1, 0, 0));
while(!q.empty() && (q.top().node != N || q.top().city != K))
{
Edge e = q.top();
q.pop();
for(auto y : graph[e.node])
{
if(dist[y.node][e.city + locations[y.node]] > y.cost + e.cost)
{
dist[y.node][e.city + locations[y.node]] = y.cost + e.cost;
q.push(Edge(y.node, dist[y.node][e.city + locations[y.node]], e.city + locations[y.node]));
}
}
}
fo << q.top().cost;
}