Pagini recente » Cod sursa (job #1577337) | Cod sursa (job #767152) | Cod sursa (job #2244004) | Cod sursa (job #1332166) | Cod sursa (job #2036517)
#include<fstream>
#include<vector>
#include<queue>
#define inf 2147483648
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
int n, m, nod, i, vecin, cost, costv;
long long d[50003];
class comparare
{
public:
bool operator() (pair<int, int> a, pair<int, int> b)
{
return a.second<b.second;
}
};
vector <pair <int, int> > l[250003];
priority_queue< pair<int, int>, vector<pair<int, int> >, comparare> heap;
int main()
{
f>>n>>m;
for(i=1;i<=m;i++)
{
int x, y, c;
f>>x>>y>>c;
l[x].push_back(make_pair(y, c));
}
for(i=2;i<=n;i++)
d[i]=inf;
heap.push(make_pair(0, 1));
while(!heap.empty())
{
nod=heap.top().second;
cost=heap.top().first;
heap.pop();
if(d[nod]==cost)
for(vector< pair<int, int> > :: iterator it=l[nod].begin(); it!=l[nod].end(); it++)
{
vecin=it->first;
costv=it->second;
if(d[vecin]>costv+cost)
{
d[vecin]=costv+cost;
heap.push(make_pair(d[vecin], vecin));
}
}
}
for(i=2;i<=n;i++)
if(d[i]!=inf)
g<<d[i]<<" ";
else g<<"0 ";
return 0;
}