Pagini recente » Cod sursa (job #238) | Cod sursa (job #3224746) | Cod sursa (job #2339941) | Cod sursa (job #2138554) | Cod sursa (job #1438759)
#include <iostream>
#include <fstream>
#include <vector>
#include <utility>
using namespace std;
ifstream f("sate.in");
ofstream g("sate.out");
vector <pair <int, int> > G[30005];
bool viz[30005];
int distanta[30005];
void DFS(int x, int y, int ok)
{
viz[x]=1;
for(int i=0;i<G[x].size();i++)
if(viz[G[x][i].first]==0 && ok==0)
{
distanta[G[x][i].first]=distanta[x]+G[x][i].second;
if(G[x][i].first!=y)
DFS(G[x][i].first,y,ok);
else ok=1;
}
}
int main()
{
int n,m,i,a,b,c,x,y;
f>>n>>m>>x>>y;
for(i=1;i<=m;i++)
{
f>>a>>b>>c;
G[a].push_back(make_pair(b,c));
G[b].push_back(make_pair(a,-c));
}
/*for(i=1;i<=n;i++)
{
for(int j=0;j<G[i].size();j++)
cout<<'('<<G[i][j].first<<','<<G[i][j].second<<"), ";
cout<<endl;
}*/
DFS(x,y,0);
g<<distanta[y];
}