Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
  4. #define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
  5. #define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
  6. #define EACH(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
  7.  
  8. #define DEBUG(x) { cout << #x << " = "; cout << (x) << endl; }
  9. #define PR(a,n) { cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; }
  10. #define PR0(a,n) { cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl; }
  11.  
  12. #define sqr(x) ((x) * (x))
  13. using namespace std;
  14.  
  15. #define F_INF 1000111000LL
  16. #define C_INF 1000111000LL
  17.  
  18. template<class Flow = long long, class Cost = long long>
  19. struct MinCostFlow {
  20. int V, E;
  21. vector<Flow> cap;
  22. vector<Cost> cost;
  23. vector<int> to, prev;
  24.  
  25. vector<Cost> dist, pot;
  26. vector<int> last, path, used;
  27. priority_queue< pair<Cost, int> > q;
  28.  
  29. MinCostFlow(int V, int E) : V(V), E(0), cap(E*2,0), cost(E*2,0), to(E*2,0), prev(E*2,0),
  30. dist(V,0), pot(V,0), last(V, -1), path(V,0), used(V,0) {}
  31.  
  32. int addEdge(int x, int y, Flow f, Cost c) {
  33. cap[E] = f; cost[E] = c; to[E] = y; prev[E] = last[x]; last[x] = E; E++;
  34. cap[E] = 0; cost[E] = -c; to[E] = x; prev[E] = last[y]; last[y] = E; E++;
  35. return E - 2;
  36. }
  37.  
  38. pair<Flow, Cost> search(int s, int t) {
  39. Flow ansf = 0; Cost ansc = 0;
  40. REP(i,V) used[i] = false;
  41. REP(i,V) dist[i] = C_INF;
  42.  
  43. dist[s] = 0; path[s] = -1; q.push(make_pair(0, s));
  44. while (!q.empty()) {
  45. int x = q.top().second; q.pop();
  46. if (used[x]) continue; used[x] = true;
  47. for(int e = last[x]; e >= 0; e = prev[e]) if (cap[e] > 0) {
  48. Cost tmp = dist[x] + cost[e] + pot[x] - pot[to[e]];
  49. if (tmp < dist[to[e]] && !used[to[e]]) {
  50. dist[to[e]] = tmp;
  51. path[to[e]] = e;
  52. q.push(make_pair(-dist[to[e]], to[e]));
  53. }
  54. }
  55. }
  56. REP(i,V) pot[i] += dist[i];
  57. if (used[t]) {
  58. ansf = F_INF;
  59. for(int e=path[t]; e >= 0; e=path[to[e^1]]) ansf = min(ansf, cap[e]);
  60. for(int e=path[t]; e >= 0; e=path[to[e^1]]) { ansc += cost[e] * ansf; cap[e] -= ansf; cap[e^1] += ansf; }
  61. }
  62. return make_pair(ansf, ansc);
  63. }
  64. pair<Flow, Cost> minCostFlow(int s, int t) {
  65. Flow ansf = 0; Cost ansc = 0;
  66. while (1) {
  67. pair<Flow, Cost> p = search(s, t);
  68. if (!used[t]) break;
  69. ansf += p.first; ansc += p.second;
  70. }
  71. return make_pair(ansf, ansc);
  72. }
  73. };
  74. const int MN = 111*111*2;
  75. long long f[111][111], c[111][111];
  76. int id_u[MN], id_v[MN], edges[MN];
  77.  
  78. int main() {
  79. int n, m, k, s, t;
  80. while (cin >> n >> m >> k >> s >> t) {
  81. MinCostFlow<long long, long long> mcf(n+1, m*2+2);
  82.  
  83. mcf.addEdge(0, s, k, 0);
  84.  
  85. memset(f, 0, sizeof f);
  86. FOR(i,1,m) {
  87. int u, v;
  88. cin >> u >> v;
  89. cin >> c[u][v] >> f[u][v];
  90. c[v][u] = c[u][v]; f[v][u] = f[u][v];
  91. }
  92.  
  93. m = 0;
  94. FOR(i,1,n) FOR(j,i+1,n) if (f[i][j]) {
  95. edges[++m] = mcf.addEdge(i, j, f[i][j], c[i][j]); id_u[m] = i; id_v[m] = j;
  96. edges[++m] = mcf.addEdge(j, i, f[i][j], c[i][j]); id_u[m] = j; id_v[m] = i;
  97. }
  98.  
  99. pair<long long, long long> res = mcf.minCostFlow(0, t);
  100.  
  101. if (res.first < k) {
  102. cout << -1 << endl;
  103. continue;
  104. }
  105. cout << res.second << endl;
  106.  
  107. FOR(i,1,m/2) {
  108. int u = id_u[i], v = id_v[i];
  109. int f_xuoi = f[u][v] - mcf.cap[edges[i*2-1]];
  110. int f_nguoc = f[v][u] - mcf.cap[edges[i*2]];
  111.  
  112. int t = min(f_xuoi, f_nguoc);
  113. f_xuoi -= t;
  114. f_nguoc -= t;
  115.  
  116. if (f_xuoi > 0) {
  117. cout << id_u[i*2-1] << ' ' << id_v[i*2-1] << ' ' << f_xuoi << endl;
  118. }
  119. if (f_nguoc > 0) {
  120. cout << id_u[i*2] << ' ' << id_v[i*2] << ' ' << f_nguoc << endl;
  121. }
  122. }
  123. cout << "0 0 0" << endl;
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement