Pagini recente » Cod sursa (job #2468181) | Cod sursa (job #1017741) | Cod sursa (job #1033573) | Cod sursa (job #1288738) | Cod sursa (job #2864559)
#include <iostream>
#include <fstream>
#include <utility>
#include <vector>
#include <queue>
using namespace std;
ifstream f("ubuntzei.in");
ofstream g("ubuntzei.out");
const int oo = (1 << 30);
const int NMax=50005;
vector < pair <int,int> > G[NMax];
bool Jart[NMax];
int D[NMax];
int Tavolsag[NMax]={};
struct rendezes{
bool operator()(int x,int y)
{
return D[x] > D[y];
}
};
priority_queue <int , vector<int> , rendezes> Sor;
bool Ubuntuk_helye[NMax]={};
int n,m,k,L=1;
void beolvas()
{
int a,b,c;
f>>n>>m>>k;
for(int i=1;i<=k;i++)
{
f>>a;
Ubuntuk_helye[a]=1;
}
while(f>>a>>b>>c)
{
G[a].push_back(make_pair(b,c));
G[b].push_back(make_pair(a,c));
}
}
void kiir()
{
for(int i=1;i<=n;i++)
{
if(D[i] == oo)
{
cout<<"0 ";
}
else cout<<D[i]<<" ";
}
}
void dijkstra(int KezdoCsucs)
{
for(int i=1;i<=n;i++)
{
D[i]=oo;
Tavolsag[i]=0;
}
D[KezdoCsucs] = 0;
Jart[KezdoCsucs] = true;
Sor.push(KezdoCsucs);
while(!Sor.empty())
{
int JelenlegiCsucs = Sor.top();
Sor.pop();
Jart[JelenlegiCsucs] = false;
for(size_t i = 0;i < G[JelenlegiCsucs].size();i++)
{
int Szomszed = G[JelenlegiCsucs][i].first;
int Ertek = G[JelenlegiCsucs][i].second;
if(D[JelenlegiCsucs]+Ertek < D[Szomszed])
{
D[Szomszed] = D[JelenlegiCsucs]+Ertek;
Tavolsag[Szomszed]=Tavolsag[JelenlegiCsucs]+1;
if(Jart[Szomszed] == false)
{
Sor.push(Szomszed);
Jart[Szomszed] = true;
}
}
}
}
}
int main()
{
beolvas();
dijkstra(n);
int TavolsagNtol[n+1];
for(int i=1;i<=n;i++)
{
TavolsagNtol[i]=Tavolsag[i];
}
dijkstra(1);
kiir();
while(1)
{
int NtolLegtavolabbi=0;
int NtolLegtavolabbiHely;
for(int i=1;i<=n;i++)
{
if(Ubuntuk_helye[i]==1&&D[i]>NtolLegtavolabbi)
{
NtolLegtavolabbiHely=i;
NtolLegtavolabbi=D[i];
}
}
if(NtolLegtavolabbi==0)
{
cout<<Tavolsag[n];
L+=Tavolsag[n];
break;
}
L+=Tavolsag[NtolLegtavolabbiHely];
Ubuntuk_helye[NtolLegtavolabbiHely]=0;
dijkstra(NtolLegtavolabbiHely);
cout<<endl<<"L="<<L<<endl<<NtolLegtavolabbiHely<<endl;
kiir();
}
g<<L;
return 0;
}