Pagini recente » Cod sursa (job #1783509) | Cod sursa (job #2980711) | Cod sursa (job #729875) | Cod sursa (job #1959579) | Cod sursa (job #2908222)
#include <fstream>
#include <climits>
#include <vector>
#include <queue>
#include <string.h>
#define NMAX 1005
using namespace std;
ifstream f("ciclu.in");
ofstream g("ciclu.out");
int n, m, x, y, cost, left1, right1, answerProblem;
int timesInQueue[NMAX];
long long int answer[NMAX];
vector < pair <int, int> > v[NMAX];
queue <int> que;
bool isCycle(int scad) {
memset(timesInQueue, 0, sizeof(timesInQueue));
while (!que.empty())
que.pop();
for (int i = 1; i <= n; i++) {
answer[i] = INT_MAX / 2;
}
que.push(0);
while (!que.empty()) {
int node = que.front();
que.pop();
for (auto neighbour : v[node]) {
if (answer[neighbour.first] > answer[node] + neighbour.second - scad) {
answer[neighbour.first] = answer[node] + neighbour.second - scad;
que.push(neighbour.first);
timesInQueue[neighbour.first]++;
if (timesInQueue[neighbour.first] == n)
return 1;
}
}
}
return 0;
}
int main()
{
f >> n >> m;
for (int i = 1; i <= m; i++) {
f >> x >> y >> cost;
v[x].push_back({y, cost * 100});
right1 = max(right1, cost * 100);
}
for (int i = 1; i <= n; i++)
v[0].push_back({i, 0});
while (left1 <= right1) {
int mid = (left1 + right1) / 2;
if (isCycle(mid)) {
right1 = mid - 1;
answerProblem = mid;
} else {
left1 = mid + 1;
}
}
answerProblem--;
g << answerProblem / 100 << "." << answerProblem % 100 << "\n";
return 0;
}