Pagini recente » Cod sursa (job #15210) | Cod sursa (job #1070137) | Cod sursa (job #123193) | Cod sursa (job #2366408) | Cod sursa (job #761992)
Cod sursa(job #761992)
#include<fstream>
#include<stdlib.h>
#include<stdio.h>
#define nmax 30004
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
int i, j ,n , m , x, y, k ;
bool viz[nmax];
int *A[nmax] , *B[nmax];
struct nod{int inf; nod *urm;} ;
int c[nmax *100];
int dis[nmax];
int bfs(int x,int y)
{
int i, j;
int p = 1;
int u = 1;
if(x>y)
swap(x,y);
viz[x] = true;
c[1] = x;
while(dis[y] == 0)
{
for(i = 1, x = c[p]; i <= A[x][0] ; i ++)
{
if(viz[A[x][i]] == false )
{
viz[c[++u] = A[x][i]] = true;
dis[A[x][i]] = dis[x] + B[x][i];
if(dis[y])
return dis[y];
}
}
++p;
}
return dis[y] ;
}
void read()
{
int i,j , d;
fin >> n >> m >> x >>y;
for(i = 1; i <= n ;i++)
{
A[i] = (int *) realloc(A[i], sizeof(int));
A[i][0] = 0;
B[i] = (int *) realloc(B[i], sizeof(int));
B[i][0] = 0;
}
for(k =1; k <= m ;k++)
{
fin >> i >> j >>d;
A[i][0]++;
A[i] = (int *) realloc(A[i] , (A[i][0] + 1 ) * sizeof(int));
B[i] = (int *) realloc(B[i] , (A[i][0] + 1 ) * sizeof(int));
A[j][0] ++;
A[j] = (int *) realloc(A[j], (A[j][0] + 1) * sizeof(int));
B[j] = (int *) realloc(B[j], (A[j][0] + 1) * sizeof(int));
B[i][A[i][0]] = d;
B[j][A[j][0]] = -d;
A[i][A[i][0]] = j;
A[j][A[j][0]] = i;
}
//fout << bfs(x,y);
fout << bfs(x,y);
// fout<< dis[i] <<" " ;
}
int main()
{
read();
fin.close();
return 0;
}