Pagini recente » Cod sursa (job #2077469) | Cod sursa (job #848654) | Cod sursa (job #3005222) | Cod sursa (job #1356192) | Cod sursa (job #1702094)
#include <bits/stdc++.h>
const int DIM = 1 << 10;
using namespace std;
int N, M, K, node1, node2, cost, Marked[DIM], Good[DIM];
struct edge{ int node, cost; }; vector <edge> Edge[DIM]; int List[DIM][DIM];
class input_reader {
private:
FILE *input_file;
static const int SIZE = 1 << 17;
char buffer[SIZE]; int cursor;
inline void advance() {
if( ++cursor == SIZE ) {
cursor = 0;
fread( buffer, SIZE, 1, input_file );
}
return;
}
inline char current() {
return buffer[cursor];
}
public:
input_reader( const char *file_name, const char *file_type ) {
input_file = fopen( file_name, file_type ); cursor = 0;
fread( buffer, SIZE, 1, input_file );
}
input_reader &operator >>( int &value ) {
value = 0;
while( current() < '0' || current() > '9' )
advance();
while( current() >= '0' && current() <= '9' ) {
value = value * 10 + ( current() - '0' );
advance();
}
return *this;
}
} input_file( "pitici.in", "r" );
FILE *output_file = fopen( "pitici.out", "w" );
class nrx_heap{
private:
static const int SIZE = 1 << 10; int heap_size;
struct heap_node{ int val, x, y, cost; } my_heap[SIZE];
void up( int son ) {
int father = son / 2;
if( father && my_heap[son].val > my_heap[father].val ) {
swap( my_heap[son], my_heap[father] );
up( father );
}
return;
}
void down( int father ) {
int son = father * 2;
if( son < heap_size && my_heap[son].val < my_heap[son + 1].val )
son ++;
if( son <= heap_size && my_heap[son].val > my_heap[father].val ) {
swap( my_heap[son], my_heap[father] );
down( son );
}
return;
}
public:
bool empty_heap() { return heap_size == 0; }
int get_best () { return my_heap[1].val; }
int get_size () { return heap_size; }
int get_cost () { return my_heap[1].cost; }
void clear_heap() { heap_size = 0; return; }
int get_value( int position ) {
return my_heap[position].val;
}
void erase_node( int position ) {
int x = my_heap[position].x, y = my_heap[position].y;
int cost = my_heap[position].cost;
swap( my_heap[position], my_heap[heap_size] );
heap_size --;
up( position );
down( position );
if( y < List[x][0] ) {
my_heap[++heap_size] = { List[x][y + 1] + cost, x, y + 1, cost };
up( heap_size );
}
return;
}
void insert_node( int value, int x, int y, int cost ) {
my_heap[++heap_size] = {value, x, y, cost};
up( heap_size );
if( heap_size == K + 1 )
erase_node( 1 );
return;
}
} my_heap;
void DFS( int node ) {
Marked[node] = 1;
if( node == N ) {
Good[node] = 1;
List[N][ ++List[N][0] ] = 0;
return;
}
vector <edge> :: iterator it;
for( it = Edge[node].begin(); it != Edge[node].end(); it ++ ) {
edge next_node = *it;
if( !Marked[next_node.node] )
DFS( next_node.node );
}
my_heap.clear_heap();
for( it = Edge[node].begin(); it != Edge[node].end(); it ++ ) {
edge next_node = *it;
if( Good[next_node.node] ) {
Good[node] = 1;
my_heap.insert_node( List[next_node.node][1] + next_node.cost, next_node.node, 1, next_node.cost );
}
}
for( int i = 1; i <= K && !my_heap.empty_heap(); i ++ ) {
int cost = my_heap.get_cost();
List[node][ ++List[node][0] ] = my_heap.get_best();
my_heap.erase_node(1);
}
sort( List[node] + 1, List[node] + List[node][0] + 1 );
return;
}
int main() {
input_file >> N >> M >> K;
for( int i = 1; i <= M; i ++ ) {
input_file >> node1 >> node2 >> cost;
Edge[node1].push_back( {node2, cost} );
}
DFS( 1 );
sort( List[1] + 1, List[1] + K + 1 );
for( int i = 1; i <= List[1][0]; i ++ )
fprintf( output_file, "%d ", List[1][i] );
return 0;
}