Pagini recente » Cod sursa (job #982041) | Cod sursa (job #2735260) | Cod sursa (job #2923720) | Cod sursa (job #2975211) | Cod sursa (job #1083094)
#include <cstdio>
#include <vector>
#define nmax 30005
FILE *f,*g;
using namespace std;
int n, m, X, Y, ans, SEM;
int VIZ[nmax];
vector < pair <int, int> > L[nmax];
void depth_first(int nod, int from, int cost)
{
VIZ[nod] = 1;
if (nod == Y)
{
ans = cost;
SEM = 1;
}
for (int i = 0 ; i < L[nod].size() && !SEM ; i++)
{
int next = L[nod][i].first;
if (from != next && !VIZ[next])
depth_first(next, nod, cost + L[nod][i].second);
}
}
int main()
{
f=fopen("sate.in","r");
g=fopen("sate.out","w");
fscanf(f,"%d %d %d %d",&n,&m,&X,&Y);
for(int i=1 ; i<=m ; i++)
{
int a, b, c;
fscanf(f,"%d %d %d",&a,&b,&c);
L[a].push_back (make_pair (b, c));
L[b].push_back (make_pair (a, -c));
}
depth_first(X, 0, 0);
fprintf(g,"%d\n",ans);
return 0;
}