Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define oo 1e9
- using namespace std;
- ifstream fin("parc.in");
- ofstream fout("parc.out");
- int a[105][105], n, m, C, P, t[105], viz[105], d[105];
- void Citire()
- {
- int i, j, x, y, cost;
- fin >> n >> m >> C;
- for (i=1; i<n; i++)
- for (j=i+1; j<=n; j++)
- a[i][j]=a[j][i]=oo;
- for (i=1; i<=m; i++)
- {
- fin >> x >> y >> cost;
- a[x][y]=cost;
- a[y][x]=cost;
- }
- fin >> P;
- for (i=1; i<=P; i++)
- fin >> t[i];
- }
- void Dijkstra (int s)
- {
- int i, p, minim, pas;
- for (i=1; i<=n; i++)
- {
- d[i]=oo;
- viz[i]=0;
- }
- d[s]=0;
- for (pas=1; pas<n; pas++)
- {
- minim=oo;
- p=0;
- for (i=1; i<=n; i++)
- if (minim>d[i] and viz[i]==0)
- {
- minim=d[i];
- p=i;
- }
- if (p==0) return ;
- viz[p]=1;
- for (i=1; i<=n; i++)
- if (a[p][i]<oo and d[i]>d[p]+a[p][i])
- d[i]=d[p]+a[p][i];
- }
- }
- void Afisare()
- {
- int dmin=oo, i, nod=0;
- for (i=1; i<=P; i++)
- if (dmin>d[t[i]])
- {
- dmin=d[t[i]];
- nod=t[i];
- }
- else if (dmin==d[t[i]])
- nod = min (nod, t[i]);
- fout << nod;
- }
- int main()
- {
- Citire();
- Dijkstra(C);
- Afisare();
- fin.close();
- fout.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement