Pagini recente » Cod sursa (job #1501159) | Monitorul de evaluare | Cod sursa (job #2206001) | Cod sursa (job #534578) | Cod sursa (job #938421)
Cod sursa(job #938421)
#include<fstream>
#include<vector>
#include<queue>
using namespace std;
struct st
{
int x,y;
bool operator <(const st a)const
{
return a.y<y;
}
};
priority_queue <st> h;
vector <int> a[50001],cst[50001];
int c[50001],i,n,m,x,y,z;
st mk(int x,int y)
{
st a;
a.x=x;
a.y=y;
return a;
}
void solve()
{
int i,x;
while (!h.empty())
{
x=h.top().x;
if (c[x]!=0)
{
h.pop();
continue;
}
c[x]=h.top().y;
for (i=0;i<a[x].size();i++)
if (c[a[x][i]]==0)
h.push(mk(a[x][i],c[x]+cst[x][i]));
h.pop();
}
}
int main()
{
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
f >> n >> m;
for (i=1;i<=m;i++)
{
f >> x >> y >> z;
a[x].push_back(y);
cst[x].push_back(z);
a[y].push_back(x);
cst[y].push_back(z);
}
h.push(mk(1,1));
solve();
for (i=2;i<=n;i++)
if (c[i]==0)
g << 0 << ' ';
else g << c[i]-1 << ' ';
return 0;
}