Pagini recente » Cod sursa (job #75567) | Cod sursa (job #3186010) | Cod sursa (job #2308303) | Cod sursa (job #3221385) | Cod sursa (job #360812)
Cod sursa(job #360812)
#include<stdio.h>
#include<vector>
#define NMAX 30001
using namespace std;
int N, M, V[NMAX];
short int X, Y, C[NMAX];
vector<short int> A[NMAX];
vector<int> B[NMAX];
char S[30];
int main()
{ int t, aux,k, in, sf, cost,x,y,i,l;
freopen("sate.in", "r", stdin);
freopen("sate.out", "w", stdout);
scanf("%d%d%hd%hd", &N, &M, &X, &Y);
for(i = 1; i <= M; i++)
{
//scanf("%d%d%d", &x, &y, &cost);
gets(S);
x = y = cost = 0;
for(t = 0; S[t] != ' '; t++)
x = x*10 + S[t]-'0';
//t++;
for(++t; S[t] !=' '; t++)
y = y*10 + S[t]-'0';
//t++;
for(++t; S[t] !=' '; t++)
cost = cost* 10 + S[t]-'0';
A[x].push_back(y);
A[y].push_back(x);
B[x].push_back(cost);
B[y].push_back(cost);
}
in = sf = 1;
C[1] = X;
while(!V[Y])
{
X = C[in++];
//l = A[X].size();
k = 0;
while(k < A[X].size())
{
aux = A[X][k];
if(!V[aux])
{
t = B[X][k];
V[aux] = V[X];
if(X < aux)
V[aux] += t;
else
V[aux] -= t;
sf++;
C[sf] = aux;
}
k++;
}
//in++;
}
printf("%d\n",V[Y]);
return 0;
}