Pagini recente » Cod sursa (job #303235) | Cod sursa (job #3257596) | Cod sursa (job #1605594) | Cod sursa (job #671101) | Cod sursa (job #1509691)
#include <cstdio>
using namespace std;
FILE *f=fopen("sate.in", "r");
FILE *g=fopen("sate.out", "w");
const int nmax=30001;
const int oo=(1<<31)-1;
int n, m, x, y, viz[nmax], d[nmax];
int xi, xf;
struct nod
{
int info, cost;
nod*urm;
}*prim[nmax];
void AddNod(int x, int y, int c)
{
nod *p=new nod;
p->info=y;
p->cost=c;
p->urm=prim[x];
prim[x]=p;
}
void Citire()
{
int i, D;
fscanf(f, "%d%d%d%d", &n, &m, &xi, &xf);
for(i=1; i<=m; i++)
{
fscanf(f, "%d%d%d", &x, &y, &D);
AddNod(x, y, D);
AddNod(y, x, -D);
}
}
void BFS(int xi)
{
nod *p;
int pr, ul, c[nmax], xc;
c[1]=xi;
pr=ul=1;
while(pr<=ul)
{
xc=c[pr];
pr++;
for(p=prim[xc]; p!=0; p=p->urm)
{
if(viz[p->info]==0)
{
//if(d[p->info]>=d[xc]+p->cost)
{
d[p->info]=d[xc]+p->cost;
viz[p->info]=1;
ul++;
c[ul]=p->info;
}
}
}
}
}
int main()
{
Citire();
BFS(xi);
fprintf(g, "%d", d[xf]);
fclose(f);
fclose(g);
return 0;
}