Pagini recente » Cod sursa (job #272925) | Cod sursa (job #1267729) | Cod sursa (job #7033) | Cod sursa (job #2572065) | Cod sursa (job #1864424)
#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define nod first
#define cost second
#define inf 0x3f3f3f3f
using namespace std;
ifstream f("ubuntzei.in");
ofstream g("ubuntzei.out");
const int NMAX = 2000;
vector< pair<int, int> > G[NMAX];
queue<int> q;
bool USED[NMAX];
int D[NMAX], vec_prieteni[NMAX], parinti[NMAX], drum[NMAX];
int n, m, k, Nod, counter;
inline bool exista(int value)
{
int left, right, middle;
left = 1;
right = k;
while(left <= right)
{
middle = (left+right) / 2;
if(vec_prieteni[middle] == value) return true;
else if(vec_prieteni[middle] > value) right = middle-1;
else left = middle + 1;
}
return false;
}
void citire()
{
f>>n>>m;
f>>k;
for(int i = 1; i <= k; i ++)
f>>vec_prieteni[i];
for(int i = 1; i <= m; i++)
{
int x, y, c;
f>>x>>y>>c;
G[x].pb( mp(y, c) );
//G[y].pb( mp(x, c) );
}
}
void Bellman_Ford()
{
while(!q.empty())
{
int Nod = q.front();
q.pop();
USED[Nod] = false;
for(int i = 0; i < G[Nod].size(); i++)
{
//if(exista((G[Nod][i]).nod) == true) counter++;
if( D[(G[Nod][i]).nod] > D[Nod] + (G[Nod][i]).cost)
{
D[(G[Nod][i]).nod] = D[Nod] + (G[Nod][i]).cost;
parinti[(G[Nod][i]).nod] = Nod;
if(USED[(G[Nod][i]).nod] == false)
{
USED[(G[Nod][i]).nod] = true;
q.push((G[Nod][i]).nod);
}
}
}
}
}
int main()
{
citire();
memset(D, inf, sizeof(D));
//--start node
int START = 1;
q.push(START);
USED[START] = true;
D[START] = 0;
//--end start node
sort(vec_prieteni, vec_prieteni+k);
if(exista(START) == true) counter++;
Bellman_Ford();
int suma_minima = 0;
int z = n, j = 1;
//--find drum
while( z!=0 )
{
drum[j] = D[z];
z = parinti[z];
j++;
}
bool treci = false;
for(int i = 1; i <= n; i++)
if(parinti[i] != 0)
{
if(exista(i) == true) counter ++;
}
if(counter == k) treci = true;
if(treci == true)
{
for(int i = 1; i <= n; i++) if(drum[i] != 0) suma_minima += drum[i];
g<<suma_minima;
}
return 0;
}