Pagini recente » Cod sursa (job #1450635) | Istoria paginii runda/12334/clasament | Cod sursa (job #1008118) | Cod sursa (job #1650834) | Cod sursa (job #1805084)
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#define MaxN 7505
#define INF 2140000000
#define MAX 131072
using namespace std;
FILE *IN,*OUT;
int pos=0,N,M,X,Y,Z,Alpha,Beta,Cost[MaxN],Nodes=0;
queue <int> Q;
vector <pair<int,int>> Graph[MaxN];
char f[MAX];
void Read(int &nr)
{
nr=0;
while(f[pos]>'9'||f[pos]<'0')
{
pos++;
if(pos==MAX)
pos=0,fread(f,1,MAX,IN);
}
while(f[pos]<='9'&&f[pos]>='0')
{
nr=nr*10+f[pos++]-'0';
if(pos==MAX)
pos=0,fread(f,1,MAX,IN);
}
}
void BFS(int node,int *v)
{
v[node]=0;
Q.push(node);
while(!Q.empty())
{
for(int i=0;i<Graph[Q.front()].size();i++)
if(v[Graph[Q.front()][i].first]>v[Q.front()]+Graph[Q.front()][i].second)
v[Graph[Q.front()][i].first]=v[Q.front()]+Graph[Q.front()][i].second,Q.push(Graph[Q.front()][i].first);
Q.pop();
}
}
int main()
{
IN=fopen("sate.in","r");
OUT=fopen("sate.out","w");
fread(f,1,MAX,IN);
Read(N),Read(M),Read(Alpha),Read(Beta);
for(int i=1;i<=M;i++)
{
Read(X),Read(Y),Read(Z);
Graph[X].push_back(make_pair(Y,Z));
Graph[Y].push_back(make_pair(X,-Z));
}
for(int i=1;i<=N;i++)
Cost[i]=INF;
BFS(Alpha,Cost);
fprintf(OUT,"%d",Cost[Beta]);
return 0;
}