Pagini recente » Cod sursa (job #971855) | Cod sursa (job #1040731) | Cod sursa (job #3144297) | Cod sursa (job #2729714) | Cod sursa (job #2764440)
///#include <iostream>
#include <fstream>
#include <deque>
#include <map>
#include <vector>
const int SIZE = 32e3;
using namespace std;
typedef pair<int,int> pii;
ifstream cin("sate.in");
ofstream cout("sate.out");
void readit();
void BFS();
void coutVec();
int n, m, start, fin;
map <int, int> v[SIZE];
int main()
{
readit();
BFS();
return 0;
}
void readit()
{
int y, x, c;
cin>>n>>m>>start>>fin;
for(int i=1; i<=m; i++)
cin>>y>>x>>c,
v[y][x]= c,
v[x][y]=-c;
}
void coutVec()
{
for(int i=1; i<=n; i++)
{
cout<<i<<": \n";
for(auto el : v[i])
cout<<"\t"<<el.first<<" = "<<el.second<<"\n";
}
}
void BFS()
{
pii now;
deque <pii> bfsq;
for(auto el : v[start])
bfsq.push_back({start, el.first});
while(!bfsq.empty())
{
now=bfsq.front();
bfsq.pop_front();
for(auto nxt : v[now.second])
if(!v[now.first][nxt.first] && now.first!=nxt.first)
{
v[now.first][nxt.first]=v[now.first][now.second]+v[now.second][nxt.first];
v[nxt.first][now.first]=-v[now.first][nxt.first];
bfsq.push_back({now.first, nxt.first});
}
if(v[start][fin])
{
cout<<v[start][fin];
break;
}
}
}