Pagini recente » Cod sursa (job #842685) | Cod sursa (job #238920) | Cod sursa (job #1871931) | Cod sursa (job #2169283) | Cod sursa (job #1194507)
#include <fstream>
#include <iostream>
#include <vector>
#include <queue>
#include <stdlib.h>
#define mx 30004
#define inf 2147483647
using namespace std;
int n,m,x,y, grad[mx];
struct muchie
{
int cost;
int dest;
}*Gr[mx];
muchie make_muchie(int c, int d)
{
muchie aux;
aux.cost=c;
aux.dest=d;
return aux;
}
vector< pair<int,int> > G[mx];
int best[mx];
ifstream f("sate.in");
ofstream g("sate.out");
void Initialisation()
{
for(int i=0;i<mx;i++)
{
best[i]=0;
}
}
void Read()
{
f>>n>>m>>x>>y;
Initialisation();
for(int i=0;i<m;i++)
{
int a,b,d;
f>>a>>b>>d;
G[a].push_back( make_pair(b,d) );
G[b].push_back( make_pair(a,-d) );
}
}
void TrickyRead()
{
f>>n>>m>>x>>y;
Initialisation();
for(int i=0;i<m;i++)
{
int a,b,c;
f>>a>>b>>c;
grad[a]++;
grad[b]++;
}
for(int i=1;i<=n;i++)
{
Gr[i]= (muchie *)malloc(grad[i]*sizeof(muchie));
grad[i]=0;
}
f.close();
ifstream f("sate.in");
for(int i=0;i<m;i++)
{
int a,b,c;
f>>a>>b>>c;
G[a][grad[b]++] = make_muchie(c,b);
G[b][grad[a]++] = make_muchie(-c,b);
}
}
void TrickyBFSit(int start, int fin)
{
int Q[mx], left, right;
left=0;
right=1;
Q[right]=start;
for(;left<right;left++)
{
for(int *i=Gr[left];i<grad[left];i++)
{
if(!best[*i])
{
right++;
Q[right]=*i;
best[*i]=Gr[left][i].cost;
}
}
left++;
}
}
void BFSit(int start, int fin)
{
queue<int> Q;
int curent=-1;
for(Q.push(start);Q.size() && (curent!=fin);Q.pop())
{
curent=Q.front();
for(int i=0;i<G[curent].size();i++)
{
if(!best[ G[curent][i].first ])
{
best[G[curent][i].first]=best[curent]+G[curent][i].second;
Q.push(G[curent][i].first);
}
}
}
}
void BFSrec(int curent)
{
for(int i=0;i<G[curent].size();i++)
{
if(best[ G[curent][i].first ]==0)
{
best[G[curent][i].first]=best[curent]+G[curent][i].second;
BFSrec(G[curent][i].first);
}
}
}
int main()
{
Read();
BFSit(x,y);
g<<best[y];
return 0;
}