Pagini recente » Cod sursa (job #385000) | Cod sursa (job #976708) | Cod sursa (job #1957165) | Cod sursa (job #367815) | Cod sursa (job #782660)
Cod sursa(job #782660)
#include<fstream>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
const int Nmax = 1000;
vector <int> vecini[Nmax];
queue <int> coada;
bool ok[Nmax], pus[Nmax];
int nod[Nmax], i, r, p, t, n, m, k, x, y, z, best[Nmax], nc, cv, cost[Nmax][Nmax];
int main()
{
ifstream f("ubuntzei.in");
ofstream h("ubuntzei.out");
f>>n>>m;
f>>k;
for (i=1; i<=k; ++i)
{
f>>p;
nod[i]=p;
ok[p]=true;
}
nod[0]=1;
nod[k+1]=n;
for (i=1; i<=m; ++i)
{
f>>x>>y>>z;
vecini[x].push_back(y);
cost[x][y]=cost[y][x]=z;
vecini[y].push_back(x);
}
f.close();
for (r=0; r<=k+1; ++r)
{
cv=nod[r];
memset (best, 0x3f, sizeof(best));
memset (pus, 0, sizeof (pus));
best[cv]=0;
coada.push(cv);
pus[cv] = 1;
while(!coada.empty())
{
nc = coada.front();
coada.pop();
pus[nc] = 1;
for(i = 0; i < vecini[nc].size(); ++i)
if(best[vecini[nc][i]] > best[nc] + cost[nc][vecini[nc][i]] && ok[vecini[nc][i]] && vecini[nc][i]>nc)
{
if(!pus[vecini[nc][i]])
{
coada.push(vecini[nc][i]);
pus[vecini[nc][i]] = 1;
}
best[vecini[nc][i]] = best[nc] + cost[nc][vecini[nc][i]];
}
}
for (t=1; t<=n; ++t)
h<<best[t]<<" ";
h<<'\n';
}
h.close();
return 0;
}