Pagini recente » Cod sursa (job #2667065) | Cod sursa (job #3178240) | Cod sursa (job #133854) | Cod sursa (job #1163267) | Cod sursa (job #818891)
Cod sursa(job #818891)
#include <cstdio>
const int MAX_SIZE(30001);
struct edge
{
int vertex;
int cost;
struct edge *next;
} *graph [MAX_SIZE];
int queue [MAX_SIZE];
int cost [MAX_SIZE];
int n, m, start, finish;
inline void add (int x, int y, int cost)
{
struct edge *new_edge(new struct edge);
new_edge->vertex = y;
new_edge->cost = cost;
new_edge->next = graph[x];
graph[x] = new_edge;
}
inline void read (void)
{
std::freopen("sate.in","r",stdin);
std::scanf("%d%d%d%d",&n,&m,&start,&finish);
int x, y, cost;
for (int counter(0) ; counter < m ; ++counter)
{
std::scanf("%d%d%d",&x,&y,&cost);
add(x,y,cost);
add(y,x,cost);
}
std::fclose(stdin);
}
inline void print (void)
{
std::freopen("sate.out","w",stdout);
std::printf("%d\n",cost[finish] - 1);
std::fclose(stdout);
}
inline void BreadthFirstSearch (void)
{
*queue = start;
cost[start] = 1;
int *push(queue + 1), *pop(queue), vertex, new_cost;
struct edge *iterator;
while (pop < push)
{
vertex = *pop;
++pop;
for (iterator = graph[vertex] ; iterator ; iterator = iterator->next)
{
if (cost[iterator->vertex])
continue;
new_cost = cost[vertex];
if (iterator->vertex > vertex)
new_cost += iterator->cost;
else
new_cost -= iterator->cost;
cost[iterator->vertex] = new_cost;
if (iterator->vertex == finish)
break;
*push = iterator->vertex;
++push;
}
}
}
int main (void)
{
read();
BreadthFirstSearch();
print();
return 0;
}