Mai intai trebuie sa te autentifici.
Cod sursa(job #2854600)
Utilizator | Data | 21 februarie 2022 15:25:25 | |
---|---|---|---|
Problema | Algoritmul Bellman-Ford | Scor | 0 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 1.23 kb |
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
const int MAXN = 5e4;
const int INF = 1e7;
ifstream fin( "camionas.in" );
ofstream fout( "camionas.out" );
struct Edge{
int node;
int cost;
};
vector <Edge> graph[MAXN+1];
queue <int> bfQueue;
int dist[MAXN+1];
int counter[MAXN+1];
int n;
void bf( int node ) {
int i;
for( i = 0; i < n; i++ )
dist[i] = INF;
bfQueue.push( node );
counter[node]++;
dist[node] = 0;
while( !bfQueue.empty() ) {
int qfront = bfQueue.front();
for( auto edge: graph[qfront] ) {
if( dist[qfront] + edge.cost < dist[edge.node] ) {
dist[edge.node] = dist[qfront] + edge.cost;
if( counter[edge.node] == 0 ) {
counter[edge.node]++;
bfQueue.push( edge.node );
}
}
}
counter[qfront]--;
bfQueue.pop();
}
}
int main() {
int m, g, i, x, y, cost;
fin >> n >> m >> g;
for( i = 0; i < m; i++ ) {
fin >> x >> y >> cost;
x--;
y--;
if( cost >= g )
cost = 0;
else
cost = 1;
graph[x].push_back( { y, cost } );
graph[y].push_back( { x, cost } );
}
bf( 0 );
fout << dist[n-1];
return 0;
}