Pagini recente » Cod sursa (job #1437463) | Cod sursa (job #1370644) | Cod sursa (job #1312298) | Cod sursa (job #3170237) | Cod sursa (job #343238)
Cod sursa(job #343238)
#include<cstdio>
#define N 30001
#define M 100025
#include<vector>
using namespace std;
int m,d[N];
short int n,x1,y1;
vector <short int>a[N],a1[N];
bool b[N];
void citire()
{
freopen("sate.in","r",stdin);
freopen("sate.out","w",stdout);
scanf("%hd%d%hd%hd",&n,&m,&x1,&y1);
short int f,g,c;
for (int i=1; i<=m; ++i)
{
scanf("%hd%hd%hd",&f,&g,&c);
a[f].push_back(g);
a[g].push_back(f);
a1[f].push_back(c);
a1[g].push_back(-c);
}
}
void bfs()
{
int coada[N],u=0,p=0,x,y;
coada[u++]=x1;
b[x1]=true;
while (u!=p)
{
x=coada[p++];
int g=a[x].size();
for (int i=0; i<g; ++i)
{
y=a[x][i];
if (!b[y])
{
b[y]=true;
coada[u++]=y;
d[y]=a1[x][i]+d[x];
}
if(y==y1)
return;
}
}
}
int main()
{
citire();
bfs();
printf("%d",d[y1]);
return 0;
}