Pagini recente » Istoria paginii runda/igorj_11/clasament | Cod sursa (job #1503104) | Cod sursa (job #990775) | Cod sursa (job #1351519) | Cod sursa (job #767040)
Cod sursa(job #767040)
#include<iostream>
#include<vector>
#include<cstring>
#include<queue>
#include<fstream>
using namespace std;
struct s{int v,c;};
vector <s> a[75001];
int n,m,X,Y,x,y,dist[75001];
void alg( int nod)
{
int i, cost, nodc;
queue <int >q;
q.push(nod);
while (!q.empty())
{
nod=q.front();
q.pop();
for (i=0;i<a[nod].size();i++)
{
nodc=a[nod][i].v;
cost=a[nod][i].c;
if (nodc<nod)
cost=cost-2*cost;
if (dist[nodc]==0 || dist[nodc]>dist[nod]+cost)
{
dist[nodc]=dist[nod]+cost;
q.push(nodc);
}
}
}
}
int main(void)
{
fstream f,g;
f.open("sate.in",ios::in);
g.open("sate.out",ios::out);
f>>n>>m>>X>>Y;
int i,nr;
s z,l;
char lin[400];
f.get();
f.get();
for (i=1;i<=m;i++)
{
int ok=0,qw[4]={0};
nr=0;
f.getline(lin,500);
while (ok<3)
{
if (isdigit(lin[nr]))
{
ok++;
do
{
qw[ok]= qw[ok]*10+lin[nr]-48;
nr++;
}while (isdigit(lin[nr])
);
}
else
nr++;
}
x=qw[1];
z.v=qw[2];
z.c=qw[3];
l.v=x;
l.c=z.c;
a[x].push_back(z);
a[z.v].push_back(l);
}
alg(X);
g<<dist[Y];
}