Pagini recente » Cod sursa (job #1925943) | Cod sursa (job #1071944) | Cod sursa (job #415169) | Cod sursa (job #124939) | Cod sursa (job #389894)
Cod sursa(job #389894)
# include <fstream>
# include <iostream>
# define INFINIT 1000000000
using namespace std;
struct nod {
int capat, cost;
nod *next;};
nod *a[15003];
int n, h[15003], poz[15003], d[15003], v[15003], nh;
void add (int i, int j, int c)
{
nod *p=new nod;
p->capat=j;
p->cost=c;
p->next=a[i];
a[i]=p;
}
void cerne (int k, int n)
{
int eh=0, i;
while (!eh && 2*k<=n)
{
i=2*k;
if (i+1<=n && d[h[i+1]]<d[h[i]])
i++;
if (d[h[i]]>=d[h[k]])
eh=1;
else
{
int aux;
aux=h[i], h[i]=h[k], h[k]=aux;
poz[h[i]]=i;
poz[h[k]]=k;
k=i;
}
}
}
void promoveaza (int k)
{
int eh=0;
while (!eh && k/2)
if (d[h[k]]>=d[h[k/2]])
eh=1;
else
{
int aux;
aux=h[k], h[k]=h[k/2], h[k/2]=aux;
poz[h[k]]=k;
poz[h[k/2]]=k/2;
k/=2;
}
}
void initializeaza ()
{
for (int i=1;i<=n;i++)
d[i]=INFINIT, h[i]=i, poz[i]=i, v[i]=0;
nh=n;
}
int dijkstra (int sursa, int dest)
{
int max=-1;
initializeaza ();
h[sursa]=h[nh];
poz[h[sursa]]=sursa;
nh--;
cerne (sursa, nh);
for (nod *p=a[sursa];p;p=p->next)
{
d[p->capat]=p->cost;
promoveaza(poz[p->capat]);
}
d[sursa]=0, v[sursa]=1;
while (v[dest]==0)
{
int P;
P=h[1];
v[P]=1;
h[1]=h[nh];
poz[h[1]]=1;
nh--;
cerne(1, nh);
if (d[P]>max)
max=d[P];
for (nod *p=a[P];p;p=p->next)
if (v[p->capat]==0 && d[p->capat]>p->cost)
{
d[p->capat]=p->cost;
promoveaza(poz[p->capat]);
}
}
return max;
}
int main ()
{
ifstream fin ("radiatie.in");
ofstream fout ("radiatie.out");
int m, k;
fin>>n>>m>>k;
for (;m;--m)
{
int x, y, c;
fin>>x>>y>>c;
add(x, y, c);
add(y, x, c);
}
for (;k;--k)
{
int sursa, dest;
fin>>sursa>>dest;
fout<<dijkstra(sursa, dest)<<endl;
}
return 0;
}