Pagini recente » Cod sursa (job #1929560) | Cod sursa (job #1961037) | Cod sursa (job #1365833) | Cod sursa (job #2756711) | Cod sursa (job #1316979)
#include <stdio.h>
#include <vector>
#define DIM 10000
char buff[DIM];
int poz=0;
void citeste(int &numar)
{
numar = 0;
//cat timp caracterul din buffer nu e cifra ignor
while (buff[poz] < '0' || buff[poz] > '9')
//daca am "golit" bufferul atunci il umplu
if (++poz == DIM)
fread(buff,1,DIM,stdin),poz=0;
//cat timp dau de o cifra recalculez numarul
while ('0'<=buff[poz] && buff[poz]<='9')
{
numar = numar*10 + buff[poz] - '0';
if (++poz == DIM)
fread(buff,1,DIM,stdin),poz=0;
}
}
std::vector<int> *adj,*cost;
int n,m,x,y;
bool ver[30001];
bool as=0;
void dfs(int sat,int counter)
{
int a,b;
ver[sat]=1;
if(sat==y)
{
printf("%d\n",counter);
as=1;
return;
}
std::vector<int>::iterator it1,it2;
for(it2=cost[sat].begin(),it1=adj[sat].begin();it1!=adj[sat].end();++it1,++it2)
{
a=*it1;
if(ver[a]==0)
{
b=*it2;
dfs(a,counter+b);
if(as==1) return;
}
}
ver[sat]=0;
}
int main()
{
freopen ("sate.in","r",stdin);
freopen ("sate.out","w",stdout);
citeste(n);
citeste(m);
citeste(x);
citeste(y);
int p1,p2,c;
int temp;
adj=new std::vector<int> [n+1];
cost=new std::vector<int> [n+1];
for(int i=1;i<=m;i++)
{
citeste(p1);
citeste(p2);
citeste(c);
if(p1>p2)
{
temp=p1;
p1=p2;
p2=temp;
}
adj[p1].push_back(p2);
adj[p2].push_back(p1);
cost[p1].push_back(c);
c*=(-1);
cost[p2].push_back(c);
}
dfs(x,0);
}