Description
| |
Problem H |
Halum |
Time Limit: 3 seconds |
|
| |
You are given a directed graph G (V, E) with a set of vertices and edges. each edge (I, j) that connects some vertex I to vertex J has an integer cost associated with that edge. Define the operation halum (v, d) to operate on a vertex v using an integer d as follows: subtract D from the cost of all edges that enter V and add D to the cost of every edge that leaves v.
As an example of that operation, consider graph G that has three vertices named (1, 2, 3) and two edges. edge (1, 2) has cost-1, and edge (2, 3) has cost 1. the operation halum (2,-3) operates on edges entering and leaving vertex 2. thus, edge (1, 2) gets cost-1-(-3) = 2 and the edge (2, 3) gets cost 1 + (-3) =-2. Your goal is to apply the halum function to a graph, potentially repeatedly, until every edge in the graph has at least a certain cost that is greater than zero. You have to maximize this cost. |
|
| |
Input |
|
|
| |
Two space-separated integers per case: V (v ≤ 500) ande (e ≤ 2700 ). E lines follow. each line represents a directed edge using three space-separated integers (u, v, D ). absolute value of cost can be at most 10000. |
|
| |
|
|
| |
Output |
|
| |
If the problem is solvable, then print the maximum possible value. If there is no such solution print "no solution". If the value can be arbitrary large print "infinite" |
|
| |
|
|
| |
Sample Input |
Sample output |
|
|
| |
2 1 1 2 10 2 1 1 2-10 3 3 1 2 4 2 3 2 3 1 5 4 5 2 3 4 4 2 5 3 4 2 3 1 0 1 2-1
|
Infinite Infinite 3 1
|
Given a directed graph, each edge has a weight. You can select a node v and an integer d each time, reduce the weight of all edges whose end points are V by D, and increase the weight of all edges whose end points are V by D, finally, we need to make the minimum values of all edges non-negative and as big as possible.
Idea: the minimum value is the largest. Obviously, sum (u) can be used to represent the sum of all d s acting on node u, in this case, the goal of the question is to determine all sum (U). For edge a-> B, it is not difficult to find that the weight after the operation is W (A, B) + sum (a)-sum (B)> = x
Then we can obtain an inequality: sum (B)-sum (a) <= W (a, B)-X, while W (A, B) -X is known, so we can get the most short-circuited
Inequality d [v] <= d [u] + W (u, v), so we can build a new graph, so when we are doing Bellman-Ford, if a negative weight ring is found, we cannot obtain an inequality similar to the shortest path.
#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>#include <queue>#include <vector>using namespace std;const int maxn = 505;const int inf = 0x3f3f3f3f;struct Edge {int from, to, dist;};struct BellmanFord {int n, m;vector<Edge> edges;vector<int> G[maxn];int inq[maxn];int d[maxn];int p[maxn];int cnt[maxn];void init(int n) {this->n = n;for (int i = 0; i < n; i++)G[i].clear();edges.clear();}void AddEdge(int from, int to, int dist) {edges.push_back((Edge){from, to, dist});m = edges.size();G[from].push_back(m-1);}bool negativeCycle(int tmp) {queue<int> Q;memset(inq, 0, sizeof(inq));memset(cnt, 0, sizeof(cnt));for (int i = 0; i < n; i++) {d[i] = 0;inq[0] = 1;Q.push(i);}while (!Q.empty()) {int u = Q.front();Q.pop();inq[u] = 0;for (int i = 0; i < G[u].size(); i++) {Edge &e = edges[G[u][i]];if (d[e.to] > d[u] + e.dist - tmp) {d[e.to] = d[u] + e.dist - tmp;p[e.to] = G[u][i];if (!inq[e.to]) {Q.push(e.to);inq[e.to] = 1;if (++cnt[e.to] > n)return 1;}}}}return 0;}};BellmanFord solve;int n, m;int main() {int u, v, w;while (scanf("%d%d", &n, &m) != EOF) {solve.init(n);int l = 1, r = 0, mid;for (int i = 0; i < m; i++) {scanf("%d%d%d", &u, &v, &w);u--, v--;solve.AddEdge(u, v, w);r = max(r, w);}if (solve.negativeCycle(1))printf("No Solution\n");else if (!solve.negativeCycle(r+1))printf("Infinite\n");else {while (l < r) {mid = l + (r-l+1)/2;if (solve.negativeCycle(mid))r = mid-1;else l = mid;}printf("%d\n", l);}}return 0;}