Pagini recente » Cod sursa (job #2822146) | Cod sursa (job #868677) | Cod sursa (job #1755572) | Cod sursa (job #534146) | Cod sursa (job #483986)
Cod sursa(job #483986)
#include<fstream>
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
int BFS(vector<vector<pair<unsigned short,int> > >& graph,
unsigned short n,
unsigned short start,
unsigned short end)
{
queue<pair<unsigned short,int> > Q;
Q.push(make_pair(start,0));
vector<bool> visited;
visited.resize(n+1);
while(!Q.empty())
{
unsigned short node=Q.front().first;
int dist=Q.front().second;
Q.pop();
if(node==end)
return dist;
for(int i=0; i<(int)graph[node].size(); ++i)
{
if(!visited[graph[node][i].first])
{
visited[graph[node][i].first]=true;
if(graph[node][i].first<node)
{
Q.push(make_pair(graph[node][i].first,dist-graph[node][i].second));
}
else
{
Q.push(make_pair(graph[node][i].first,dist+graph[node][i].second));
}
}
}
}
return n;
}
int main()
{
unsigned short n,st,end,x,y;
int m,c;
fstream fin("sate.in", fstream::in);
fstream fout("sate.out", fstream::out);
vector<vector<pair<unsigned short,int> > > graph;
fin>>n>>m>>st>>end;
//cout<<n<<" "<<m<<" "<<st<<" "<<end<<endl;
graph.resize(n+1);
for(int i=0; i<m; ++i)
{
fin>>x>>y>>c;
graph[x].push_back(make_pair(y,c));
graph[y].push_back(make_pair(x,c));
}
/*for(int i=1; i<=n; ++i)
{
cout<<i<<": ";
for(int j=0; j<(int)graph[i].size(); ++j)
{
cout<<graph[i][j].first<<" ";
}
cout<<endl;
}*/
fout<<BFS(graph,n,st,end)<<endl;
fin.close();
fout.close();
return 0;
}