Pagini recente » Cod sursa (job #1643352) | Cod sursa (job #1093658) | Cod sursa (job #1749212) | Cod sursa (job #1317178) | Cod sursa (job #1803235)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
class InputReader {
public:
InputReader() {}
InputReader(const char *file_name) {
input_file = fopen(file_name, "r");
cursor = 0;
fread(buffer, SIZE, 1, input_file);
}
inline InputReader &operator >>(int &n) {
while(buffer[cursor] < '0' || buffer[cursor] > '9') {
advance();
}
n = 0;
while('0' <= buffer[cursor] && buffer[cursor] <= '9') {
n = n * 10 + buffer[cursor] - '0';
advance();
}
return *this;
}
private:
FILE *input_file;
static const int SIZE = 1 << 17;
int cursor;
char buffer[SIZE];
inline void advance() {
++ cursor;
if(cursor == SIZE) {
cursor = 0;
fread(buffer, SIZE, 1, input_file);
}
}
};
InputReader f ("sate.in");
ofstream t ("sate.out");
struct edges{
int dest,cost;};
vector <edges> v[30010];
int n,m,cost[30010];
bool vaz[30010];
int bfs(int nod,int target){int curent;
queue <int> q;
q.push(nod);
while (!q.empty()){
curent=q.front();
vaz[curent]=true;
q.pop();
for (auto i:v[curent])
if (!vaz[i.dest]){
q.push(i.dest);
if (i.dest>curent)
cost[i.dest]=cost[curent]-i.cost;
else
cost[i.dest]=cost[curent]+i.cost;
}
}
return 0;
}
int main()
{
int x,y,t1,t2,c;
f>>n>>m>>x>>y;
for (int i=0;i<m;++i)
f>>t1>>t2>>c,v[t1].push_back({t2,c}),v[t2].push_back({t1,c});
if (x>y) swap (x,y);
bfs(x,y);
t<<cost[x]-cost[y];
return 0;
}