Pagini recente » Cod sursa (job #3130005) | Cod sursa (job #827551) | Cod sursa (job #811338) | Cod sursa (job #926534) | Cod sursa (job #626148)
Cod sursa(job #626148)
#include <fstream>
#include <vector>
using namespace std;
const int N=30005,M=100002,inf=1<<30;
struct muchie{int x,c;};
int v[M],dist[N],n;
bool use[N];
vector<muchie> a[N];
ifstream in("sate.in");
ofstream out("sate.out");
inline void push(int x)
{
v[++v[0]]=x;
}
inline void pop(int& val)
{
val=v[v[0]--];
}
void bfs(int x)
{
int i,y,c;
for (i=1;i<=n;i++)
dist[i]=inf;
dist[x]=0;
push(x);
while (v[0])
{
pop(x);
if (use[x])
continue;
use[x]=true;
for (vector<muchie>::iterator i=a[x].begin();i!=a[x].end();i++)
{
y=(*i).x;
c=(*i).c;
if (dist[y]==inf)
{
dist[y]=dist[x]+c;
push(y);
}
}
}
}
int main()
{
int m,x,y,c,X,Y;
in>>n>>m>>X>>Y;
while (m--)
{
in>>x>>y>>c;
if (x>y)
c=-c;
a[x].push_back((muchie){y,c});
a[y].push_back((muchie){x,-c});
}
bfs(X);
out<<dist[Y]<<"\n";
return 0;
}