T1:

北京集训:20180323
这题又是动态题面问题,一开始连求什么都没告诉你......
当时自然没有看了,现在没人做没人改......
官方题解:

北京集训:20180323
标程:

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <map>
  4 #include <set>
  5 #include <queue>
  6 #include <vector>
  7 #include <cstring>
  8 #include <cstdlib>
  9 #include <algorithm>
 10 #define rep(i,a,b) for(int i = a; i <= b; i++)
 11 #define dep(i,a,b) for(int i = a; i >= b; i--) 
 12 #define Rep(i,a) for(int i = 0; i < a; i++)
 13 #define pb(a) push_back(a)
 14 #define mp(a,b) make_pair(a,b)
 15 #define ab(x) ((x) < 0 ? -(x) : (x))
 16 using namespace std;
 17 typedef long long LL;
 18 typedef map<int, int>::iterator mit;
 19 typedef set<int>::iterator sit;
 20 typedef pair<int, int> pii;
 21 #define x first
 22 #define y second
 23 const int N = 1e5, E = 1e5, inf = 1e9;
 24 int n, m, q, k, S, T, C[E << 1];
 25 struct edge{ int to, pre, w, c; } e[E << 1]; int u[N], l = 1;//l==1!
 26 void ins(int a, int b, int w, int c) { e[++l] = (edge){b, u[a], w, c}, u[a] = l; }
 27 void insert(int a, int b, int w, int c) { ins(a, b, w, c), ins(b, a, 0, -c); }
 28 #define v e[i].to
 29 #define reg(i,a) for(int i = u[a]; i; i = e[i].pre)
 30 #define ec e[i].c
 31 #define ew e[i].w
 32 int cost, h[N], c;
 33 bool vis[N];
 34 bool dij() {
 35     static pii q[E]; int l = 1;
 36     rep(i,S,T) h[i] = inf, vis[i] = false; q[1] = mp(-(h[S] = 0), S);
 37     while (l) {
 38         int x = q[1].y; pop_heap(q + 1, q + l + 1), l--;
 39         if (vis[x] || h[x] == inf) continue; vis[x] = true;
 40         reg(i,x) if (ew && h[x] + ec < h[v]) {
 41             h[v] = h[x] + ec; 
 42             q[++l] = mp(-h[v], v); push_heap(q + 1, q + l + 1);
 43         }
 44     }
 45     rep(x,S,T) reg(i,x) ec -= h[v] - h[x];
 46     c += h[T];
 47     return h[T] < inf;
 48 }
 49 int cur[N];
 50 int dfs(int x, int f) {
 51     if (x == T) return f;
 52     int w, used = 0; vis[x] = true;
 53     for(int i = cur[x]; i; i = e[i].pre) if (ew && !ec && !vis[v] && ~h[v]) {
 54         w = min(ew, f - used), w = dfs(v, w);
 55         ew -= w; if (ew) cur[x] = i;
 56         e[i ^ 1].w += w;
 57         used += w; if (used == f) break;
 58     }
 59     vis[x] = false;
 60     if (!used) h[x] = -1;
 61     return used;
 62 }
 63 void flow() {
 64     while (dij()) { 
 65         rep(i,S,T) cur[i] = u[i], vis[i] = false;
 66         int w = dfs(S, inf);
 67         cost += c * w; 
 68     }
 69 }
 70  
 71 int ans = 0, d[N];
 72 void addedge(int a, int b, int c) {
 73     ans += c;
 74     insert(a, b, q, 0); d[b]++, d[a]--; insert(b, a, 1, c * 100); 
 75 }
 76 int a[N], b[N];
 77 map<int, int> f;
 78 int check(int c1) {
 79     if (f.find(c1) != f.end()) return f[c1];
 80     static int u1[N], l1; 
 81     rep(i,S,T) u1[i] = u[i]; l1 = l;
 82     rep(i,1,l) C[i] = e[i].c;
 83     rep(i,1,q) insert(b[i], a[i], 1, c1 * 100 + 1);
 84     cost = c = 0; flow();   
 85     rep(i,S,T) u[i] = u1[i]; l = l1;
 86     rep(i,1,l) e[i].c = C[i];
 87     for(int i = 2; i <= l; i += 2) 
 88         e[i].w += e[i ^ 1].w, e[i ^ 1].w = 0;
 89     return f[c1] = cost;
 90 }
 91  
 92 void solve() {
 93     int l = -1, r = 1e5 + 10;
 94     while (l + 1 < r) {
 95         int mid = (l + r) >> 1;
 96         if (check(mid) % 100 <= k) r = mid; else l = mid; 
 97     }
 98     int C = check(r);
 99     ans = ans - C / 100 + r * k;
100 }
101  
102 int main() {
103     scanf("%d%d%d%d",&n,&m,&q,&k);
104     S = 0, T = n + 1;
105     rep(i,1,m) {
106         int a, b, c; scanf("%d%d%d",&a,&b,&c);
107         addedge(a, b, c);
108     }
109     rep(i,1,n) if (d[i] > 0) insert(S, i, d[i], 0); else if (d[i] < 0) insert(i, T, -d[i], 0);
110     rep(i,1,q) scanf("%d%d",a + i, b + i);
111     solve();
112     printf("%d\n", ans);
113     return 0;
114 }
View Code

相关文章:

  • 2021-09-29
  • 2021-10-09
  • 2022-01-17
  • 2022-01-27
  • 2022-01-14
  • 2021-09-13
  • 2021-09-18
  • 2021-08-11
猜你喜欢
  • 2021-04-20
  • 2021-08-14
  • 2022-01-20
  • 2021-09-16
  • 2021-11-27
  • 2021-05-02
  • 2021-12-09
相关资源
相似解决方案