Pagini recente » Cod sursa (job #236486) | Cod sursa (job #2893045) | Cod sursa (job #1447192) | Cod sursa (job #1134950) | Cod sursa (job #1458821)
#include <queue>
#include <iostream>
#include <vector>
#include <cstdio>
#include <stack>
#define INF -666013
using namespace std;
class AdjNode {
private:
int _v;
int _weight;
public:
AdjNode(int v, int weight) {
_v = v;
_weight = weight;
}
int getNode() {
return this->_v;
}
int getWeight() {
return this->_weight;
}
};
class Sate {
private:
vector<AdjNode> *_adjList;
int _nodesNr;
public:
Sate(int V) {
_nodesNr = V;
_adjList = new vector<AdjNode>[_nodesNr + 2];
}
~Sate() {
delete[] _adjList;
}
void addEdge(int u, int v, int weight) {
AdjNode node(v,weight);
_adjList[u].push_back(node);
}
int BFS(int src_node, int dst_node) {
int cost = 0;
bool *visited = new bool[this->_nodesNr + 2];
for(int i = 1; i <= this->_nodesNr + 2; ++i) {
visited[i] = false;
}
queue<int> Q;
visited[src_node] = true;
Q.push(src_node);
while(!Q.empty()) {
int node = Q.front(); Q.pop();
for(vector<AdjNode>::iterator it = _adjList[node].begin(); it != _adjList[node].end(); ++it) {
if(!visited[it->getNode()]) {
visited[it->getNode()] = true;
Q.push(it->getNode());
cost += it->getWeight();
if(it->getNode() == dst_node) {
return cost;
}
}
}
}
return INF;
}
};
int main(int argc, char const *argv[])
{
FILE *f, *fwr;
if((f = fopen("sate.in","r")) == NULL) {
fprintf(stderr, "Can't open file!\n");
return 0;
}
fwr = fopen("sate.out", "w");
int N, M, X, Y, x, y, w;
fscanf(f,"%d %d %d %d", &N, &M, &X, &Y);
// fscanf(f, "%d %d", &N, &M);
//cout << N << ' ' << M << ' ' << X << ' ' << Y << endl;
Sate g(N);
for(int i = 0 ; i < M; i++) {
fscanf(f,"%d %d %d", &x, &y, &w);
g.addEdge(x,y,w);
g.addEdge(y,x,-w);
}
// cout << g.BFS(X,Y) << endl;
fprintf(fwr, "%d\n", g.BFS(X,Y));
fclose(f);
fclose(fwr);
return 0;
}