Problem UVA - 11478 - Halum
Time Limit: 3000 mSec
Problem Description
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) and E (E ≤ 2700). E lines follow. Each line represents a directe
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
Infinite
Infinite
3
1
题解:二分+差分约束系统,解决方法很固定,跑个spfa判负环就能判断差分约束系统是否有解,这个题是让最小边权最大,所以显然二分,还加了一点说是要边权大于0,所以先判断最小边权是1行不行,不行的话显然就是无解得情况,再判断最小边权是所有边中最大的+1行不行,如果可以,说明根本就没有回路,这时最小权值想多大就多大(让入度为0的点增加INF),所以是无穷大的情况,除此之外就是二分了,没什么难度。还有个地方需要说明一下,从代码可以看出这和普通的spfa有一点区别就是没有源点,看似是个大问题,其实很容易理解,差分约束系统其实一般是要添加一个源点的,从源点到所有点的距离都是0,这里省略了加点,加边的操作,直接将第一次松弛后的状态作为初始状态,这显然是可以的,因为即便加边也是加的从源点到各个点的有向边,第一次松弛结束后不可能再通过源点松弛,所以直接省略源点即可。
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define REP(i, n) for (int i = 1; i <= (n); i++) 6 #define sqr(x) ((x) * (x)) 7 8 const int maxn = 600 + 10; 9 const int maxm = 4000 + 10; 10 const int maxs = 10000 + 10; 11 12 typedef long long LL; 13 typedef pair<int, int> pii; 14 typedef pair<double, double> pdd; 15 16 const LL unit = 1LL; 17 const int INF = 0x3f3f3f3f; 18 const LL mod = 1000000007; 19 const double eps = 1e-14; 20 const double inf = 1e15; 21 const double pi = acos(-1.0); 22 23 struct Edge 24 { 25 int to, next, w; 26 } edge[maxm]; 27 28 int n, m, st; 29 int head[maxn], tot; 30 31 void init() 32 { 33 memset(head, -1, sizeof(head)); 34 tot = 0; 35 } 36 37 void AddEdge(int u, int v, int w) 38 { 39 edge[tot].to = v; 40 edge[tot].w = w; 41 edge[tot].next = head[u]; 42 head[u] = tot++; 43 } 44 45 int dist[maxn], cnt[maxn]; 46 bool vis[maxn]; 47 48 bool spfa() 49 { 50 queue<int> que; 51 memset(cnt, 0, sizeof(cnt)); 52 for(int i = 0; i < n; i++) 53 { 54 que.push(i); 55 dist[i] = 0; 56 vis[i] = true; 57 } 58 while (!que.empty()) 59 { 60 int u = que.front(); 61 que.pop(); 62 vis[u] = false; 63 for (int i = head[u]; i != -1; i = edge[i].next) 64 { 65 int v = edge[i].to; 66 if (dist[v] > dist[u] + edge[i].w) 67 { 68 dist[v] = dist[u] + edge[i].w; 69 if (!vis[v]) 70 { 71 vis[v] = true; 72 que.push(v); 73 if (++cnt[v] > n) 74 { 75 return false; 76 } 77 } 78 } 79 } 80 } 81 return true; 82 } 83 84 bool Judge(int lim) 85 { 86 for (int i = 0; i < m; i++) 87 { 88 edge[i].w -= lim; 89 } 90 bool ok = spfa(); 91 for (int i = 0; i < m; i++) 92 { 93 edge[i].w += lim; 94 } 95 return ok; 96 } 97 98 int main() 99 { 100 ios::sync_with_stdio(false); 101 cin.tie(0); 102 //freopen("input.txt", "r", stdin); 103 //freopen("output.txt", "w", stdout); 104 while (cin >> n >> m) 105 { 106 init(); 107 int u, v, d; 108 int le = 1, ri = 0; 109 for (int i = 0; i < m; i++) 110 { 111 cin >> u >> v >> d; 112 u--, v--; 113 AddEdge(u, v, d); 114 ri = max(ri, d); 115 } 116 117 if(!Judge(le)) 118 { 119 cout << "No Solution" << endl; 120 continue; 121 } 122 else if(Judge(ri + 1)) 123 { 124 cout << "Infinite" << endl; 125 continue; 126 } 127 128 int ans = -INF; 129 while (le <= ri) 130 { 131 int mid = (le + ri) / 2; 132 if (Judge(mid)) 133 { 134 le = mid + 1; 135 ans = mid; 136 } 137 else 138 { 139 ri = mid - 1; 140 } 141 } 142 cout << ans << endl; 143 } 144 return 0; 145 }