Pagini recente » Cod sursa (job #2856943) | Cod sursa (job #371799) | Cod sursa (job #1549625) | Cod sursa (job #2962544) | Cod sursa (job #1611157)
#include <fstream>
#include <iostream>
#include <queue>
#define nmax 50004
#define fin "dijkstra.in"
#define fou "dijkstra.out"
#define inf 0x7fffffff
using namespace std;
ifstream t1(fin);
ofstream t2(fou);
int n,m1;
vector< pair<int,int> > m[nmax];
int d[nmax];
class mycomparison
{
public:
bool operator () ( int a,int b)
{
return d[a]>d[b];
}
};
priority_queue<int,vector<int>,mycomparison> q;
int main()
{
freopen("dijkstra.in","r",stdin);
freopen("dijkstra.out","w",stdout);
t1>>n>>m1;
int n1,n2,cst;
for(;m1;m1--)
{
t1>>n1>>n2>>cst;
m[n1].push_back(make_pair(n2,cst));
}
int i;
for(i=1;i<=n;i++) d[i]=inf;
d[1]=0;
int crt;
q.push(1);
while(!q.empty())
{
crt=q.top(); q.pop();
for(i=0;i<m[crt].size();i++)
if( d[m[crt][i].first] > d[crt]+m[crt][i].second)
{
q.push(m[crt][i].first);
d[m[crt][i].first]=d[crt]+m[crt][i].second;
}
}
for(int i=2;i<=n;i++) if(d[i]!=inf) t2<<d[i]<<' ';
else t2<<0<<' ';
fclose(stdin);
fclose(stdout);
return 0;
}