Pagini recente » Cod sursa (job #553794) | Cod sursa (job #435808) | Cod sursa (job #2242038) | Cod sursa (job #1846767) | Cod sursa (job #1482103)
#include <cstdio>
#include <vector>
#define NMAX 300023
#define DIM 10000
using namespace std;
char buff[DIM];
int poz=0;
void next()
{
++poz;
if(poz>=DIM)
{
poz=0;
fread(buff,1,DIM,stdin);
}
}
int citeste()
{
char semn='+';
while((buff[poz]!='-')&&(!('0'<=buff[poz]&&buff[poz]<='9')))
{
next();
}
if(buff[poz]=='-')
{
semn='-';
next();
}
int nr=0;
while('0'<=buff[poz]&&buff[poz]<='9')
{
nr*=10;
nr+=(buff[poz]-'0');
next();
}
if(semn=='-') nr*=(-1);
return nr;
}
int q[NMAX],cst[NMAX],vis[NMAX];
vector <int> adj[NMAX],cost[NMAX];
int n,m,st,dr,nod1,nod2;
void bfs()
{
int nod=q[st];
st++;
if(st>dr) return;
vector<int>::iterator it1,it2;
for(it1=adj[nod].begin(),it2=cost[nod].begin();it1!=adj[nod].end();++it1,++it2)
{
if(vis[*it1]==0)
{
q[dr]=*it1;
dr++;
vis[*it1]=1;
cst[*it1]=*it2+cst[nod];
if(*it1==nod2) return;
}
}
bfs();
}
int main()
{
freopen ("sate.in","r",stdin);
freopen ("sate.out","w",stdout);
fread(buff,1,DIM,stdin);
n=citeste();
m=citeste();
nod1=citeste();
nod2=citeste();
int x,y,c;
for(int i=0;i<m;i++)
{
x=citeste();
y=citeste();
c=citeste();
adj[x].push_back(y);
cost[x].push_back(c);
adj[y].push_back(x);
cost[y].push_back(-c);
}
cst[nod1]=0;
q[1]=nod1;
vis[nod1]=1;
st=1;
dr=2;
bfs();
printf("%d\n",cst[nod2]);
}