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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
| import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.StreamTokenizer; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.HashMap; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.Queue; import java.util.Scanner; import java.util.Stack; import java.util.StringTokenizer;
import com.sun.org.apache.regexp.internal.recompile;
public class Main { public static void main(String[] args) throws IOException{ StreamTokenizer cin = new StreamTokenizer(new BufferedInputStream(System.in)); InputReader in = new InputReader(System.in) ; PrintWriter out = new PrintWriter(System.out) ; while(cin.nextToken() != cin.TT_EOF){ new Task().solve(cin, out) ; } out.flush() ;
} }
class Task{ static int[] a = new int[108] ; static int[] b = new int[108] ; static ArrayList<Integer>[] adj = new ArrayList[108] ; static ArrayList<Integer>[] colorg = new ArrayList[108] ; static int[] color = new int[108] ; void dfs(int u , int c){ color[u] = c ; colorg[c].add(u) ; for(int v : adj[u]){ if(color[v] == -1) dfs(v , c) ; } } int[] dist = new int[108] ; boolean[] in = new boolean[108] ; void spfa(int s){ Arrays.fill(dist, Integer.MAX_VALUE) ; dist[s] = 0 ; Arrays.fill(in, false) ; in[s] = true ; Queue<Integer> q = new LinkedList<Integer>() ; q.add(s) ; while(! q.isEmpty()){ int u = q.poll() ; in[u] = false ; for(int v : adj[u]){ if(dist[u] + 1 < dist[v]){ dist[v] = dist[u] + 1 ; if(! in[v]){ in[v] = true ; q.add(v) ; } } } } } public void solve(StreamTokenizer cin , PrintWriter out) throws IOException{ int n = (int) cin.nval ; cin.nextToken() ; int m = (int) cin.nval ; for(int i = 1 ; i <= n ; i++){ cin.nextToken() ; a[i] = (int) cin.nval ; cin.nextToken() ; b[i] = (int) cin.nval ; } for(int i = 1 ; i <= n ; i++) adj[i] = new ArrayList<Integer>() ; while(m-- > 0){ cin.nextToken() ; int u = (int) cin.nval ; cin.nextToken() ; int v = (int) cin.nval ; adj[u].add(v) ; adj[v].add(u) ; } Arrays.fill(color, -1) ; for(int i = 0 ; i <= n ; i++) colorg[i] = new ArrayList<Integer>() ; int k = 0 ; for(int i = 1 ; i <= n ; i++){ if(color[i] == -1){ dfs(i , k++) ; } } int sum = 0 ; for(int i = 0 ; i < k ; i++){ int sa = 0 , sb = 0 ; for(int u : colorg[i]){ sa += a[u] ; sb += b[u] ; sum += Math.abs(a[u] - b[u]) ; } if(sa != sb){ out.println(-1) ; return ; } } ArrayList<Integer> adx = new ArrayList<Integer>() ; ArrayList<Integer> ady = new ArrayList<Integer>() ; for(int i = 1 ; i <= n ; i++){ if(a[i] > b[i]) adx.add(i) ; else if(a[i] < b[i]) ady.add(i) ; } int N= adx.size() ; int M= ady.size() ; Mincostflow mcf = new Mincostflow(0 , 2*N+2*M+1) ; for(int i = 1 ; i <= N ; i++){ int u = adx.get(i-1) ; mcf.add(i, N + i, a[u] - b[u] , 0) ; mcf.add(0, i, Integer.MAX_VALUE , 0) ; } for(int i = 1 ; i <= M ; i++){ int u = ady.get(i-1) ; mcf.add(2*N + i, 2*N + M + i, b[u] - a[u] , 0 ) ; mcf.add(2*N+M+i , 2*N+2*M+1, Integer.MAX_VALUE , 0) ; } for(int i = 1 ; i <= adx.size() ; i++){ int u = adx.get(i-1) ; spfa(u) ; for(int j = 1 ; j <= ady.size() ; j++){ int v = ady.get(j-1) ; if(dist[v] == Integer.MAX_VALUE) continue ; mcf.add(N+i, 2*N+j, Integer.MAX_VALUE, dist[v] ) ; } } out.println(mcf.mincostflow()) ; } }
class Mincostflow{ public Mincostflow(int sourse , int meet){ this.source = sourse ; this.meet = meet ; Arrays.fill(g, 0) ; id = 1 ; } static final int maxn = 5000 , maxm = 50000 ; static class Edge{ int v , f , w , next ; Edge(){} Edge(int _v , int _f , int _w , int _next){ this.v = _v ; this.f = _f ; this.w = _w ; this.next = _next ; } } static int[] g = new int[maxn + 10] ; static Edge[] e = new Edge[maxm + 10] ; int source , meet ; int id ;
void add(int u , int v , int f , int w){ e[++id] = new Edge(v , f , w , g[u]) ; g[u] = id ; e[++id] = new Edge(u , 0 , -w , g[v]) ; g[v] = id ; }
Queue<Integer> que = new LinkedList<Integer>() ; static boolean[] in = new boolean[maxn + 10] ; static int[] dist = new int[maxn + 10] ; static int[] pv = new int[maxn + 10] ; static int[] pe = new int[maxn + 10] ;
boolean bfs(){ while(! que.isEmpty()) que.poll() ; que.add(source) ; Arrays.fill(dist, Integer.MAX_VALUE) ; dist[source] = 0 ; in[source] = true ; while(! que.isEmpty()){ int u = que.poll() ; in[u] = false ; for(int i = g[u] ; i > 0; i = e[i].next){ int v = e[i].v ; if(e[i].f > 0 && dist[u] + e[i].w < dist[v]){ dist[v] = dist[u] + e[i].w ; pv[v] = u ; pe[v] = i ; if(! in[v]){ in[v] = true ; que.add(v) ; } } } } return dist[meet] < Integer.MAX_VALUE ; }
int augment(){ int u = meet ; int delta = Integer.MAX_VALUE ; while(u != source){ delta = Math.min(delta , e[pe[u]].f) ; u = pv[u] ; } u = meet ; while(u != source){ e[pe[u]].f -= delta ; e[pe[u] ^ 1].f += delta ; u = pv[u] ; } return dist[meet] * delta ; }
int mincostflow(){ int ans = 0 ; while(bfs()) ans += augment() ; return ans ; } }
class InputReader{ public BufferedReader reader; public StringTokenizer tokenizer;
public InputReader(InputStream stream){ reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; }
public String next(){ while(tokenizer == null || !tokenizer.hasMoreTokens()){ try{ tokenizer = new StringTokenizer(reader.readLine()); }catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); }
public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); }
}
|