Pagini recente » Cod sursa (job #1543518) | Cod sursa (job #3220334) | Cod sursa (job #2789431) | Cod sursa (job #1588013) | Cod sursa (job #521458)
Cod sursa(job #521458)
#include<queue>
#include<vector>
#include<fstream>
using namespace std;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
const int N=50200;
const int INF=10000000;
vector<int>a[N],c[N];
bool inc[N];
int n,m;
void read()
{
int x,y,z;
in>>n>>m;
for(int i=1;i<=m;i++)
{
in>>x>>y>>z;
a[x].push_back(y);
c[x].push_back(z);
}
}
int d[N],q[N];
queue<int>coada;
void bf()
{
// bool bn=1;
for(int i=1;i<=n;i++)
d[i]=INF;
int x,cost,r;
d[1]=0;
coada.push(1);
inc[1] = true;
while(!coada.empty())
{
r=coada.front();
coada.pop();
inc[r] = false;
unsigned int du=a[r].size();
for(int i=0;i<du;i++)
{
x = a[r][i];
cost = c[r][i];
if(d[r]+cost < d[x])
{
d[x] = d[r]+cost;
if(!inc[x])
{
inc[x] = true;
q[x]++;
//if(q[x]>n-1)
//bn=false;
coada.push(x);
}
}
}
}
//return bn;
}
int main()
{
read();
//bf();
for(int i=2;i<=n;i++)
if(d[i]!=INF)
out<<d[i]<<" ";
else
out<<"0 ";
return 0;
}