1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| #include <bits/stdc++.h> #define MAX_N 500 #define MAX_M 100000 #define mid ((l+r)>>1) #define INF 0x3f3f3f3f using namespace std; int n, m, q, x, s, t, cnt, pr[MAX_N+5], cr[MAX_N+5], f[MAX_N+5], g[MAX_N+5]; struct node {int v, c, w, nxt;} E[MAX_M+5]; int G[MAX_N+5][MAX_N+5], tot, cost; void init() {s = 0, t = n*2+1, memset(pr, -1, sizeof pr);} void insert(int u, int v, int c, int w) {E[cnt] = (node){v, c, w, pr[u]}, pr[u] = cnt++;} void addedge(int u, int v, int c, int w) {insert(u, v, c, w), insert(v, u, 0, -w);} bool SPFA() { queue <int> que; bool inq[MAX_N+5]; int d[MAX_N+5], cr[MAX_N+5]; memset(inq, false, sizeof inq), memset(d, INF, sizeof d); d[s] = 0, que.push(s), inq[s] = true, memset(cr, -1, sizeof cr); while (!que.empty()) { int u = que.front(); que.pop(), inq[u] = false; for (int i = pr[u]; ~i; i = E[i].nxt) { int v = E[i].v, c = E[i].c, w = E[i].w; if (c && d[u]+w < d[v]) { d[v] = d[u]+w, cr[v] = i; if (!inq[v]) que.push(v), inq[v] = true; } } } if (d[t] == INF) return false; int flow = INF; for (int i = cr[t]; ~i; i = cr[E[i^1].v]) flow = min(flow, E[i].c); for (int i = cr[t]; ~i; i = cr[E[i^1].v]) E[i].c -= flow, E[i^1].c += flow; cost = d[t]; return true; } int bi_search(int l, int r) {while (l <= r) f[mid] < x ? l = mid+1 : r = mid-1; return g[l-1]+(n-l+1)*x;} int main() { scanf("%d%d%d", &n, &m, &q), init(); memset(G, INF, sizeof G); for (int i = 1; i <= n; i++) addedge(s, i, 1, 0), addedge(i+n, t, 1, 0), G[i][i] = 0; for (int i = 0, u, v, c; i < m; i++) scanf("%d%d%d", &u, &v, &c), G[u][v] = min(G[u][v], c); for (int k = 1; k <= n; k++) for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) G[i][j] = min(G[i][j], G[i][k]+G[k][j]); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (G[i][j]^INF && (i^j)) addedge(i, j+n, 1, G[i][j]); while (SPFA()) f[++tot] = cost, g[tot] = f[tot]+g[tot-1]; while (q--) scanf("%d", &x), printf("%d\n", bi_search(0, tot)); return 0; }
|