Pagini recente » Istoria paginii runda/iconcurs14/clasament | Cod sursa (job #202433) | Istoria paginii runda/knird/clasament | Istoria paginii runda/sim0002 | Cod sursa (job #2556543)
#include <cstdio>
#include <vector>
#include <cstring>
#include <string>
#include <iostream>
#include <queue>
using namespace std;
const int NMAX = 30000;
vector <pair <int , int> > G[NMAX + 5];
int d[NMAX + 5] , n , m , start , finish;
bool viz[NMAX + 5];
string s;
queue <int> q;
void bfs(int u)
{
viz[u] = 1;
q.push(u);
while(!q.empty())
{
int u = q.front();
q.pop();
for(int j = 0 ; j < G[u].size() ; j ++)
{
int v = G[u][j].first;
if(viz[v] == 0)
{
d[v] = d[u] + G[u][j].second;
viz[v] = 1;
q.push(v);
if(v == finish)
return;
}
}
}
}
int main()
{
freopen("sate.in" , "r" , stdin);
freopen("sate.out" , "w" , stdout);
int x , y , z , i , j;
scanf("%d%d%d%d\n" , &n , &m , &start , &finish);
for( ; m ; m --)
{
getline(cin , s);
s.push_back(' ');
x = y = z = j = 0;
while(s[j] != ' ')
x = x * 10 + s[j ++] - '0';
j ++;
while(s[j] != ' ')
y = y * 10 + s[j ++] - '0';
j ++;
while(s[j] != ' ')
z = z * 10 + s[j ++] - '0';
G[x].push_back(make_pair(y , z));
G[y].push_back(make_pair(x , -z));
}
bfs(start);
printf("%d\n" , d[finish]);
return 0;
}