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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
| #include <bits/stdc++.h> #define mp make_pair #define pb push_back #define sz(x) (int)x.size() #define all(x) begin(x), end(x) #define fi first #define se second #define debug(x) cerr << #x << " " << x << '\n' using namespace std; using ll = long long; using pii = pair<int,int>; using pli = pair<ll,int>; const int INF = 0x3f3f3f3f; const ll LINF = 1e18 + 5; constexpr int mod = 1e9 + 7; const int N = 1e3 + 5, M = 1e6 + 5; int n, q, m, x[N], y[N]; int cnt, head[N]; struct node { int next, to, w, f; }e[M<<1]; void add(int u,int v,int w,int f) { e[++cnt] = {head[u],v,w,f}; head[u] = cnt; } struct MCMF { int n, m, s, t; int flow, cost; bool inq[N]; int d[N], p[N], a[N]; void init(int n,int s,int t) { this->n = n, this->s = s, this->t = t; cnt = 1, m = 0, flow = cost = 0; memset(head,0,(n+1)*sizeof(int)); } void addedge(int u,int v,int cap,int cost) { add(u,v,cap,cost); add(v,u,0,-cost); m += 2; } bool spfa() { memset(d,0x3f,(n+1)*sizeof(int)); memset(inq,0,(n+1)*sizeof(bool)); queue<int> q; q.push(s); d[s] = 0, a[s] = INF; while(!q.empty()) { int u = q.front(); q.pop(); inq[u] = 0; if(u==t) continue; for(int i=head[u];i;i=e[i].next) { int v = e[i].to; if(e[i].w&&d[v]>d[u]+e[i].f) { d[v] = d[u] + e[i].f; p[v] = i; a[v] = min(a[u],e[i].w); if(!inq[v]) { q.push(v); inq[v] = 1; } } } } return d[t] != INF; } void go() { while(spfa()) { flow += a[t]; cost += a[t] * d[t]; int u = t; while(u!=s) { e[p[u]].w -= a[t]; e[p[u]^1].w += a[t]; u = e[p[u]^1].to; } } } }MM; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m >> q; MM.init(2*n+2, 0, 2*n+1); for(int i=1; i<=n; i++) { int a, b; cin >> a >> b; for(int j=1; j<=q; j++) MM.addedge(i, n+i, 1, (j-1)*b+a); } for(int i=1; i<=m; i++) { int c, d; cin >> c >> d; MM.addedge(c+n, d, q, 0); MM.addedge(d+n, c, q, 0); } for(int i=1; i<=q; i++) cin >> x[i]; for(int i=1; i<=q; i++) cin >> y[i]; for(int i=1; i<=q; i++) { MM.addedge(MM.s, x[i], 1, 0); MM.addedge(y[i]+n, MM.t, 1, 0); } MM.go(); cout << MM.cost << '\n'; return 0; }
|