Pagini recente » Cod sursa (job #1484607) | Cod sursa (job #2717541) | Cod sursa (job #1192795) | Cod sursa (job #489516) | Cod sursa (job #2858208)
#include <iostream>
#include <vector>
#define MAXN 30000
using namespace std;
struct edge{
int pos, cost;
};
struct nodes{
int dist;
vector <edge> edges;
bool visited;
};
nodes graph[MAXN + 1];
static inline void addEdge(int a, int b, int cost) {
graph[a].edges.push_back({b, cost});
graph[b].edges.push_back({a, cost});
}
void dfs(int pos) {
graph[pos].visited = true;
for ( edge node : graph[pos].edges ) {
if ( !graph[node.pos].visited ) {
if ( node.pos < pos ) {
graph[node.pos].dist = graph[pos].dist - node.cost;
dfs(node.pos);
} else {
graph[node.pos].dist = graph[pos].dist + node.cost;
dfs(node.pos);
}
}
}
}
int main() {
FILE *fin, *fout;
fin = fopen("sate.in", "r");
fout = fopen("sate.out", "w");
int n, m, a, b, c, i, start, finish;
fscanf(fin, "%d%d%d%d", &n, &m, &start, &finish);
for ( i = 0; i < m; i++ ) {
fscanf(fin, "%d%d%d", &a, &b, &c);
addEdge(a, b, c);
}
dfs(start);
fprintf(fout, "%d\n", graph[finish].dist);
fclose(fin);
fclose(fout);
return 0;
}